vendredi 15 mai 2020

Fix a large class using inheritance

It seems to me that my class is too big and complicated, I would like to reduce it. Can I use inheritance in this way, given that I created the InitCar class only to inherit it and am not going to use objects of this class explicitly.

Before refactoring. People and License are not my own classes, I cannot change them.

class Car
{
public:
    void Move();
    void SpeedUp();
    void SpeedDw();
    //More other

private:
    int speed = 0;
    std::string name;
    int id = 0;
    People owner;    // not my own class
    License license; // not my own class

    void InitCarFromConfig()
    {
        //Here I read the data from the file
    }

    void InitOwner()
    {
        //Here I init the People owner
    }

    void InitInspection()3
    {
        //Here I init the License license
    }


};

After refactoring

class InitCar
{
protected:
    std::string name;
    int id = 0;
    People owner;    // not my own class
    License license; // not my own class

    void InitCarFromConfig()
    {
        //Here I read the data from the file
    }

    void InitOwner()
    {
        //Here I init the People owner
    }

    void InitInspection()
    {
        //Here I init the License license
    }
};

class Car : InitCar
{
public:
    void Move()
    {
        InitOwner();
    }
    void SpeedUp();
    void SpeedDw();
    //More other
private:
    int speed = 0;
};

Is this use of inheritance acceptable and are performance issues possible?

Aucun commentaire:

Enregistrer un commentaire