基类和派生类的一道题,帮我看一下错在哪里?
#include<iostream>#include<cstring>
using namespace std;
class Vehicle //汽车类定义
{
private:
int Speed;
int Weight;
public:
Vehicle(int speed,int weight)
{
Speed=speed;
Weight=weight;
cout<<"Vehicle类的构造函数被调用"<<endl;
}
~Vehicle()
{
cout<<"Vehicle类的析构函数被调用"<<endl;
}
void Display()
{
cout<<"最大速度:"<<Speed<<"km/h"<<'\t'<<"车的重量:"<<Weight<<"kilo"<<'\t'<<endl;
}
int GetSpeed()
{
return Speed;
}
int GetWeight()
{
return Weight;
}
};
class Truck:public Vehicle //卡车类定义(公有继承)
{
private:
int Load;
public:
Truck(int load,int speed,int weight):Vehicle(speed,weight)
{
Load=load;
cout<<"Truck类的构造函数被调用"<<endl;
}
~Truck()
{
cout<<"Truck类的析构函数被调用"<<endl;
}
void DisplayTruc()
{
cout<<"载重:"<<Load<<"kilo"<<'\t';
Display();
}
int GetLoad()
{
return Load;
}
double ratio()
{
double ratio;
int load,weight;
load=GetLoad();
weight=GetWeight();
ratio=load/(load+weight);
return(ratio);
}
};
int main()
{
Truck truck1(500,160,500);
truck1.DisplayTruc();
cout<<"卡车的载重效率="<<truck1.ratio()<<endl;
return 0;
}
请问输出载重效率的错在哪了?