Você está na página 1de 27

1.

Write Program for private access specifier

#include<iostream.h> #include<conio.h> class furniture { private int height,width;: public: } void main() { furniture f; f.height=10; f.width=20; cout<<"Height="<<height<<"\n Width="<<width; }

Output: when we compile this Programe it will give errors that are furniture::height is not accessible furniture::width is not accessible because height,width you have declared in the private scope so, that maynot be accessible out side of the class.

2. Write Program for function overloading


#include<iostream.h> #include<conio.h> class funover { private: int num1,num2,num3; float fnum1,fnum2; public: void add(); int add(int); float add(int,float); float add(float,int); int add(int,int,int); }; void funover::add() { num1=10; num2=20; num3=num1+num2; cout<<num3<<endl; } int funover::add(int n1) { num1=n1;

num2=20; num3=num1+num2; return num3; } float funover::add(int n1,float f2) { num1=n1; fnum2=f2; return num1+fnum2; } float funover::add(float f1,int n2) { fnum1=f1; num2=n2; return f1+n2; } int funover::add(int n1,int n2,int n3) { num1=n1; num2=n2; num3=n3; return num1+num2+num3; }

void main() { funover f; f.add() cout<<f.add(10); cout<<f.add(10,20.5); cout<<f.add(20.5,10); cout<<f.add(10,20,30); }

Output: 30 30 30.5 30.5 60

3. Write a program for Friend function


#include<iostream.h> #include<conio.h> class sample { private: public: { height=10; width=20; } friend void show(sample a) { cout<<a.height<<" " <<a.width; } }; void main() { clrscr(); sample s; s.getdata(); show(s); getch(); } Output: 10 20 int height,width; void getdata()

4. Write Program for Unary Operator Overloading


#include<iostream.h> #include<conio.h> class operat_over { private: int num1,num2,num3; public: void getdata() { num1=10; num2=20; num3=30; } void operator+() { num1=-num1; num2=-num2; num3=-num3; } void operator-() { num1=num1+num1; num2=num2+num2; num3=num3+num3; }

void show() { cout<<num1=<<num1; cout<<num2=<<num2; cout<<num3=<<num3; } }; void main() { operat_over op; op.getdata(); cout<<Before Operator Overloading values are \n; op.show(); cout<< After Operator Overloading values are \n; +op; op.show(); -op; op.show(); }

Output Before Operator Overloading values are num1=10 num2=20 num3=30 After Operator Overloading values are num1=-10 num2=-20 num3=-30 num1=0 num2=0 num3=0

5. Program for Binary Operator Overloading using Complex numbers


#include<iostream.h> #include<conio.h> class complex { private: float real,imag; public: complex() { real=0; imag=0; } complex(float x,float y) { real=x; imag=y; } complex operator+(complex); void show(); }; complex complex::operator+(complex c) { complex temp;

temp.real=real+c.real; temp.imag=imag+c.imag; return temp; } void complex::show() { cout<<real<< + i <<imag<<endl; } void main() { complex c1(10.5,20.5); complex c2(20.5,10.5); complex c3=c1+c2; c1.show(); c2.show(); cout<<Complex numbers addition is <<endl; c3.show(); getch(); }

output 10.5 + i 20.5 20.5 + i 10.5 Complex numbers addition is 31 + i 31

6. Write a Program for Virtual base class


#include<iostream.h> #include<conio.h> class furniture { protected: int height,width,length,legs; public: void getdata(int h,int w,int l,int lg) { height=h; width=w; length=l; legs=lg; } void show() { cout<<"Height="<<height<<endl; cout<<"Width="<<width<<endl; cout<<"Length="<<length<<endl; cout<<"Legs="<<legs<<endl; } };

class bookshelf:virtual public furniture { protected: public: }; class chair:virtual public furniture { protected: public: }; class windows:public bookshelf,public chair { private: public: } void main() { clrscr(); windows w; w.getdata(10,20,30,4); w.show(); getch(); }

Output: Height=10 Width=20 Length=30 Legs=4

7. Program for matrix addition


#include<iostream.h> #include<conio.h> class matrix_add { private: int a[3][3],b[3][3],c[3][3],i,j; public: void getdata() { cout<<"Enter the Elements of First matrix"<<endl; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>a[i][j]; cout<<"Enter the Elements of Second matrix"<<endl; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>b[i][j]; } void calculate() { for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; }

void show() { for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<c[i][j]<<"\t"; } cout<<"\n"; } } }; void main() { clrscr(); matrix_add m; m.getdata(); m.calculate(); cout<<"Resultant matrix is"<<endl; m.show(); getch(); }

Output: Enter the Elements of First matrix 1 2 3 4 5 6 7 8 9 Enter the Elements of Second matrix 1 1 1 1 1 1 1 1 1 Resultant matrix is 2 5 8 3 6 9 4 7 10

8. Program for Exception handling Using Multiple Catch Blocks


#include<iostream.h> #include<conio.h> void test(int a) { try { if(a==1) throw a; else if(a==0)throw a; else if(a==-1)throw 1.0; } catch(int i) { cout<<Caught an integer<<endl; } catch(char c) { cout<<Caught a character<<endl; } catch(double d) { cout<<Caught a double<<endl; }

} void main() { cout<<a==1<<endl; test(1); cout<<a==0<,endl; test(0); cout<<a==-1<<endl; test(-1); getch(); }

Output: a==1 Caught an integer a==0 Caught a character a==-1 Caught a double

9. Program for Function Template


#include<iostream.h> #inlcude<conio.h> template<class T> void swap(T &x,T &y) { T temp=x; x=y; y=temp; } void fun(int m,int n,float a,float b) { cout<<m and n before swap:<<m<< \t<<n<<endl; swap(m,n); cout<<m and n after swap:<<m<< \t<<n<<endl; cout<<a and b before swap:<<a<< \t<<b<<endl; swap(a,b); cout<<a and b after swap:<<a<< \t<<b<<endl; } void main() { fun(100,200,10.5,20.5); getch(); }

Output: m and n before swap:100 200 m and n after swap:200 100 a and b before swap:10.5 20.5 a and b after swap:20.5 10.5

10. Write Program for Dynamic Constructor (or) program for Dynamic Memory Allocation
#include<iostream.h> #include<conio.h> class dynamic_cons { private: int length; char *name; public: dynamic_cons() { length=0; name=new char[length+1]; } dynamic_cons(char *s) { length=strlen(s); delete name; name=new char[length+1]; strcpy(name,s); } void show() { cout<<name<<endl; }

}; void main() { dynamic_cons d1; dynamic_cons d2(C++); dynamic_cons d3(C++ & JAVA); d2.show(); d3.show(); }

output C++ C++ & JAVA

Você também pode gostar