-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut27.cpp
70 lines (54 loc) · 1.33 KB
/
tut27.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
#include <iostream>
using namespace std;
// forward declaration
class complex;
class calculator
{
public:
int add(int a, int b)
{
return (a + b);
}
int sumRealComplex(complex, complex);
int sumCompComplex(complex, complex);
};
class complex
{
int a, b;
// Individually declaring functions as friends
// friend int calculator ::sumRealComplex(complex, complex);
// friend int calculator ::sumCompComplex(complex, complex);
// Aliter: Declaring the entire calculator class as friend
friend class calculator;
public:
void setData(int v1, int v2)
{
a = v1;
b = v2;
}
void printNumber()
{
cout << "Your complex number is " << a << " + " << b << "i" << endl;
}
};
int calculator ::sumRealComplex(complex o1, complex o2)
{
return (o1.a + o2.a);
}
int calculator ::sumCompComplex(complex o1, complex o2)
{
return (o1.b + o2.b);
}
int main()
{
// Friend Classes and Member Friend functions
complex o1, o2;
o1.setData(3, 5);
o2.setData(2, 3);
calculator calc;
int res = calc.sumRealComplex(o1, o2);
int resc = calc.sumCompComplex(o1, o2);
cout << "Sum of real part of complex numbers o1 and o2 is: " << res << endl;
cout << "Sum of complex part of complex numbers o1 and o2 is: " << resc << endl;
return 0;
}