-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25member_friend_function.cpp
More file actions
65 lines (54 loc) · 1.4 KB
/
Copy path25member_friend_function.cpp
File metadata and controls
65 lines (54 loc) · 1.4 KB
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
#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); //here o1 o2 are not declared yet and will be declared
//later and thus cant be mentioned right now
int sumcompcomplex(complex, complex);
};
class complex
{
int a, b;
// individually declaring functions as friends
// friend int calculator::sumRealComplex(complex o1, complex o2);
// friend int calculator::sumcompcomplex(complex o1, complex o2);
//alternate method: declaring the entire calculator as friend
friend class calculator;
public:
void setNumber(int n1, int n2)
{
a = n1;
b = n2;
}
void printNumber()
{
cout << "your 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()
{
complex o1, o2;
o1.setNumber(1, 3);
o2.setNumber(5, 7);
calculator calc;
int res = calc.sumRealComplex(o1, o2);
cout << "the sum of real part of o1 and o2 is " << res << endl;
int resc = calc.sumcompcomplex(o1, o2);
cout<<"the sum of the complex part of o1 o2 is "<<resc<<endl;
return 0;
}