-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriend.cpp
More file actions
40 lines (36 loc) · 823 Bytes
/
friend.cpp
File metadata and controls
40 lines (36 loc) · 823 Bytes
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;
class complex
{
int a, b;
public:
void setnumber(int n1, int n2)
{
a = n1;
b = n2;
}
friend complex sumcomplex(complex o1, complex o2);
void printnumber()
{
cout << "your name is " << a << " + " << b << "i" << endl;
}
};
complex sumcomplex(complex o1, complex o2)
{
complex o3;
o3.setnumber((o1.a + o2.a), (o1.b + o2.b));
return o3;
}
int main()
{
complex c1, c2, sum;
c1.setnumber(1, 9);
c1.printnumber();
c2.setnumber(7, 8);
c2.printnumber();
sum = sumcomplex(c1, c2);
sum.printnumber();
return 0;
}
// here sum complex is written outside class so it cannot access,
// its data in class so we need to assign it friend to use its values.