-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMAIN.CPP
56 lines (47 loc) · 1.18 KB
/
MAIN.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
// Balagurusamy Ch: 6, 151 Page, Overloaded Constructors
#include<iostream>
using namespace std;
class complex
{
float x,y;
public:
complex(){} // constructor no arg
complex(float a){x = y = a;} // constructor-one arg
complex(float real, float imag) // constructor-two args
{
x = real; y = imag;
}
friend complex sum(complex, complex);
friend void show(complex);
};
complex sum(complex c1, complex c2) // friend
{
complex c3;
c3.x = c1.x + c2.y;
c3.y = c1.y + c2.y;
return(c3);
}
void show(complex c) // friend
{
cout << c.x << " + j" << c.y << "\n";
}
int main()
{
complex A(2.7, 3.5); // define & initialize
complex B(1.6); // define & initialize
complex C; // define
C = sum(A, B); // sum() is a friend
cout << "A = "; show(A);
cout << "B = "; show(B);
cout << "C = "; show(C);
//Another way to give initial values (second method)
complex P,Q,R; // define P,Q,R
P = complex(2.5,3.9); // initialize P
Q = complex(1.6,2.5); // initialize Q
R = sum(P,Q);
cout << "\n";
cout << "P = "; show(P);
cout << "Q = "; show(Q);
cout << "R = "; show(R);
return 0;
}