Posts

Showing posts from 2016

Q3 copy constructor program .

 #include<iostream> using namespace std; class A { int x, y; public : A():x(20),y(40) { } A(const A &ob){ x=ob.x; y=ob.y; } void show() { cout<<x<<endl<<y<<endl; } }; int main() { A obj1; A obj2=obj1; //copy constructor is called . obj2.show(); return 0; }

Q2 program for Assignment operator/unary operator(prefix and postfix )/binary operator overloading .

#include<iostream> using namespace std; class A { int x,y; public : A() { x=10; y=20; } void show(); A operator +(A obj) { A temp; temp.x=x+obj.x; temp.y=y+obj.y; return temp; } A operator -(A obj) { A temp; temp.x=x-obj.x; temp.y=y-obj.y; return temp; } //prefix expression overloaded A operator ++() { x++; ++y; return *this; } //postfix expression overloaded A operator ++(int ) { x++; ++y; return *this; } A operator =(A obj) { this->x=obj.x; this->y=obj.y; return *this; } }; void A::show(){ cout<<x<<endl<<y<<endl; } int main() { A obj1,obj2,obj3; obj1=obj1+obj2;//overloaded assignment operator and binary operator will be called . obj1.show(); ++obj1; //overloaded unary "prefix"  operator will be  called . obj1++; //overloaded unary "post"  operator will be  called . obj1.show(); obj3=obj1 ;//overloaded assignment operator will be called . obj3.show(); return 0; }

Q 1 write pograme in c++ for passbyvalue/passbyrefrence/passbyaddress .

#include<iostream> using namespace std; class passvalue { public:     void passbyvalue(int x, int y); //pass by value     void passbyrefrence(int &x, int &y);//pass by refrence.     void passbyaddress(int *x, int *y); //pass by address. }; void passvalue::passbyvalue(int x, int y) { x=10; y=20; } //actual value will be changed . void passvalue::passbyrefrence(int &x, int &y) { //passbyrefence : cout<<"passbyrefrence called"<<endl; x=60; y=50; } //actual value will be changed . void passvalue::passbyaddress(int *x, int *y) { cout<<"passbyaddress is  called"<<endl; *x=70; *y=80; } int main() { int i=40,j=30; passvalue B; cout<<"actual value"<<endl; cout<<i<<endl<<j<<endl; B.passbyvalue(i,j); cout<<"passbyvalue called"<<endl; cout<<i<<endl<<j<<endl; cout<<"passbyrefrence  called"<<endl; B.passbyrefrence(i,j); c