Skip to main content

Posts

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;             } ...
Recent posts

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++)                        ...