This code works:
PHP Code:
#include <iostream>
#include <string>
using namespace std;
class Vehicle
{
public:
Vehicle(string, int, int);
Vehicle();
private:
string name;
int tire;
int speed;
};
Vehicle::Vehicle(string s, int t, int sp)
{
name = s;
tire = t;
speed = sp;
cout << name << ' ' << tire << ' ' << speed << endl;
}
Vehicle::Vehicle()
{
cout << "Default constructor\n";
}
class Car : public Vehicle
{};
int main()
{
Car myCar;
Vehicle yourCar("pickup", 17, 55);
return 0;
}
but changing Vehicle yourCar... to Car yourCar... doesn't. That's because w/o a base class initializer, the default base class constructor is called, which doesn't take 3 arguments.
This works for Car
PHP Code:
class Car : public Vehicle
{
public:
Car::Car(){};
Car::Car(string n, int t, int s) : Vehicle(n, t, s){};
};
That runs and compiles correctly. You have to add the default constructor to Car when you add the other one, at least in MSVC. W/o the 2nd constructor, you'll be able to use the default constructor in Vehicle.