注册 登录
编程论坛 C++教室

基类和派生类的一道题,帮我看一下错在哪里?

简Greensoul 发布于 2011-12-16 16:45, 857 次点击
#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;
}

请问输出载重效率的错在哪了?
8 回复
#2
kangzong992011-12-16 17:23
新手路过
#3
我菜1192011-12-16 17:42
ratio=load/(load+weight);
这块有问题
#4
简Greensoul2011-12-16 17:46
回复 3楼 我菜119
请问该怎么改?
#5
我菜1192011-12-16 17:49
回复 4楼 简Greensoul
怎么改??其实我也不知道,不知道你信不信,反正我是信了!
#6
CrystalFan2011-12-16 22:29
这是C语言问题,和类继承无关。整数除整数按整除算,小的除大的永远是0.
ratio=(double)load/(load+weight);
#7
简Greensoul2011-12-17 14:59
回复 6楼 CrystalFan
明白了,谢谢!
#8
我菜1192011-12-17 17:37
#9
iseelove2011-12-18 10:38
C/C++交流进群 76406740
一起交流,学习
1