作业帮 > 综合 > 作业

#includeclass vehicle{private:float weight;int wheels;public

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/12 11:08:21
#include
class vehicle
{
private:
float weight;
int wheels;
public:
vehicle(int in_wheels,float in_weight)
{wheels=in_wheels;weight=in_weight;}
int get_wheels(){return wheels;}
float get_weight(){return weight;}
};
class car:public vehicle
{
private:
int passenger_load;
public:
car(int in_wheels,float in_weight,int people=5):vehicle(in_wheels,in_weight)
{passenger_load=people;}
int get_passengers(){return passenger_load;}
};
void main()
{
car bm(4,1000);
cout
这种构造函数的写法叫 使用参数列表来初始化构造函数
在本题中,car类的构造函数接入三个开参,其中有一个有默认值的形参,但是在初始化过程中,它又调用vehicle类的构造函数,并把前两个形友传给这个构造函数.
这种用法在子类的构造函数中经常使用.即子类调用父类的构造函数.
希望可以帮到你~