C++编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight
编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的私有派生类其中包含载人数passenger_load。...
编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的私有派生类其中包含载人数passenger_load。卡车类truck是vehicle的私有派生类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。
展开
3个回答
展开全部
#include<iostream.h>
class vehicle // 定义汽车类
{
protected:
int wheels; // 车轮数
float weight; // 重量
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
class car:public vehicle // 定义小车类
{
int passenger_load; // 载人数
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck:public vehicle // 定义卡车类
{
int passenger_load; // 载人数
float payload; // 载重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
};
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;
}
void main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "输出结果" << endl;
car1. show ();
tru1. show ();
}
class vehicle // 定义汽车类
{
protected:
int wheels; // 车轮数
float weight; // 重量
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
class car:public vehicle // 定义小车类
{
int passenger_load; // 载人数
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck:public vehicle // 定义卡车类
{
int passenger_load; // 载人数
float payload; // 载重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
};
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;
}
void main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "输出结果" << endl;
car1. show ();
tru1. show ();
}
展开全部
#include<iostream>
using namespace std;
class vehicle{
public:
vehicle(int m,int n);
~vehicle();
void print();
private:
int wheels;
int weight;
};
class car:private vehicle{
public:
car(int m,int n,int p);
~car();
void print();
private:
int passenger_load;
};
class truck:private vehicle{
public:
truck(int m,int n,int p1,int p2);
~truck();
void print();
private:
int passenger_load;
int payload;
};
vehicle::vehicle(int m,int n){
wheels=m;
weight=n;
}
vehicle::~vehicle(){
cout<<"vehicle destructor called."<<endl;
}
void vehicle::print(){
cout<<"the wheel number of the vehicle are: "<<wheels<<endl;
cout<<"the weight of the vehicle is: "<<weight<<endl;
}
car::car(int m,int n,int p):vehicle(m,n){
passenger_load=p;
}
car::~car(){
cout<<"car destructor called."<<endl;
}
void car::print(){
cout<<"the passenger_load of the car is: "<<passenger_load<<endl;
}
truck::truck(int m,int n,int p1,int p2):vehicle(m,n){
passenger_load=p1;
payload=p2;
}
truck::~truck(){
cout<<"truck destructor called."<<endl;
}
void truck::print(){
cout<<"the passenger_load of the truck is: "<<passenger_load<<endl;
cout<<"the payload of the truck is: "<<payload<<endl;
}
int main(){
vehicle a(4,200);
car b(3,300,400);
truck c(4,250,300,450);
a.print();
b.print();
c.print();
system("pause");
return 0;
}
其中倒数第二句话你可以去掉,那句话是用来暂停查看结果的。 析构函数的结果看不到,如果想看的话在命令行里面输入:vehicle.exe>1.txt,这样1.txt里面就是输出结果。
using namespace std;
class vehicle{
public:
vehicle(int m,int n);
~vehicle();
void print();
private:
int wheels;
int weight;
};
class car:private vehicle{
public:
car(int m,int n,int p);
~car();
void print();
private:
int passenger_load;
};
class truck:private vehicle{
public:
truck(int m,int n,int p1,int p2);
~truck();
void print();
private:
int passenger_load;
int payload;
};
vehicle::vehicle(int m,int n){
wheels=m;
weight=n;
}
vehicle::~vehicle(){
cout<<"vehicle destructor called."<<endl;
}
void vehicle::print(){
cout<<"the wheel number of the vehicle are: "<<wheels<<endl;
cout<<"the weight of the vehicle is: "<<weight<<endl;
}
car::car(int m,int n,int p):vehicle(m,n){
passenger_load=p;
}
car::~car(){
cout<<"car destructor called."<<endl;
}
void car::print(){
cout<<"the passenger_load of the car is: "<<passenger_load<<endl;
}
truck::truck(int m,int n,int p1,int p2):vehicle(m,n){
passenger_load=p1;
payload=p2;
}
truck::~truck(){
cout<<"truck destructor called."<<endl;
}
void truck::print(){
cout<<"the passenger_load of the truck is: "<<passenger_load<<endl;
cout<<"the payload of the truck is: "<<payload<<endl;
}
int main(){
vehicle a(4,200);
car b(3,300,400);
truck c(4,250,300,450);
a.print();
b.print();
c.print();
system("pause");
return 0;
}
其中倒数第二句话你可以去掉,那句话是用来暂停查看结果的。 析构函数的结果看不到,如果想看的话在命令行里面输入:vehicle.exe>1.txt,这样1.txt里面就是输出结果。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class vehicle
{
public:
void GetWheels();
void GetWeight();
priviate:
int wheels;
double weight;
};
class car : priviate vehicle
{
public:
GetPassenger_load();
priviate:
int passenger_load;
};
class truck : priviate vehicle
{
public:
void GetPassenger_load();
void GetPayload();
priviate:
int passenger_load;
double payload;
};
{
public:
void GetWheels();
void GetWeight();
priviate:
int wheels;
double weight;
};
class car : priviate vehicle
{
public:
GetPassenger_load();
priviate:
int passenger_load;
};
class truck : priviate vehicle
{
public:
void GetPassenger_load();
void GetPayload();
priviate:
int passenger_load;
double payload;
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询