vehicle类是基类由它派生出car类和truck类将公共的属性和方法放在vehicle类中。
本题程序如下:
本程序的执行结果如下:
#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
ublic vehicle // 定义小车类
{
int passenger_load;
// 载人数
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck
ublic 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 ();
}
输出结果
车型:小车
车轮:4个
重量:2000公斤
载人:5人
车型:卡车
车轮:10个
重量:8000公斤
载人:3人
效率:0.977012