-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCopy_Assignment_Operator.cpp
78 lines (65 loc) · 2.02 KB
/
Copy_Assignment_Operator.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
#include <bits/stdc++.h>
using namespace std;
/*
Copy Assignment Operator
Read comments in the code for questions and answers asked form this topic
*/
class Complex {
public:
double re_;
double im_;
public:
//constructor
Complex(double re, double im): re_(re), im_(im) {
cout << "ctor: ";
print();
}
Complex(const Complex& c): re_(c.re_), im_(c.im_) {
cout << "copy ctor: ";
print();
}
~Complex() {
cout << "dtor: ";
print();
}
Complex& operator=(const Complex& c) { //copy assignment operator
//Why pass by reference ?
//Because we don't want an unnecessary copy constructor to be called
//Why const ?
//Because, I don't want c to change
//Why returnin Complex& (object of same type ) ?
//To support Copy Assignment Chain
if(this != &c) { //we don't want a self copy by mistake
re_ = c.re_;
im_ = c.im_;
cout << "Copy Assignment: ";
print();
}
return *this;
}
double norm() {
return sqrt(re_*re_ + im_*im_);
}
void print() {
cout << "|" << re_ << "+j" << im_ << "| = " << norm() << endl;
}
};
int main() {
Complex c1(4.2, 5.3); //Constructor - Complex(double, double)
Complex c2(7.9, 8.5); //Constructor - Complex(double, double)
Complex c3(c2); //Copy Constructor - Complex(const Complex&)
//NOTE : c3 doesn't exist. While it's created, c2 is copied to it
c1.print();
c2.print();
c3.print();
c2 = c1; //Copy Assignment Operator (i.e. c2.operator=(c1))
//NOTE : c2 already exists and I want to take c1 and make c2 a copy of c1
c2.print();
c1 = c2 = c3; //Copy Assignment chain (c1 = (c2 = c3)), so, (c2 = c3) should be an expression and that
//should be such an expression which can be assigned to c1. Hence, We return Complex& from copy assignemnt
//operator above
c1.print();
c2.print();
c3.print();
return 0;
}