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

本文详细讲解了如何使用C++设计一个水陆两用汽车类,通过多重继承结合虚拟继承来解决属性冲突问题,展示了类的构造函数、成员函数及属性设置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【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);
}
### 西北农林科技大学 C++ OJ 继承 示例 题目 解法 在西北农林科技大学的C++在线判题系统(OJ)中,继承是一个常见的编程主题。以下是一些继承相关的题目示例以及可能的解决方案。 #### 示例题目 1: 单继承 题目描述:定义一个基类 `Animal` 和派生类 `Dog`,其中 `Dog` 类继承自 `Animal` 类。要求实现以下功能: - 基类 `Animal` 包含一个虚函数 `makeSound()`。 - 派生类 `Dog` 重写该函数以输出 `"Woof!"`。 ```cpp #include <iostream> using namespace std; class Animal { public: virtual void makeSound() { cout << "Some generic sound" << endl; } }; class Dog : public Animal { public: void makeSound() override { cout << "Woof!" << endl; } }; int main() { Animal* myAnimal = new Dog(); myAnimal->makeSound(); // 输出 "Woof!" delete myAnimal; return 0; } ``` 上述代码展示了单继承的基本用法[^1]。 #### 示例题目 2: 多继承 题目描述:定义两个基类 `Swimmer` 和 `Runner`,以及一个派生类 `Dolphin`,要求 `Dolphin` 同时继承自 `Swimmer` 和 `Runner`。实现以下功能: - `Swimmer` 包含一个函数 `swim()`,输出 `"Swimming..."`。 - `Runner` 包含一个函数 `run()`,输出 `"Running..."`。 - `Dolphin` 能够调用这两个函数。 ```cpp #include <iostream> using namespace std; class Swimmer { public: void swim() { cout << "Swimming..." << endl; } }; class Runner { public: void run() { cout << "Running..." << endl; } }; class Dolphin : public Swimmer, public Runner {}; int main() { Dolphin d; d.swim(); // 输出 "Swimming..." d.run(); // 输出 "Running..." return 0; } ``` 这段代码展示了多继承的实现方式[^2]。 #### 示例题目 3: 虚继承 题目描述:定义三个类 `A`、`B` 和 `C`,其中 `B` 和 `C` 都继承自 `A`,而 `D` 同时继承自 `B` 和 `C`。为了避免菱形继承问题,使用虚继承。 ```cpp #include <iostream> using namespace std; class A { public: int x; A(int val) : x(val) {} }; class B : virtual public A { public: B(int val) : A(val) {} }; class C : virtual public A { public: C(int val) : A(val) {} }; class D : public B, public C { public: D(int val) : A(val), B(val), C(val) {} }; int main() { D obj(10); cout << obj.x; // 输出 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ferry_xie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值