-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession11A.cpp
More file actions
98 lines (70 loc) · 1.71 KB
/
Session11A.cpp
File metadata and controls
98 lines (70 loc) · 1.71 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
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
#include<iostream>
#include<string>
using namespace std;
// Classes having a Friend Function
// Friend Function as a Bridge between two classes
class Vehicle; // Declare the Class
class Driver{
// Attributes
string name;
int age;
public:
// Constructor For Initialization of Data in Object
Driver(){
name = "NA";
age = 0;
}
Driver(string name, int age){
this->name = name;
this->age = age;
}
// Write Data in Object
void setDriverData(string name, int age){
this->name = name;
this->age = age;
}
void showDriverDetails(){
cout<<"Driver "<<name<<" is "<<age<<" years old"<<endl;
}
// friend function can only be in public/private or protected section
friend void show(Driver d, Vehicle v);
};
class Vehicle{
// Attributes
string regNumber;
string model;
int engine;
friend void show(Driver d, Vehicle v);
public:
// Constructor For Initialization of Data in Object
Vehicle(){
regNumber = "NA";
model = "NA";
engine = 0;
}
// Write Data in Object
void setVehicleData(string regNumber, string model, int engine){
this->regNumber = regNumber;
this->model = model;
this->engine = engine;
}
void showVehicleDetails(){
cout<<"Vehicle "<<model<<" has a Registration Number "<<regNumber<<" with "<<engine<<" cc engine"<<endl;
}
};
void show(Driver d, Vehicle v){
cout<<"Driver Name: "<<d.name<<endl;
cout<<"Vehicle Reg Num: "<<v.regNumber<<endl;
}
int main(){
Driver d1("John", 32);
Vehicle v1;
v1.setVehicleData("PB10AA3333","Honda City", 1499);
d1.showDriverDetails();
v1.showVehicleDetails();
// error : Since we cannot access private data
//cout<<"Driver Name: "<<d1.name<<endl;
//cout<<"Vehicle Reg Num: "<<v1.regNumber<<endl;
show(d1, v1);
return 0;
}