You are on page 1of 38

Program 1

Write a program to implement Divide by zero exception. #include<iostream.h> #include<conio.h> class sample { public: int num1,num2; void divide(); }; void sample::divide() { try { cout<<"enter first number: "; cin>>num1; cout<<"\nenter second number: "; cin>>num2; if(num2!=0) cout<<"result: " <<num1/num2; else throw 2; } catch(int index) { cout<<"\ndivision by zero exception occured"; } } void main() { sample obj; obj.divide(); getch(); }

OUTPUT enter first number: 6 enter second number: 9 result: 0

Program 2
Write a program to read and remove all duplicate elements in array. #include<iostream.h> #include<conio.h> void main() { int n, first[10], second[10], i = 0, q, y; clrscr(); cout<<"Enter the number of elements in array: "; cin>>n; cout<<"Enter the elements in array: "; for(q=0;q<n;q++) cin>>first[q]; for(q=0;q<n;q++) { for(y=0;y<i;y++) { if(first[q]==second[y]) break; } if(y==i) { second[i] = first[q]; i++; } } cout<<"Array obtained after removing duplicate elements: "<<"\n"; for(q=0;q<i;q++) cout<<second[q]; getch(); }

OUTPUT Enter the number of elements in array: 5 Enter the elements in array: 1 4 2 2 6 array obtained after removing duplicate elements: 1246

Program 3
Write a program to find youngest and eldest person age using array. #include<iostream.h> #include<conio.h> void main() { int age[100],young,old,r,num; clrscr(); cout<<"number of person whose age are to be interted: "; cin>>num; cout<<"enter the ages: "; for(r=1;r<=num;r++) { cin>>age[r]; } old=age[1]; young=age[1]; for(r=1;r<=num;r++) { if(old<age[r]) { old=age[r]; } } for(r=1;r<=num;r++) { if(young>age[r]) { young=age[r]; } } cout<<"\n"<<"eldest person has age: "<<old; cout<<"\n"<<"youngest person has age: "<<young; getch(); }

OUTPUT Number of person whose age are to be entered: 4 Ether the ages: 34 17 27 56 eldest person has age: 56 youngest person has age: 17

Program 4
Write a program to check array size (if reference is not valid, throw exception). #include<iostream.h> #include<conio.h> class sample { public: int num1,num[100],i;

void divide(); }; void sample::divide() { try { cout<<"enter the size of array: "; cin>>num1; if(num1>100) { throw 1; } if(num1<100) { throw 'm'; } } catch(int index) { cout<<"\narray size limit is exceeded"; } catch(char m) { cout<<"\narray size is in limit"; cout<<"\nenter the elements of array:" ; int sum=0; for(i=1;i<=num1;i++) { cin>>num[num1]; sum=sum+num[num1]; } cout<<"sum is "<<sum; } } void main() { sample obj; obj.divide(); getch(); }

OUTPUT Enter the size of array: 90 array size is in limit

Program 5
Write a program to check input as positive or negative through exception handling and test it for if zero condition. #include<iostream.h> #include<conio.h> class sample { public: int num1; void divide(); }; void sample::divide() { try

{ cout<<"enter the number: "; cin>>num1; if(num1>0) throw 1; if(num1<0) throw 'm'; if(num1==0) cout<<"\nnumber is zero"; } catch(int index) { cout<<"\nnumber is positive"; } catch(char m) { cout<<"\nnumber is negative"; } } void main() { sample obj; obj.divide(); getch(); }

OUTPUT Enter the number: -4 Number is negative

Program 6
Write a program to accept a string and to convert it into capital letters. #include<iostream.h> #include<conio.h> #include<stdio.h> #include<ctype.h> void main() { char name[50]; int i; clrscr(); cout<<"enter the string: ";

gets(name); for(i=0;name[i]!='\0';i++) { name[i]=toupper(name[i]); } puts(name); getch(); }

OUTPUT enter the string: priya PRIYA

Program 7
Write a program using function overloading, overload in three ways so that it return square root for int, double and long int. #include<iostream.h> #include<conio.h> #include <math.h> int squareroot(int s) { return sqrt(s); }

double squareroot(double s1) { return sqrt(s1); } long squareroot(long s2) { return sqrt(s2); } void main() { clrscr(); cout<<"\nThe square root of is: "<< squareroot(625); cout<<"\nThe square root of is: "<<squareroot(625.25); cout<<"\nThe square root of is: "<<squareroot(62500); getch(); }

OUTPUT the square root is: 25 the square root is: 25.005 the square root is: 250

Program 8
Write a program to implement Unary operator overloading. #include<iostream.h> #include<conio.h> class sample { int m,n; public: void getdata(int a, int b); void display(); void operator-(); }; void sample::getdata(int a, int b) { n=b; m=a; } void sample::display() { cout<<m<<"\t "; cout<<n<<"\t "; } void sample::operator-() { m=-m; n=-n; } void main() {

clrscr(); sample ob; ob.getdata(40,-25); cout<<"\nvalues of data member before operator overloading: "; ob.display(); -ob; cout<<"\nvalues of data member after operator overloading: "; ob.display(); getch(); }

OUTPUT values of data member after operator overloading: 40 values of data member after operator overloading: -40

-25 25

Program 9
Write a program to implement Binary operator overloading. #include<iostream.h> #include<conio.h> class sample { float m,n; public: sample(){}; sample(float a, float b) { n=b; m=a; } void display() { cout<<"m = "<<m; cout<<"\nn = "<<n; } sample operator+(sample); }; sample sample::operator+(sample c) { sample temp; temp.m=m+c.m; temp.n=n+c.n; return(temp); } void main() { clrscr(); sample ob,ob1,ob2; ob=sample(1.5,7.25); ob1=sample(5.25,2.15); ob2=ob+ob1; cout<<"\nfirst object is:\n"; ob.display();

cout<<"\nsecond object is:\n"; ob1.display(); cout<<"\nsum of object is:\n"; ob2.display(); getch(); } OUTPUT First object is : m=1.5 n=7.25 second object is: m=5.25 n=2.15 sum of object is: m=6.75 n=9.4

Program 10
Write a program using friend function. #include<iostream.h> #include<conio.h> class sample { int a,b; public: void set() { a=25; b=40; } friend float mean(sample s); }; float mean(sample s) { return(s.a+s.b)/2.0; } void main() { clrscr(); sample ob; ob.set(); cout<<"mean value is "<<mean(ob); getch(); }

OUTPUT mean value is 32.5

Program 11
Write a program with generic function. #include<iostream.h> #include<conio.h>

template<class sample> void swap(sample &num1, sample &num2) { sample temp; temp=num1; num1=num2; num2=temp; } void main() { int first=10, second=20; double n1=10.1, n2=20.6; char a='y', b='h'; clrscr(); cout<<"\noriginal first and second: "<<first<<"\t"<<second; cout<<"\noriginal n1 and n2: "<<n1<<"\t"<<n2; cout<<"\noriginal a and b: "<<a<<"\t"<<b; swap(first,second); swap(n1,n2); swap(a,b); cout<<"\nswapped first and second: "<<first<<"\t"<<second; cout<<"\nswapped n1 and n2: "<<n1<<"\t"<<n2; cout<<"\nswapped a and b: "<<a<<"\t"<<b; getch(); }

OUTPUT original first and second: 10 20 original n1 and n2: 10.1 20.6 original a and b: p r swapped first and second: 20 10 swapped n1 and n2: 20.6 10.1 swapped a and b: r p

Program 12
Write a program to overload + operator. #include<iostream.h> #include<conio.h>

class sample { float a,b; public: sample(){}; sample(float x, float y) { a=x; b=y; } sample operator+(sample); void display() { cout<<"a = "<<a<<"\n"; cout<<"b = "<<b<<"\n"; } }; sample sample::operator+(sample c) { sample temp; temp.a=a+c.a; temp.b=b+c.b; return temp; } void main() { clrscr(); sample x,y,z; x=sample(1.5,7.25); y=sample(5.25,2.15); z=x+y; cout<<"\nfirst object is:\n"; x.display(); cout<<"second object is:\n"; y.display(); cout<<"sum of objects is:\n"; z.display(); getch(); }

OUTPUT First object is : m=1.5 n=7.25 second object is: m=5.25 n=2.15 sum of object is: m=6.75 n=9.4

Program 13
Write a program to overload modulus operator. #include<iostream.h> #include<conio.h> class MyClass { public: void Set(int i) { m_x = i; } operator%(constMyClass &mc) { return m_x % mc.m_x; } operator%(int i) { return m_x % i; } protected: int m_x; }; void main() { MyClass x,y; x.Set(100); y.Set(9); cout << "x%y = " << x % y <<endl; cout << "x%13 = " << x % 13 <<endl; getch(); }

OUTPUT x%y = 1 x%13 = 9

Program 14

Write a program to Overload ++ and -- operators. #include<iostream.h> #include<conio.h> class sample { int m,n; public: sample(){}; sample(int a, int b) { n=b; m=a; } void display() { cout<<m<<"- "<<n<<"i\n"; } void display1() { cout<<m<<"+ "<<n<<"i\n"; } sample operator--(); sample operator++(); }; sample sample::operator--() { sample temp; temp.m=--m; temp.n=--n; return(temp); } sample sample::operator++() { sample temp; temp.m=++m; temp.n=++n; return(temp); } void main() {

clrscr(); sample ob(40,25); --ob; ob.display(); --ob; ob.display(); ++ob; ob.display1(); ++ob; ob.display1(); getch(); }

OUTPUT 39- 24i 38- 23i 39+ 24i 40+ 25i

Program 15
Write a program using virtual function, explain polymorphism. #include<iostream.h>

#include<conio.h> class base { public: virtual void vfunc() { cout<<"\nthis is base class virtual function"; } }; class derived:public base { public: void vfunc() { cout<<"\nthis is derived class virtual function"; } }; void main() { clrscr(); base *p,b; derived d; p=&b; p->vfunc(); p=&d; p->vfunc(); getch(); }

OUTPUT this is base class virtual function this is derived class virtual function

Program 16
Write a program implement Bubble sort using generic function. #include<iostream.h> #include<conio.h> template<class X> void bsort(X *a,int count) { X i,j,temp; for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } void main() { int iarray[7] = {7, 5, 4, 3, 9, 8, 6}; int i; double darray[5] = {4.3, 2.5, -0.9, 10.2, 3.0}; clrscr(); cout << "Here is unsorted integer array: "; for( i=0; i<7; i++) cout << iarray[i] << ' '; cout << endl; bsort(iarray, 7); cout << "Here is sorted integer array: "; for( i=0; i<7; i++) cout << iarray[i] << ' '; cout << endl; cout << "Here is unsorted double array: "; for( i=0; i<5; i++) cout << darray[i] << ' '; cout << endl;

bsort(darray, 5); cout << "Here is sorted double array: "; for( i=0; i<5; i++) cout << darray[i] << ' '; cout << endl; getch(); }

OUTPUT Here is unsorted integer array: 7 5 4 3 9 8 6 Here is sorted integer array: 3 4 5 6 7 8 9 Here is unsorted double array: 4.3 2.5 -0.9 10.2 3.0 Here is sorted double array: -0.9 2.5 3 4.3 10.2

Program 17
Write a program to show public inheritance. #include<iostream.h> #include<conio.h>

class base { int i,j; public: void set(int a, int b) { i=a; j=b; } void display() { cout<<i<<"\t"<<j; } }; class derived:public base { int k; public: void setk(int x) { k=x; } void show() { cout<<"\t"<<k; } }; void main() { clrscr(); derived ob; ob.set(10,20); ob.display(); ob.setk(30); ob.show(); getch(); } OUTPUT 30 10 20

Program 18
Write a program to show private inheritance. #include<iostream.h> #include<conio.h> class base { protected: int i,j; public: void set(int a, int b) { i=a; j=b; }

void display() { cout<<i<<"\t"<<j; } }; class derived:private base { int k; public: void show() { set(10,20); display(); k=i*j; cout<<"\t"<<k; } }; void main() { clrscr(); derived ob; ob.show(); getch(); }

OUTPUT 10 20

200

Program 19
Write a program to show protected inheritance. #include<iostream.h> #include<conio.h> class base { protected: int i,j; public: void set(int a, int b) { i=a; j=b; } void display() { cout<<i<<"\t"<<j; } }; class derived:protected base { int k; public: void show()

{ set(10,20); display(); k=i*j; cout<<"\t"<<k; } }; void main() { clrscr(); derived ob; ob.show(); getch(); }

OUTPUT 10 20

200

You might also like