-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfriendClass.cpp
53 lines (35 loc) · 938 Bytes
/
friendClass.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
//friend class
#include<iostream>
#include<string>
using namespace std;
void display(); //function prototype
class Human {
private: //private attr of the class
int age;
int salary;
string name;
public:
Human(int iage,int isal,string iname) {
age=iage;
salary=isal;
name=iname;
}
void tell(){
cout<<age<<salary<<endl<<name<<endl;
}
friend class man; //friend class of class Human which can access the private and protected attr and methods of the class Human
};
class man{
public:
//now the member function of friend class will take the argument as the object of class Human
void display(Human *anish) {
cout<<"name: " <<anish->name<<","<<"salary : "<<anish->salary<<", "<<"age:"<<anish->age<<endl;
}
};
//friend function defination
int main() {
Human *obj=new Human(20,2004444,"Anish Singh Walia");
man *anish=new man;//object of class man
anish->display(obj);
return 0;
}