-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut12.cpp
40 lines (32 loc) · 808 Bytes
/
tut12.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
#include <iostream>
using namespace std;
// making friend function to have private variables of complex class
class Complex
{
int a, b;
public:
// sumComplex will be non-member of complex but it can have private variables because it is a friend now;
friend Complex sumComplex(Complex o1, Complex o2);
void sum(int v1, int v2)
{
a = v1;
b = v2;
}
void printNumbers(){
cout << a << " " << b;
}
};
Complex sumComplex(Complex o1, Complex o2){
Complex o3;
o3.sum((o1.a+ o2.a) , (o1.b, o2.b));
return o3;
}
int main()
{
Complex c1, c2, sum;
c1.sum(1, 2);
c2.sum(3, 4);
sum = sumComplex(c1, c2); // <-- we cannot do sum.sumComplex, because sumComplex is just a friend , not a member
sum.printNumbers();
return 0;
}