Escolar Documentos
Profissional Documentos
Cultura Documentos
#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.
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 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
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(); }
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(); }
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
Output: a==1 Caught an integer a==0 Caught a character a==-1 Caught a double
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(); }