求教一个C++题目,大神来帮忙~~~~!!!!

有一个汽车类vehicle,它具有一个需传递参数的构造函数,类中数据成员:车轮个数wheels和车重weight放在保护段中;小车类car是它的私有派生类,其中包含载人数... 有一个汽车类vehicle,它具有一个需传递参数的构造函数,类中数据成员:车轮个数wheels和车重weight放在保护段中;小车类car是它的私有派生类,其中包含载人数 passenger_load;卡车类truck是vehicle的私有派生类,其中包含载人数passenger_load和载重量payload。每个类都有相关数据的输出方法。
大神帮忙哇~~~
展开
 我来答
chengxunfendou
2008-12-28 · TA获得超过2455个赞
知道小有建树答主
回答量:499
采纳率:0%
帮助的人:154万
展开全部
#include<iostream>
using namespace std;
class vehicle // 定义汽车类
{
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
protected:
int wheels; // 车轮数
float weight; // 重量
};
class car:public vehicle // 定义小车类
{
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
private:
int passenger_load; // 载人数
};
class truck:public vehicle // 定义卡车类
{
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
private:
int passenger_load; // 载人数
float payload; // 载重量
};
vehicle::vehicle(int wheels,float weight)
{
vehicle::wheels=wheels;
vehicle::weight=weight;
}
int vehicle::get_wheels()
{
return wheels;
}
float vehicle::get_weight()
{
return weight/wheels;
}
void vehicle::show()
{
cout << "车轮:" << wheels << "个" << endl;
cout << "重量:" << weight << "公斤" << endl;
}
car::car(int wheels, float weight, int passengers) :vehicle (wheels, weight)
{
passenger_load=passengers;
}
int car::get_passengers ()
{
return passenger_load;
}
void car::show()
{
cout <<" 车型:小车" << endl;
vehicle::show();
cout << "载人:" << passenger_load << "人" << endl;
cout << endl;
}
truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight)
{
passenger_load=passengers;
payload=max_load;
}
int truck::get_passengers()
{
return passenger_load;
}
float truck::efficiency()
{
return payload/(payload+weight);
}
void truck::show()
{
cout <<"车型:卡车" << endl;
vehicle:: show ();
cout << "载人:" << passenger_load << "人" << endl;
cout << "效率:" << efficiency() << endl;
cout << endl;
}
int main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "输出结果" << endl;
car1. show ();
tru1. show ();
return 0;
}

仅供借鉴。互相学习啊
世镶柳009
2008-12-28 · TA获得超过3017个赞
知道答主
回答量:2928
采纳率:0%
帮助的人:2472万
展开全部
$ cat a.cpp
#include <iostream>
class vehicle
{
int wheels;
double weight;
public:
vehicle(int wheels, double weight)
{
this->wheels = wheels;
this->weight = weight;
}
friend std::ostream& operator<<(std::ostream& os, const vehicle& v)
{
return std::cout << "wheels: " << v.wheels << "\tweight: "<< v.weight;
}
};
class car : private vehicle
{
int passenger_load;
public:
car(int passenger_load) : vehicle(4, 0.0)
{
this->passenger_load = passenger_load;
}
friend std::ostream& operator<<(std::ostream& os, const car& c)
{
return std::cout << "passenger_load: " << c.passenger_load;
}
};
class truck : private vehicle
{
int passenger_load;
double payload;
public:
truck(int passenger_load, double payload) : vehicle(4, 0.0)
{
this->passenger_load = passenger_load;
this->payload = payload;
}
friend std::ostream& operator<<(std::ostream& os, const truck& v)
{
return std::cout << "passenger_load: " << v.passenger_load
<< "\tpayload: " << v.payload;
}
};
int main()
{
car c(3);
truck t(2, 5.0);
std::cout << c << std::endl;
std::cout << t << std::endl;
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式