-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendclass.cpp
More file actions
65 lines (53 loc) · 1.7 KB
/
Copy pathFriendclass.cpp
File metadata and controls
65 lines (53 loc) · 1.7 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;
class Complex;// Forward declaration,telling compiler this class exists somewhere below in the program
class Calculator//a class for doing calculation
{
public:
int add(int a, int b)
{
return (a + b);
}
int sumRealComplex(Complex, Complex);//member function of Calculator
int sumImagComplex(Complex, Complex);//member function of Calculator
};
class Complex
{
int a, b;
// Individually declaring functions as friends
// friend int Calculator ::sumRealComplex(Complex, Complex);
// friend int Calculator ::sumCompComplex(Complex, Complex);
friend class Calculator;/* Aliter: Declaring the entire calculator class as friend,
meaning Complex class says complex class is friend of calculator class
all functions inside calculator class is friend of this complex class */
public:
void setNumber(int n1, int n2)//member function of Calculator
{
a = n1;
b = n2;
}
void printNumber()//member function of Calculator
{
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
int Calculator ::sumRealComplex(Complex o1, Complex o2)//we can use friend function of Calculator
{
return (o1.a + o2.a);
}
int Calculator ::sumImagComplex(Complex o1, Complex o2)//we can use friend function of Calculator
{
return (o1.b + o2.b);
}
int main()
{
Complex o1, o2;
o1.setNumber(1, 4);
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 resi = calc.sumImagComplex(o1, o2);
cout << "The sum of complex part of o1 and o2 is " << resi << endl;
return 0;
}