Kulis
Kulis

Reputation: 1010

Constructor in inheritance

How to declare constructor on class Tanks, in order to create new object, like that:

tanks t34(durability, velocity, damage);

Here is my class:

#include <iostream>
using namespace std;

class vehicles{
private:
    double durability;
    double velocity;
public:
     void drive() { cout << "drive\n"; }
     void info() { cout << durability << " " << velocity << "\n"; }
     vehicles(double d, double v) : durability(d), velocity(v) {}
     ~vehicles() {}
};

class tanks:public vehicles{
private:
    double damage;
public:
    using vehicles::vehicles;
    tanks(double dmg) : damage(dmg) {}
    void shot();
};

So i would like to copy variable from:

vehicles(double d, double v) : durability(d), velocity(v) {}

and add it to class Tanks.

Upvotes: 1

Views: 74

Answers (1)

taocp
taocp

Reputation: 23634

Just add another constructor in tanks:

tanks(double dmg, double v, double d):vechicles(d,v), dmanage(dmg) {}
                                    //^^call base class constructor to init base part

Then you should be able to create an object of tanks as follows:

tanks t34(durability, velocity, damage);

Upvotes: 2

Related Questions