Inheritance in C++ is one of the feature of OOPS
In the below code, three classes have created Car PlayerCar and PoliceCar
I don't understand the concept of inheritance and Why do we need inheritance in c++?
In the below code, three classes have created Car PlayerCar and PoliceCar
Code:
class Car {
protected:
int color;
int currentSpeed;
int maxSpeed;
public:
void applyHandBrake(){
this->currentSpeed = 0;
}
void pressHorn(){
cout << "Teeeeeeeeeeeeent"; // funny noise for a horn
}
void driveAtFullSpeed(int mph){
// code for moving the car ahead;
}
};
class PlayerCar : public Car {
};
class PoliceCar : public Car {
private:
bool sirenOn; // identifies whether the siren is on or not
bool inAction; // identifies whether the police is in action (following the player) or not
public:
bool isInAction(){
return this->inAction;
}
};
Code:
class Car {
protected:
int color;
int currentSpeed;
int maxSpeed;
public:
void applyHandBrake(){
this->currentSpeed = 0;
}
void pressHorn(){
cout << "Teeeeeeeeeeeeent"; // funny noise for a horn
}
void driveAtFullSpeed(int mph){
// code for moving the car ahead;
}
};
class PlayerCar : public Car {
};
class PoliceCar : public Car {
private:
bool sirenOn; // identifies whether the siren is on or not
bool inAction; // identifies whether the police is in action (following the player) or not
public:
bool isInAction(){
return this->inAction;
}
};