【C++OJ多重继承与虚拟继承】OOP 水陆两用汽车(多重继承+虚拟继承)

【C++OJ多重继承与虚拟继承】OOP 水陆两用汽车(多重继承+虚拟继承)

题目描述

设计水陆两用汽车类。

定义Vehicle基类,包含成员变量:重量weight;成员函数:构造函数,setWeight,函数display

定义Car类,继承于Vehicle类,包含成员变量:空气排量aird;成员函数:构造函数,setAird,display

定义Boat类,继承于Vehicle类,包含成员变量:排水量tonnage;成员函数:构造函数,setTonnage,display

定义AmphibianCar类,继承于Car类与Boat类,包含成员函数:构造函数,display

构造AmphibianCar类(构造函数中输出相关信息),并输出相关属性值。随后修改各项属性值(set函数中输出相关信息),并输出。

输入

输入重量,空气排量,排水量

输入修改后的重量,空气排量,排水量

输出

构造AmphibianCar类(构造函数中输出相关信息),并输出相关属性值。随后修改各项属性值(set函数中输出相关信息),并输出。

输入样例

4 200 1.35
5 201 1.25

输出样例

载入Vehicle类构造函数
载入Car类构造函数
载入Boat类构造函数
载入AmphibianCar类构造函数
重量:4吨,空气排量:200CC,排水量:1.35吨
重新设置重量
重新设置空气排量
重新设置排水量
重量:5吨,空气排量:201CC,排水量:1.25吨

参考代码

#include <iostream>
using namespace std;

class Vehicle //定义Vehicle基类
{
public: //成员函数:构造函数,setWeight,函数display
    Vehicle(int wval) : weight(wval)
    {
        cout << "载入Vehicle类构造函数" << endl;
    }
    void setWeight(int wval)
    {
        weight = wval;
        cout << "重新设置重量" << endl;
    }
    void display()
    {
        cout << "重量:" << weight << "吨,";
    }

protected:
    int weight; //包含成员变量:重量weight;
};

//虚拟继承,只产生一个拷贝。(解决多继承时可能发生的对同一基类继承多次而产生的二义性问题)
class Car : virtual public Vehicle //定义Car类,继承于Vehicle类。
{
public: //成员函数:构造函数,setAird,display
    Car(int wval, int aval) : Vehicle(wval), aird(aval)
    {
        cout << "载入Car类构造函数" << endl;
    }
    void setAird(int aval)
    {
        aird = aval;
        cout << "重新设置空气排量" << endl;
    }
    void display()
    {
        cout << "空气排量:" << aird << "CC,";
    }

protected:
    int aird; //包含成员变量:空气排量aird
};

class Boat : virtual public Vehicle //定义Boat类,继承于Vehicle类 虚拟继承 
{
public: //成员函数:构造函数,setTonnage,display
    Boat(int wval, float tval) : Vehicle(wval), tonnage(tval)
    {
        cout << "载入Boat类构造函数" << endl;
    }
    void setTonnage(float tval)
    {
        tonnage = tval;
        cout << "重新设置排水量" << endl;
    }
    void display()
    {
        cout << "排水量:" << tonnage << "吨";
    }

protected:
    float tonnage; //包含成员变量:排水量tonnage
};

class AmphibianCar : public Car, public Boat //水陆两用汽车类,多重继承
{
public:
    AmphibianCar(int wval, int aval, float tval) : Vehicle(wval), Car(wval, aval), Boat(wval, tval) //构造函数
    {
        cout << "载入AmphibianCar类构造函数" << endl; //构造AmphibianCar类(构造函数中输出相关信息),并输出相关属性值
    }
    void setAmphibianCar(int wval, int aval, float tval) //修改各项属性值(set函数中输出相关信息)
    {
        Vehicle::setWeight(wval);
        Car::setAird(aval);
        Boat::setTonnage(tval);
        display();
    }
    void display()
    {
        Vehicle::display();
        Car::display();
        Boat::display();
        cout << endl;
    }
};

int main()
{

    int weight, aird;
    float tonnage;
    cin >> weight >> aird >> tonnage;
    AmphibianCar a(weight, aird, tonnage);
    a.display();
    cin >> weight >> aird >> tonnage;
    a.setAmphibianCar(weight, aird, tonnage);
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ferry_xie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值