-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriend2.cpp
More file actions
56 lines (41 loc) · 841 Bytes
/
Friend2.cpp
File metadata and controls
56 lines (41 loc) · 841 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
class B; //Forward declaration of class B
class A{
private:
int num1;
public:
A(){
this->num1=10;
}
A(int num1){
this->num1=num1;
}
friend void sum(A aobj, B bobj);
};
class B
{
private:
int num2;
public:
B(){
this->num2=20;
}
B(int num2){
this->num2=num2;
}
friend void sum(A aobj, B bobj);
};
void sum(A aobj, B bobj){ //friend fn defined outside the class //friend keyword not required
int res=aobj.num1+bobj.num2;
cout<<"Result = "<<res<<endl;
}
int main(){
A a1(200);
B b1(150);
sum(a1,b1);
A a2;
B b2;
sum(a2,b2);
return 0;
}