c++编程题 求解答!!
已知车辆类(vehicle)定义如下,classvehicle{private:intnumwheels;intweight;public:vehicle(intinit...
已知车辆类(vehicle)定义如下,
class vehicle{
private:
int numwheels; int weight;
public:
vehicle(int initwheels,int initweight):numwheels(initwheels),weight(initweight){}
void print()
{ cout<<endl<<"num wheels:"<<numwheels<<"weight:<<weight"; }
};
(1)从车辆类出发,定义一个卡车类(truck),为其增加两个数据成员:
int loudlimit;//最大载重量
int numtrailers;//拖车数
(2)定义构造函数正确实现其所有成员的初始化
(3)定义一个print()函数,打印其所有的数据成员
(4)编写主函数,创建一个卡车类对象,并调用其print函数 展开
class vehicle{
private:
int numwheels; int weight;
public:
vehicle(int initwheels,int initweight):numwheels(initwheels),weight(initweight){}
void print()
{ cout<<endl<<"num wheels:"<<numwheels<<"weight:<<weight"; }
};
(1)从车辆类出发,定义一个卡车类(truck),为其增加两个数据成员:
int loudlimit;//最大载重量
int numtrailers;//拖车数
(2)定义构造函数正确实现其所有成员的初始化
(3)定义一个print()函数,打印其所有的数据成员
(4)编写主函数,创建一个卡车类对象,并调用其print函数 展开
1个回答
展开全部
class truck:public vehicle
{
private:
int loudlimit;//最大载重量
int numtrailers;//拖车数
public:
truck(int initwheels,int initweight,int loudlimit,int numtrailers):vehicle(initwheels,initweight),loudlimit(loudlimit),numtrailers(numtrailers){};
void print()
{
vehicle::print();
cout<<endl<<"loudlimit:"<<loudlimit<<"numtrailers:<<numtrailers"; }
};
}
int main()
{
truck t;
t.print();
return 0;
}
{
private:
int loudlimit;//最大载重量
int numtrailers;//拖车数
public:
truck(int initwheels,int initweight,int loudlimit,int numtrailers):vehicle(initwheels,initweight),loudlimit(loudlimit),numtrailers(numtrailers){};
void print()
{
vehicle::print();
cout<<endl<<"loudlimit:"<<loudlimit<<"numtrailers:<<numtrailers"; }
};
}
int main()
{
truck t;
t.print();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询