-
Notifications
You must be signed in to change notification settings - Fork 0
/
sixtyfive.cpp
70 lines (58 loc) · 1.46 KB
/
sixtyfive.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;
class complex;
// 1 + 4i
// 5 + 8i
// -------
// 6 + 12i
class calculator
{
private:
int a, b;
public:
int addtwonum(int a,int b){
return a + b;
}
};
class Complex
{
int a, b;
friend Complex sumComplex(Complex o1, Complex o2);
public:
void setNumber(int n1, int n2)
{
a = n1;
b = n2;
}
// Below line means that non member - sumComplex funtion is allowed to do anything with my private parts (members)
void printNumber()
{
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
// this funtion is not part of this class but this can access all data bcz declared in class
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, 4);
c1.printNumber();
c2.setNumber(5, 8);
c2.printNumber();
sum = sumComplex(c1, c2);
sum.printNumber();
return 0;
}
/* Properties of friend functions
1. Not in the scope of class
2. since it is not in the scope of the class, it cannot be called from the object of that class. c1.sumComplex() == Invalid
3. Can be invoked without the help of any object
4. Usually contans the objects as arguments
5. Can be declared inside public or private section of the class
6. It cannot access the members directly by their names and need object_name.member_name to access any member.
*/