-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriend.cpp
More file actions
42 lines (30 loc) · 1 KB
/
Friend.cpp
File metadata and controls
42 lines (30 loc) · 1 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
#include<iostream>
using namespace std;
class Test{
private:
int num1;
public:
Test(){
this->num1=10;
}
Test(int num1){
this->num1=num1;
}
void disp(); //member function of the class
friend void print(); //declaration of friend function inside the class
//it is non member fn bcoz it is defined outside th class
};
void Test::disp(){
cout<<"Inside disp(), Num1 = "<<this->num1<<endl;
}
void print(){ //friend fn can access private data members //no scope resolution required
Test tobj;
tobj.num1=600;
cout<<"\nInside print(), Num1 = "<<tobj.num1<<endl;
}
int main(){
Test tobj;
tobj.disp();
print(); //friend fn does not reqire object name to call [i.e. tobj.print()]
return 0;
}