Why do we need inheritance in c++?

Thread Starter

John99407

Joined Jul 12, 2019
77
Inheritance in C++ is one of the feature of OOPS

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;
         }
};
I don't understand the concept of inheritance and Why do we need inheritance in c++?
 

Ian Rogers

Joined Dec 12, 2012
1,136
Think of it as a schematic... You can create a copy, add more circuits, draw all over it, screw it up and bin it, but the original is untouched.

So you are using all the components of the object and adding your finer details, without re-writng a whole new class.
 

402DF855

Joined Feb 9, 2013
271
In reality "objects" are defined by their similarities and differences with other objects. Turns out this provides a very effect method of software design where the code elements model the objects.
C:
class Item;
class Vehicle : Item;
class WaterVehicle : Vehicle;
class Boat : WaterVehicle;
class LandVehicle : Vehicle;
class Truck : LandVehicle;
class Car : LandVehicle;
class SportsCar : Car;
class PoliceCar : Car;

class Person : Item;
class Police : Person;
class Thug : Person;

Let us say each Vehicle has a driver;

class Vehicle : Item {
    protected:
        Person *driver;
   public:
       void SetDriver(Person *person) {driver=person;}
       CPerson *GetDriver() {return driver;}
}

PoliceCar pcar;
Officer bob;
SportsCar ferrari;
Thug john;

pcar.SetDriver( &bob );
ferrari.SetDriver( &john );

pcar.Chase(ferrari).
One of the advantages of this type of design is that objects and their characteristics are more static than the actions one takes with them. In practice this means that as software features/requirements evolve (inevitable) the impact on the code is lessened which implies less effort to cope over the lifetime of the project(s).
 

Ian Rogers

Joined Dec 12, 2012
1,136
@402DF855 Remember most newbie programmers will read that and run for the hills... Try to explain as if explaining to a child ( not that I'm calling the OP a child).

Inheritance is simply extending an object.... basic car can be extended to another style.

Basic Car:
4 wheels
engine
brakes
steering wheel

When creating a police car all you need is some optional extras.. faster engine, alarms and emergency lights.

So Police car = basic car + optional extra's

Police car will inherit basic cars internal functions saving you loads of time creating your own..
 

WBahn

Joined Mar 31, 2012
30,062
Inheritance in C++ is one of the feature of OOPS

I don't understand the concept of inheritance and Why do we need inheritance in c++?
It's not a case of "need", it's a case of usefulness.

One of the most useful things to a programmer of serious code (i.e., "real" applications that are enormously more complex than any homework or semester project programming assignment) is the ability to reuse previously written code. Object-oriented programming and features such as inheritance are really little more than formalisms to make this reuse easier and less error prone.

Using your example of cars, there are many different kinds of cars that a particular application, such as a game, might have. But all of those cars have (or can be made to have) most of their features in common. So say one of the features of a car is the amount of fuel it has. There are going to be a set of tasks that we might want to do with that amount of fuel. We need to be able to check how much fuel we have right now. We need to be able to use some of that fuel as we drive. We need to be able to add fuel when we stop for gas. So if we make a 'Car' class that has methods to do all of these things, then when we start making specialized cars if we inherit from the Car class we start off with the ability to do all of those things and we can focus on adding just those features that our new specialized type of car, say PoliceCar, has such as a siren and the methods to turn it on or off.
 

402DF855

Joined Feb 9, 2013
271
@402DF855 Remember most newbie programmers will read that and run for the hills... Try to explain as if explaining to a child ( not that I'm calling the OP a child).
And referencing schematics to explain inheritance won't?
I imagine making a name for yourself in academia is a challenge and might take one down some strange paths.
But making Square a subtype of Rectangle puts two methods for mutating length and height on our squares, which is a bit weird.
Uh, no it is not weird at all.
class Rectangle extends Square {
Why would he even go there? It suggests he is simply bewildered by the basic principles of OOD.
 

Ian Rogers

Joined Dec 12, 2012
1,136
And referencing schematics to explain inheritance won't?
It wasn't a dig, I just try and dumb it down a tad.. I remember when I first came across the 4 laws of oop… I think the hole in the wall where the book hit it is still there... Most books, and programmers, trying the explanation route use "simple to us" remarks that just isn't useful when you are learning.

TWINDOW twindow = new TWINDOW(twindow);

Sometimes the learned run away with themselves when explaining...
 
Top