-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_overload_assignment.cpp
85 lines (69 loc) · 1.6 KB
/
15_overload_assignment.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
// // Create a complex class and overload assignment operator for that class.
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define class Complex
class Complex
{
private:
// // instance member variables
double real;
double imag;
public:
// // constructors
Complex()
{
real = imag = 0;
}
Complex(double r)
{
real = imag = r;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
// // instance member function to set compelx number
void setData(double r, double i)
{
real = r;
imag = i;
}
// // instance member function to display compelx number
void showData()
{
cout << "\n"
<< real << " + " << imag << "i" << endl;
}
// // overload assignment (=) operator
Complex operator=(Complex c)
{
real = c.real;
imag = c.imag;
return c;
}
};
// // Main Function Start
int main()
{
double real, imag;
// // Get complex
cout << "\n>>>>>>>> Enter A Complex Number <<<<<<<<<\n";
cout << "\nEnter Real Part => ";
cin >> real;
cout << "\nEnter Imaginary Part => ";
cin >> imag;
Complex c1(real, imag), c2; // create objects of Complex
c2 = c1; // assign c1 to c2
// // display first complex number
cout << "\n>>>>>>>> First Complex Number <<<<<<<<<\n";
c1.showData();
cout << "\n>>>>>>>> Copy of Complex Number <<<<<<<<<\n";
c2.showData();
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End