File tree 6 files changed +161
-0
lines changed
6 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include " Hatchback.h"
2
+
3
+ int Hatchback::vehicleTrunkSize ()
4
+ {
5
+ std::cout << " Hatchback trunk size: " << trunk << std::endl;
6
+ return trunk;
7
+ }
8
+
9
+ double Hatchback::vehiclePrice ()
10
+ {
11
+ std::cout << " Hatchback price: " << price << std::endl;
12
+ return price;
13
+ }
14
+
15
+ int Hatchback::vehicleNumberOfDoors ()
16
+ {
17
+ std::cout << " Hatchback door count: " << doors << std::endl;
18
+ return doors;
19
+ }
20
+
21
+ bool Hatchback::vehicleEngineIsOn ()
22
+ {
23
+ if (engine == true )
24
+ {
25
+ std::cout << " Hatchback engine is on!" << std::endl;
26
+ }
27
+ else
28
+ {
29
+ std::cout << " Hatchback engine is off!" << std::endl;
30
+ }
31
+ return engine;
32
+ }
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include " Vehicle.h"
3
+
4
+ class Hatchback : public Vehicle
5
+ {
6
+ int doors = 5 ;
7
+ double price = 2000 ;
8
+ bool engine = true ;
9
+ int trunk = 30 ;
10
+
11
+ public:
12
+ int vehicleNumberOfDoors ();
13
+ double vehiclePrice ();
14
+ bool vehicleEngineIsOn ();
15
+ int vehicleTrunkSize ();
16
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include " Sedan.h"
3
+ #include " Hatchback.h"
4
+
5
+ // Sedan salesperson
6
+ void sedanSalesperson (Vehicle* tWhat)
7
+ {
8
+ tWhat->vehicleNumberOfDoors ();
9
+ tWhat->vehicleTrunkSize ();
10
+ tWhat->vehicleEngineIsOn ();
11
+ }
12
+
13
+ // Sedan accountant
14
+ void sedanAccountant (Vehicle* tWhat)
15
+ {
16
+ tWhat->vehiclePrice ();
17
+ }
18
+
19
+ // Hatchback salesperson
20
+ void hatchbackSalesperson (Vehicle* tWhat)
21
+ {
22
+ tWhat->vehicleNumberOfDoors ();
23
+ tWhat->vehicleTrunkSize ();
24
+ tWhat->vehicleEngineIsOn ();
25
+ }
26
+
27
+ // Hatchback accountant
28
+ void hatchbackAccountant (Vehicle* tWhat)
29
+ {
30
+ tWhat->vehiclePrice ();
31
+ }
32
+
33
+ int main ()
34
+ {
35
+ Hatchback* tHatchback = new Hatchback;
36
+ Sedan* tSedan = new Sedan;
37
+
38
+ hatchbackSalesperson (tHatchback);
39
+ hatchbackAccountant (tHatchback);
40
+ std::cout << " -----------------------------" << std::endl;
41
+ sedanSalesperson (tSedan);
42
+ sedanAccountant (tSedan);
43
+
44
+
45
+ delete tHatchback;
46
+ delete tSedan;
47
+ }
Original file line number Diff line number Diff line change
1
+ #include " Sedan.h"
2
+
3
+ int Sedan::vehicleTrunkSize ()
4
+ {
5
+ std::cout << " Sedan trunk size: " << trunk << std::endl;
6
+ return trunk;
7
+ }
8
+
9
+ double Sedan::vehiclePrice ()
10
+ {
11
+ std::cout << " Sedan price: " << price << std::endl;
12
+ return price;
13
+ }
14
+
15
+ int Sedan::vehicleNumberOfDoors ()
16
+ {
17
+ std::cout << " Sedan door count: " << doors << std::endl;
18
+ return doors;
19
+ }
20
+
21
+ bool Sedan::vehicleEngineIsOn ()
22
+ {
23
+ if (engine == true )
24
+ {
25
+ std::cout << " Sedan engine is on!" << std::endl;
26
+ }
27
+ else
28
+ {
29
+ std::cout << " Sedan engine is off!" << std::endl;
30
+ }
31
+ return engine;
32
+ }
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include " Vehicle.h"
3
+
4
+ class Sedan : public Vehicle
5
+ {
6
+ int doors = 4 ;
7
+ double price = 1000 ;
8
+ bool engine = true ;
9
+ int trunk = 20 ;
10
+
11
+ public:
12
+ int vehicleNumberOfDoors ();
13
+ double vehiclePrice ();
14
+ bool vehicleEngineIsOn ();
15
+ int vehicleTrunkSize ();
16
+ };
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include < iostream>
3
+
4
+ class Vehicle
5
+ {
6
+ public:
7
+ // Sedan and Hatchback both have a price.
8
+ virtual double vehiclePrice () = 0;
9
+
10
+ // Sedan and Hatchback both have doors.
11
+ virtual int vehicleNumberOfDoors () = 0;
12
+
13
+ // Sedan and Hatchback both have a trunk.
14
+ virtual int vehicleTrunkSize () = 0;
15
+
16
+ // Sedan and Hatchback can be started.
17
+ virtual bool vehicleEngineIsOn () = 0;
18
+ };
You can’t perform that action at this time.
0 commit comments