-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendClassesAndMemberFriendFunctions.cpp
70 lines (56 loc) · 1.45 KB
/
FriendClassesAndMemberFriendFunctions.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 sumReal(Complex, Complex);
int sumImag(Complex, Complex);
};
class Complex
{
int a, b;
// How other functions or functions under other classes can access the private data member oof this class?
// Method 1: Individally declaring fucntion as friends (better when 1 or 2 functions)
// friend int Calculator::sumReal(Complex c1, Complex c2);
// friend int Calculator::sumImag(Complex c1, Complex c2);
// Method 2: Declare the whole class as friend (better when big number of functions)
friend class Calculator;
public:
void setdata(int n1, int n2)
{
a = n1;
b = n2;
}
void PrintNumber()
{
cout << "The complex no is: " << a << " + " << b << "i" << endl;
}
};
int Calculator::sumReal(Complex c1, Complex c2)
{
return c1.a + c2.a;
}
int Calculator::sumImag(Complex c1, Complex c2)
{
return c1.b + c2.b;
}
int main()
{
Complex c1, c2;
c1.setdata(1, 5);
c1.PrintNumber();
c2.setdata(4, 5);
c2.PrintNumber();
Calculator cal;
int resultReal = cal.sumReal(c1, c2);
cout << "The sum of real part of Complex Numbers is: " << resultReal << endl;
int resultImag = cal.sumImag(c1, c2);
cout << "The sum of Imaginary part of Complex Numbers is: " << resultImag << endl;
return 0;
}