Skip to main content

Posts

Showing posts from August, 2014

OOPS CPP Program-20

20. Write a program to find factorial of a given number. Throw multiple exceptions and define multiple catch statements to handle negative number andout of memory exception. Negative number exception thrown if given number is negative value and out of memory exception is thrown if the given number is greater than 20 # include<iostream> using namespace std; int main()     {         int n,i,f=1;         cout<<"\n enter a number";         cin>>n;         try         {             if(n>0&&n<20)             {                 for(i=1;i<n+1;i++)                 f=f*i;             cout<<"the factorial is"<<f;             } ...

OOPS CPP Program-19

19.Write a program to find average marks of the subjects of a student. Throw multiple exceptions and define multiple catch statements to handle division by zero as well as array index out of bounds exceptions. # include<iostream> using namespace std; int main()  {     int n,marks[10];     float avg=0,tot=0;     cout<<"\n enter the no of subjects";     cin>>n;     cout<<"\n enter the marks of a student\n"<<n<<"students\n";     try         {             if(n>0&&n<=10)             {                 for(int i=1;i<=n;i++)                 {                     cout<<"enter marks of student"<<i<<":\t";         ...

OOPS CPP Program-18

18. Write a program to search a key element in a given set of elements using class template. #include<iostream> #include<string.h> using namespace std; template<class t> class array {     t a[30],se;int size;  public:       void get() {     cout<<"\n Enter the size of array";     cin>>size;     cout<<"\n Enter "<<size<<" elements";         for(int i=0;i<size;i++)          {               cin>>a[i];          } } void search() {     cout<<"\n Enter the elements to search";     cin>>se;     int f=0;     for(int i=0;i<size;i++)         if(a[i]==se)         {              f=1;cout<<"\n Element found at positio...

OOPS CPP Program-17

17. Write a program to sort a given set of elements using function template. #include<iostream>  #include<string.h>  using namespace std;  template<class t>  class array  {      int size;     t a[30];  public:     void get()    {      cout<<"\n Enter the  array size";      cin>>size;      cout<<"\n Enter "<<size<<" elements";      for(int i=0;i<size;i++)       cin>>a[i];    }  void print()  {     cout<<"\n The sorted array is: ";         for (int i=0;i<size;i++)          cout<<a[i]<<"\t";  }  void sort()  {    int i,j;    t temp;    for(i=0;i<size;i++)      for(j=0;j<size-1;...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...

OOPS CPP Program-16

16. Write a program that uses functions to perform the following operations:             a. To copy contents of one file into another file. #include<iostream> #include<fstream> #include<string.h> using namespace std; int main()  {     fstream ifile;     fstream ofile;     char fname[30],st[30];     ofile.open("temp.cpp",ios::out);     cout<<"\n Enter the existing file name";     cin>>fname;     ifile.open(fname,ios::in);     while(ifile)         {             ifile.getline(st,30);             ofile<<st;         } ifile.close(); ofile.close(); cout<<"\n The contents of the copied file are\n"; fstream tfile; tfile.open("temp.cpp",ios::in); char str[30]; while(tfile)     {       ...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-13

13.Write a program to add two matrices of mxn size using binary operator overloading. #include<iostream> using namespace std; class matrix  {         int m, n, x[30][30];  public:         matrix(int a, int b)        {                  m=a;                 n=b;        }         matrix(){}         void get();         void put();         matrix operator +(matrix); };  void matrix:: get() {           cout<<"\n Enter values into the matrix";                for(int i=0; i<m; i++)                        for(int j=0; j<n;j++)              ...

OOPS CPP Program-12

12. Write a program to calculate bonus of the employees. The class master derives the information from both admin and account classes which intern derives information from class person. Create base and all derived classes having same member functions called getdata, display data and bonus. Create a base class pointer that capable of accessing data of any class and calculates bonus of the specified employee. (Hint: Use virtual functions) # include <iostream> using namespace std; class person {         int id;         char name[30]; public:         virtual void getdata();         virtual void display();         virtual void bonus(); }; void person :: getdata() {         cout <<"\n Enter the name and number of the employee: "<< endl;         cin >> name>>id; } void person :: display() {     ...

OOPS CPP Program-11

11. Write a program to calculate gross and net pay of employee from basic salary. Create employee class which consists of employee name, emp_id, and basic salary as its data members. Use parameterized constructor in the derived class to initialize data members of the base class and calculate gross and net pay of the employee in the derived class. #include<iostream.h> #include<string.h> class employee {     protected:                 int id,basic;                                     char name[20];     public:                 employee(int i,char *s,int b)                     {                         id=i;                   ...

OOPS CPP Program-10

10. Write a program which prompts the user to enter a string and returns the length of the longest sequence of identical consecutive characters within the string using pointers to data members and member function. For example, in the string "aaaAAAAAjjB", the longest sequence of identical consecutive characters is "AAAAA". #include<iostream> #include<string> using namespace std; class sequence {             char s[20];                                     public:             void getdata();             void find(sequence); }; void sequence::getdata() {             cout<<"Enter a String: ";             cin>>s; } void sequence::find(sequence ob) {             int len,max,i,j,k=0,c...

OOPS CPP Program-9

9. Write a program for finding area of different geometric shapes (circle, rectangle, cube). Use function overloading with type, order, sequence of arguments to find the area of shapes. #include<iostream.h> #include<iomanip.h> float area(int);            // circle float area(float);         // cube  int area(int,int);          // rectangle void main() {             int radius,length,breadth;             float side;             cout<<"Radius of Circle: ";             cin>>radius;             cout<<"Length of Side of Cube: ";             cin>>side;             cout<<"Length and Breadth of Rectangle: ";            ...