-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendFunctionoftwoClass.cpp
100 lines (96 loc) · 2.48 KB
/
FriendFunctionoftwoClass.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//Adding the instant variable of to classes
#include<iostream>
using namespace std;
class Car2;
class Car1
{
private:
float Petrol, Distance;
public:
Car1()
{
cout<<"\t\tEnter the value of Petrol => ";
cin>>Petrol;
cout<<"\t\tEnter the value of Distance => ";
cin>>Distance;
}
Car1(float P, float D)
{
Petrol = P;
Distance = D;
}
Car1(Car1 &obj)
{
Petrol = obj.Petrol;
Distance = obj.Distance;
}
friend void Add(Car1 obj1, Car2 obj2);
~Car1()
{
}
};
class Car2
{
private:
float Petrol, Distance;
public:
Car2()
{
cout<<"\t\tEnter the value of Petrol => ";
cin>>Petrol;
cout<<"\t\tEnter the value of Distance => ";
cin>>Distance;
}
Car2(float P, float D)
{
Petrol = P;
Distance = D;
}
Car2(Car2 &obj)
{
Petrol = obj.Petrol;
Distance = obj.Distance;
}
friend void Add(Car1 obj1, Car2 obj2);
~Car2()
{
}
};
void Add(Car1 obj1, Car2 obj2)
{
cout<<"Petrol of car1 => "<<obj1.Petrol<<endl;
cout<<"Petrol of car2 => "<<obj2.Petrol<<endl;
cout<<"Distance of car1 => "<<obj1.Distance<<endl;
cout<<"Distance of car2 => "<<obj2.Distance<<endl;
cout<<"Total Petrol => "<< obj1.Petrol + obj2.Petrol<<endl;
cout<<"Total Distance => "<< obj1.Distance + obj2.Distance<<endl;
}
int main()
{
float P, D;
cout<<"Car1 objects Details:- "<<endl;
Car1 honda;
cout<<"\t\t*****************************"<<endl;
cout<<"\t\tEnter the value of Petrol => ";
cin>>P;
cout<<"\t\tEnter the value of Distance => ";
cin>>D;
Car1 audi(P,D), audi2(audi);
cout<<"***********************************************"<<endl;
cout<<"Car2 objects Details:- "<<endl;
Car2 civic;
cout<<"\t\t*****************************"<<endl;
cout<<"\t\tEnter the value of Petrol => ";
cin>>P;
cout<<"\t\tEnter the value of Distance => ";
cin>>D;
Car2 bmw(P,D), bmw2(bmw);
cout<<"********************************************"<<endl;
Add(honda, civic);
cout<<"***************************"<<endl;
Add(audi, bmw);
cout<<"***************************"<<endl;
Add(audi2, bmw2);
cout<<"***************************"<<endl;
return 0;
}