Home > Mobile >  Constructor in double inheritance
Constructor in double inheritance

Time:12-28

I have a problem with my constructor. I have class vehicle, then I made class motorVehicle which inherited after vehicle and then I want to make class motorcycle which inherits after class motorVehicle and I can't make my default constructor because I have error: Class vehicle and motorVehicle isn't changed by me and class motorcycle is in 2 option none of these option works but I give you both of them. Btw the problems are (First option): no matching constructor for initialization of 'motorVehicle' and with second option expected ; after expression and expected member name or ; after declaration specifiers

class vehicle {
public:
    int numberOfWheels;
    string color;
    float vehiclePayload;

    vehicle(): numberOfWheels{4},color{"black"},vehiclePayload{500}{

    }
};
class motorVehicle : public vehicle {
public:
    float tankCapacity;
        float fuelConsumption;
        float mileage;
        float currentAmountOfFuel;
        int yearOfProduction;
        unsigned long long int vin;
        motorVehicle(): tankCapacity{30}, fuelConsumption{6}, mileage{100}, currentAmountOfFuel{10}, yearOfProduction{2021}, vin{1}, vehicle{4, "black", 500} {

        }
};


class motorcycle : public motorVehicle{
public:
    float bootSize;
    string brand;
    string model;
    motorcycle(): bootSize{500}, brand{"Ninja"}, model{"Kawasaki"}, motorVehicle{30,6,100,10,2021,1,vehicle{4, "black", 500}}{

    }

};

class motorcycle : public motorVehicle{
public:
    float bootSize;
    string brand;
    string model;
    motorcycle(): bootSize{500}, brand{"Ninja"}, model{"Kawasaki"}, motorVehicle(){30,6,100,10,2021,1,vehicle{4, "black", 500}}{

    }

};

CodePudding user response:

Your base classes do not declare any constructor taking actual values. As these classes do declare a default constructor, aggregate initialisation doesn't work: there is a declared constructor, i.e., the class isn't an aggregate. If you want to directly initialise the base class you could use their implicitly generate copy constructor, though. However, you'd still need to populate the content of the class separately as you'll need to created the object using a default constructor:

class motorVehicle : public vehicle {
public:
    float tankCapacity;
    float fuelConsumption;
    float mileage;
    float currentAmountOfFuel;
    int yearOfProduction;
    unsigned long long int vin;
    motorVehicle(): tankCapacity{30}, fuelConsumption{6}, mileage{100}, currentAmountOfFuel{10}, yearOfProduction{2021}, vin{1},
        vehicle([]{
            vehicle rc;
            rc.numberOfWheels = 4;
            rc.color = "black";
            rc vehiclePayload = 500;
            return rc;
            }()) {

    }
 };

Obviously, instead of using a lambda function you could have a factor function, let's say makeVehicle(int wheels, std::string const& color, float payload) appropriately initialising the members. There should really be a suitable constructor for vehicle, though.

  • Related