-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendFuncsAndFriendClasses.cpp
56 lines (42 loc) · 1.28 KB
/
friendFuncsAndFriendClasses.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
#include <iostream>
using namespace std;
//we must to declare a prototipe of MotorDriver class.
//Because we used it in the Sensor class.
//we used it before we defined it.
class MotorDriver;
class Sensor{
private:
int x;
public:
int getX();
//void setX(int);
friend void setDistance(Sensor&);
void setY(MotorDriver&);
};
class MotorDriver{
private:
int y;
public:
int getY();
//void setY(int);
friend class Sensor;
};
int Sensor::getX(){return x;}
//void Sensor::setX(int _x){x = _x;}
void Sensor::setY(MotorDriver& _y){_y.y = 100;}
int MotorDriver::getY(){return y;}
//void MotorDriver::setY(int _y){y = _y;}
void setDistance(Sensor& _x){_x.x = 0;} // this function is not a member of Sensor class
int main() {
Sensor mySens;
//mySens.setX(61);
cout<<"output of getx() from sensor class: "<<mySens.getX()<<endl;
setDistance(mySens);
cout<<"output after the setDistance() function: "<<mySens.getX()<<endl;
MotorDriver motor;
//motor.setY(50);
cout<<"output of gety() from motor class: "<<motor.getY()<<endl;
mySens.setY(motor);
cout<<"output after the sensor class' setY() function"<<motor.getY()<<endl;
return 0;
}