How to avoid multiple implementation of class functions


Keywords:c++ 


Question: 

I am new to C++ and hence need some help. Here is the code:

class InterfaceVehicle
{
  virtual int door() = 0;
  virtual int seats() = 0;
  virtual int enginepower() = 0; 
}
class minicar: public InterfaceVehicle
{
    int door () {return 4;}
    int seats () {return 2;}
    int enginepower () {return 10;}
}

class bigcar: public InterfaceVehicle
{
    int door () {return 4;}
    int seats () {return 5;}
    int engine () {return 20;}
}

class truck: public InterfaceVehicle
{
    int door () {return 2;}
    int seats () {return 1;}
    int engine () {return 50;}
}

I have an interface base class named Vehicle and three different classes that implements all the functions of the interface class. Here i want to avoid the same implementation of the door function. I got some suggestions to have a new class that implements this door function and this class will be aggregated into bigcar and minicar. This way i can avoid this copy paste implementation of door(); I like this idea!

Can some please give an example on how i can implement that? I read a lot about aggregation but did not understand how to implement that in my case.


2 Answers: 

I don't have the reputation to comment yet so I'm adding this as an answer instead.

Look at this stackoverflow answer: How to call a parent class function from derived class function?

You'll want to create a parent class that implements your interface and defines door but leaves the other two functions as virtual functions. Then inherit that class and define the rest of the functions in the child classes. You can then call the door function in the parent class from the child classes.



InterfaceVehicle is an interface because it has all its methods declared but not implemented, but to achieve your purpose the solution is build ** AbstractVehicle** i.e. an abstract class instead of an interface.
As an interface, an abstract class cannot be instantied, but it can provide implementantion for some common behaviour. For example, if Minicar and BigCar needs the same implementation of door() while Truck needs another one, you can specify the common behaviour in the base class as

class AbstractVehicle{   
        virtual int door() { return 4;} ;
        virtual int seats() = 0;
        virtual int enginepower() = 0; 
}

class Minicar: public virtual AbstractVehicle
{
    int seats () {return 2;}
    int enginepower () {return 10;}
}

class Bigcar: public virtual AbstractVehicle
{
    int seats () {return 5;}
    int engine () {return 20;}
}

class Truck: public virtual AbstractVehicle
{
    int door () {return 2;}
    int seats () {return 1;}
    int engine () {return 50;}
}

In this way, subclasses don't have to respect a contract for method door(), they can ovverride it (in the case of Truck that needs another implementantion), but they are not forced to implement that, as they must do for pure abstract methods like seats() and enginepower().