How Copy Constructor & Parametrized constructor executes twice
Write your question here.
How Copy Constructor & Parametrized constructor executes twice? why object b value is different?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
# include<iostream.h>
#include<conio.h>
class add {
int num1, num2, sum;
public:
add()
{ cout<<"\n Constructor without parameters.. ";
num1= 0; num2= 0; sum = 0;
}
add ( int s1, int s2 )
{ cout<<"\n Parameterized constructor... ";
num1= s1; num2=s2; sum=NULL;
}
add (add &a)
{
cout<<"\n Copy Constructor ... ";
num1= a.num1; num2=a.num2; sum = NULL;
}
void getdata()
{
cout<<"Enter data ... ";
cin>>num1>>num2;
}
void addition(add b)
{
sum=num1+ num2 +b.num1 + b.num2;
}
add addition()
{
add a(5,6);
sum = num1 + num2 +a.num1 +a.num2;
}
void putdata()
{ cout<<"\n The numbers are..";
cout<<num1<<"\t"<<num2; cout<<"\n The sum of the numbers are.. "<< sum;
}
};
void main()
{ clrscr();
add a, b (10, 20) , c(b);
a.getdata();
a.addition(b);
b = c.addition();
c.addition();
cout<<"\n Object a : ";
a.putdata();
cout<<"\n Object b : ";
b.putdata();
cout<<"\n Object c.. ";
c.putdata();
getch();
}
|
Topic archived. No new replies allowed.