使用另一个类的构造函数

时间:2013-11-17 19:05:06

标签: c++ inheritance constructor

#include <iostream>
using namespace std;

class Vehicle{
protected:
    string type;
    int wheels;
    int engine; // number of engines in vehicle

public:
    Vehicle(string t, int w,bool e):
        type(t), wheels(w), engine(e){};
    void setType(string t) {type = t;}
    void setWheels(int w) {wheels = w;}
    void setEngine(int e) {engine = e;}
    string getType(){return type;}
    int getWheels() {return wheels;}
    int getEngine() {return engine;}


};

class Auto:public Vehicle {
private:
    string brand;
    int year;
public:
    Auto(string t, int w, bool e, string b, int y):

};

int main()
{
    return 0;
}

如何在Auto类中使用车辆类的构造函数?

2 个答案:

答案 0 :(得分:2)

Auto::Auto (string t, int w, bool e, string b, int y)
:
   Vehicle (t, w, e),
   brand (b),
   year (y)
{}

答案 1 :(得分:1)

class Auto:public Vehicle {
private:
  string brand;
  int year;
public:
  Auto(string t, int w, bool e, string b, int y):Vehicle(t,w,e){}

};