-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp1.cpp
52 lines (47 loc) · 1.19 KB
/
temp1.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
#include <iostream>
using namespace std;
class Engine
{
private:
string speed;
int model;
public:
Engine(string speed, int model) : speed(speed), model(model) {}
friend ostream &operator<<(ostream &out, const Engine &engine)
{
out << "The speed of the engine :" << engine.speed << "\n";
out << "The model of the engine :" << engine.model << "\n";
return out;
}
};
class Car
{
private:
const Engine &engine;
string name;
int price;
public:
Car(const Engine &engine, string name, int price) : engine(engine)
{
this->name = name;
this->price = price;
}
friend ostream &operator<<(ostream &out, const Car &car)
{
out << "The name of the car :" << car.name << "\n";
out << "The price of the car :" << car.price << "\n";
out << "The specifications of the engine :\n"
<< car.engine << "\n";
return out;
}
};
int main()
{
Engine engine("200Km/h", 2002);
{
Car car(engine, "Mehran", 200);
cout << car << "\n";
} // this object destroys out of block
cout << engine << "\n"; // but the engine object still remains
return 0;
}