Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (104 loc) · 3.51 KB
/
Copy pathmain.cpp
File metadata and controls
123 lines (104 loc) · 3.51 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>
// Base Class
class Vehicle
{
protected:
std::string brand;
int speed;
public:
Vehicle(const std::string &brand, int speed) : brand(brand), speed(speed) {}
void start()
{
std::cout << brand << " vehicle started." << std::endl;
}
void stop()
{
std::cout << brand << " vehicle stopped." << std::endl;
}
virtual void display() const = 0; // Pure virtual function
};
// Custom class
class MotorCycle : public Vehicle
{
private:
String brand;
public:
// Set constructor
};
// Derived Class: Car
class Car : public Vehicle
{
private:
int doors;
public:
/*
Notes about constructor
- Initialization list
- The part after the colon :
- Used to initialize base class members and data members before the body of the constructor is executed
- Vehicle(brand, speed)
- Calls the constructor of the base class 'Vehicle'
- Passes 'brand'/'speed' parameters to it
- This is done to ensure the base class part of `Car` object is properly initialized
- doors(doors)
- Initializes 'doors' member of the 'Car' class with the value passed to the constructor
- Base class initialization
- When having a derived class like `car`
- The base class `Vehicle` constructor is called BEFORe the derived class's constructor body is executed
- The Vehicle constructor is called with brand and speed first.
- After the Vehicle constructor completes,
- The Car constructor continues with the initialization of doors.
- Ensures that all parts of the object are fully initialized before any other operations are performed.
*/
Car(const std::string &brand, int speed, int doors)
: Vehicle(brand, speed), doors(doors) {}
void honk() const
{
std::cout << brand << " car honks: Beep Beep!" << std::endl;
}
// const = so the state doesn't change
// override = ensures display() method is correctly overriding the base class's virtual method
// It's used to indicate that a member function in a derived class is to override a virtual function in the base class
// This helps the compiler check if you're correctly overriding a base class method, catching errors if the base method's signature doesn't match
void display() const override
{
std::cout << "Car Brand: " << brand << std::endl;
std::cout << "Speed: " << speed << " km/h" << std::endl;
std::cout << "Doors: " << doors << std::endl;
}
};
// Derived Class: Bike
class Bike : public Vehicle
{
private:
bool hasBasket;
public:
Bike(const std::string &brand, int speed, bool hasBasket)
: Vehicle(brand, speed), hasBasket(hasBasket) {}
void ringBell() const
{
std::cout << brand << " bike rings bell: Ring Ring!" << std::endl;
}
void display() const override
{
std::cout << "Bike Brand: " << brand << std::endl;
std::cout << "Speed: " << speed << " km/h" << std::endl;
std::cout << "Has Basket: " << (hasBasket ? "Yes" : "No") << std::endl;
}
};
// Main Function
int main()
{
Car myCar("Toyota", 180, 4);
Bike myBike("Giant", 25, true);
myCar.start();
myCar.honk();
myCar.display();
myCar.stop();
myBike.start();
myBike.ringBell();
myBike.display();
myBike.stop();
return 0;
}