-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24friends_function.cpp
More file actions
48 lines (39 loc) · 1.16 KB
/
Copy path24friends_function.cpp
File metadata and controls
48 lines (39 loc) · 1.16 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
#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 number 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,4);
c1.printNumber();
c2.setNumber(5,8);
c2.printNumber();
sum = sumcomplex(c1,c2);
sum.printNumber();
return 0;
}
/* properties of friend fucntions
1. not in the scope of class i.e. its just give permission to access the private data
2. since it is not in the scope of the class, it cannot be called from the object of that class.
c1.sumcomplex() is not possible
3. can be invoked without the help of any object
4. usually contains the object as the argument
5. can be declared in the public or private section of class
6. it cannot excess the member directly by the names and need obejct_name.member_name
to access any member.
*/