0

Hey guys. I dont know how i suppose to do about setting values to derived classes ctor. And here is my code. I remember something like that build in my class. But i know that we can write something like that build ( xxx():yyy(){,,,};). Check the main's second object.

    #include <iostream>

using namespace std;

class vehicle
{
protected:
    string brand;
    int wheelNumber;
    double maxSpeed=0;
public:
    vehicle(){cout<<"default ctor for vehicle"<<endl;}
    vehicle(string br1, int wn1, double ms1)
    {brand=br1; wheelNumber=wn1; maxSpeed=ms1;}

    void setbrand(string br){brand=br;}
    string getbrand(string br){return brand;}
    void setWN(int wn){wheelNumber=wn;}
    int getWN(int wn){return wheelNumber;}
    void setMaxS(double ms){maxSpeed=ms;}
    double getMaxS(double ms){return maxSpeed;}
    ~vehicle(){cout<<"dtor for vehicle."<<endl;}
};
class car: public vehicle
{
private:
    int numberOfDoors;
    string fuelType;
public:
    car(){cout<<"default ctor for car"<<endl;}
    car(int nOD,string fT){numberOfDoors=nOD; fuelType=fT;}

    void setnOD(int nOD){numberOfDoors=nOD;}
    int getnOD(int nOD){return numberOfDoors;}
    void setfT(string fT){fuelType=fT;}
    string getfT(string fT){return fuelType;}

    void printFeatures()
    {
        cout<<endl;
        cout<<"brand:"<<brand<<endl;
        cout<<"wheelNumber:"<<wheelNumber<<endl;
        cout<<"MaxSpeed:"<<maxSpeed<<endl;
        cout<<"NumberOfDoors:"<<numberOfDoors<<endl;
        cout<<"FuelType:"<<fuelType<<endl<<endl;
    }
    ~car(){cout<<"dtor for car."<<endl;}
};

int main()
{
    car *cc;
    cc= new car;
    cc->setbrand("bmw");
    cc->setfT("diesel");
    cc->setMaxS(333.25);
    cc->setWN(4);
    cc->setnOD(6);
    cc->printFeatures();
    delete cc;

    car *xx;
    xx= new car;
    car(5,"gasoline"):vehicle("mercedes",4,489.12);//Part that i cant figure it out.
    xx->printFeatures();
    delete xx;
}

3 Answers3

1

The ctor(...):base(...){} syntax is used when defining the derived class constructor, not when instantiating an object. So change the constructor to car to this:

car(int nOD,string fT, string br1, int wn1, double ms1): vehicle(br1, wn1, ms1) {
    numberOfDoors=nOD; fuelType=fT;
}

And instantiate your car object like this:

xx= new car(5,"gasoline", "mercedes",4,489.12);

Even better, take advantage of C++'s member-initialization syntax to define your constructors:

vehicle(string br1, int wn1, double ms1)
:brand(br1), wheelNumber(wn1), maxSpeed(ms1)
{}

// ...

car(int nOD,string fT, string br1, int wn1, double ms1)
: vehicle(br1,wn1, ms1), numberOfDoors(nOD), fuelType(fT)
{}
user3553031
  • 5,990
  • 1
  • 20
  • 40
1

You need to code this into the constructor to get this to work. When you derive from a class the constructor of that derived class needs to take in the parameters for the base constructor and its own construction.

class Base
{
    int foo;
public:
    Base(int f) : foo(f) {};
};

class Derived : public Base
{
    int bar;
public:
    Derivced(int f, int b) : Base(f),                  bar(b) {}
                             ^^^^^^^                   ^^^^^^^^^
                             construct the base part   constrcut the derived part
};

So for you example the constructor for car would become:

car(string br1, int wn1, double ms1, int nOD,string fT) : vehicle(br1, wn1, ms1), 
                                                          numberOfDoors(nOD), 
                                                          fuelType(fT) {}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

Since you are allocating instance of class car which is derived class so it is calling both the ctors. First it will call the ctor of class 'vehicle' and then 'car'. [Read the order of calling ctor in case inheritance]

paper.plane
  • 1,201
  • 10
  • 17