-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCopy_Constructor_II.cpp
87 lines (69 loc) · 2.36 KB
/
Copy_Constructor_II.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cmath>
using namespace std;
/*
Copy Constructor:
- call by value calls copy constructor and for this copy destructor will be called as well.
- If it had been passed by reference, then no copy constructor is called which means no new object is created and hence no destructor will be called.
Why the parameter to a copy constructor must be passed as Call-by-Reference ?
Ans: MyClass(MyClass other);
The above is an infinite loop as the call to copy constructor itself needs to make a copy for the
Call-by-Value mechanism.
*/
class Complex {
private:
double re_;
double im_;
public:
//constructor
Complex(double re , double im): re_(re), im_(im) {
cout << "Complex Constructor: ";
print();
}
//copy constructor
Complex(const Complex& c): re_(c.re_), im_(c.im_) {
cout << "Complex copy Constructor: ";
//print();
}
~Complex() {
cout << "Complex Destructor. Bye Bye !";
print();
}
double norm() {
return sqrt(re_*re_ + im_*im_);
}
void print() {
cout << "|" << re_ << "+j" << im_ << "| = " << norm() << endl;
}
void Display_Call_By_Value(Complex c_param) { //call by value
cout << "Display: " ;
c_param.print();
}
void Display_Call_By_Reference(Complex& c_param) { //call by reference
cout << "Display: " ;
c_param.print();
}
};
//Application
int main() {
Complex c(4.2, 5.3); //Constructor - Complex(double, double)
c.Display_Call_By_Value(c); //Copy constructor called to copy c to c_param
//since there is a copy of c that has been created while call to Display(),
//Hence, destructor will be called for c_param as well
/*Output :
Complex Constructor: |4.2+j5.3| = 6.7624
Complex copy Constructor: |4.2+j5.3| = 6.7624
Display: |4.2+j5.3| = 6.7624
Complex Destructor. Bye Bye !|4.2+j5.3| = 6.7624
Complex Destructor. Bye Bye !|4.2+j5.3| = 6.7624
*/
c.Display_Call_By_Reference(c);
/*Output :
Complex Constructor: |4.2+j5.3| = 6.7624
Complex copy Constructor: Display: |4.2+j5.3| = 6.7624
Complex Destructor. Bye Bye !|4.2+j5.3| = 6.7624
Display: |4.2+j5.3| = 6.7624
Complex Destructor. Bye Bye !|4.2+j5.3| = 6.7624
*/
return 0;
}