Copy constructors

I'm confused how each object calls the member function. can anyone give me step by step working of this code
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>

using namespace std;

class add 
{
	int num1, num2, sum;
	public:
	add()
	{
		 cout<<" constructor without parameters ";
		 num1 = '\0';
		 num2='\0';
		 sum='\0';
		 
	}
	add(int s1, int s2)
	{
		cout<<"\n constructor with parameters";
		num1= s1;
		num2= s2;
		sum= '\0';
		
	}
	add(add &a)
	{
		cout<<"\ncopy const";
		num1= a.num1;
		num2 = a.num2;
		sum= '\0';
		
	}
	
	void getdata()
	{
		cout<<"\nenter 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<<"\nthe  numbers are";
		cout<<num1<<'\t'<< num2 ;
		cout<< "\nthe sum of the numbers are"<<sum;
		
	}
};

int main()
{
	add a,b(10,20), c(b);
	a.getdata();
	a.addition();
	b=c.addition();
	c.addition();
	cout<<"\nobj a";
	a.putdata();
	cout<<"\n obj b ";
	b.putdata();
	cout<<"\n obj c ";  
	c.putdata();
	
}


p.s the output of this program is absurd but its main motive is to understand where the copy constructor has been invoked.
also can you explain what is being done in this function block

1
2
3
4
5
6
7
8
add(add &a)
	{
		cout<<"\ncopy const";
		num1= a.num1;
		num2 = a.num2;
		sum= '\0';
		
	}

thanks in advance.
Last edited on
Topic archived. No new replies allowed.