【C++】继承和派生编程练习

C++继承与派生:虚函数与多态实践
本文介绍了C++中继承和派生的概念,通过Building、House和Office类作为示例,探讨了基类和派生类中重名函数的调用方法,特别是虚函数的多态调用。文章详细解释了二义性问题,包括基类与派生类之间的二义性和多继承中的二义性,并阐述了虚基类的作用和如何解决二义性问题。此外,还展示了代码实践和运行结果。

 1.建立普通基类Building,用来存储一座楼房的层数、房间数以及他的总平方数。建立派生类House,继承Building,并存储下面的内容:卧室与卧室的数量。另外,建立派生类Office,继承Buiding,并存储灭火器与电话的数目。

下面是自己的解答过程,不是标准答案,但是自己调试没有问题。

#include <iostream>

using namespace std;
class Building
{
private:
    int Layer_number;
    int room_number;
    double total_square;
public:
    Building(int a=0,int b=0,double c=0)//定义基类的构造函数
    {
        Layer_number=a;
        room_number=b;
        total_square=c;
    }
    void print()
    {
        cout<<"Layer_number:"<<Layer_number<<endl;
        cout<<"room_number:"<< room_number<<endl;
        cout<<"house's total_square:"<<total_square<<endl;
    }
};
class House:public Building
{
private:
    int badroom_number;
    int bathroom_number;
public:
    //基类定义了默认构造函数(或根本没有定义),派生类定义中可以省略对基类构造函数的调用,但是如果需求必须初始化
    House(int a,int b,int x,int y,double z)
    {
        badroom_number=a;
        bathroom_numb
### C++ 继承派生的相关练习题 以下是几个基于继承派生概念的练习题目及其示例代码: --- #### 题目 1: 设计一个多态程序模拟动物叫声 要求定义一个 `Animal` 基类以及若干派生类(如 `Dog`, `Cat`)。每个派生类重载虚函数 `makeSound()` 来输出不同动物的声音。 ```cpp #include <iostream> using namespace std; // 定义基类 Animal class Animal { public: virtual void makeSound() const = 0; // 纯虚函数,使 Animal 成为抽象类 }; // 定义 Dog 类 class Dog : public Animal { public: void makeSound() const override { // 覆盖基类中的纯虚函数 cout << "Woof! Woof!" << endl; } }; // 定义 Cat 类 class Cat : public Animal { public: void makeSound() const override { // 覆盖基类中的纯虚函数 cout << "Meow! Meow!" << endl; } }; int main() { Animal* animal1 = new Dog(); // 动态分配内存并指向派生类对象 Animal* animal2 = new Cat(); animal1->makeSound(); // 输出狗叫声音 animal2->makeSound(); // 输出猫叫声音 delete animal1; // 释放动态分配的内存 delete animal2; return 0; } ``` 此代码展示了如何利用多态性虚函数来实现行为的不同表现形式[^2]。 --- #### 题目 2: 使用虚基类解决菱形继承问题 设计一个具有菱形继承结构的程序,并通过虚基类消除重复成员变量的问题。 ```cpp #include <iostream> using namespace std; // 定义虚基类 A class A { protected: int value; public: A(int v) : value(v) {} void showValue() const { cout << "A's Value: " << value << endl; } }; // 定义 B C 派生于 A class B : virtual public A { public: B(int v) : A(v * 2) {} // 构造函数初始化列表调用 A 的构造函数 void showMessage() const { cout << "B Class Message" << endl; } }; class C : virtual public A { public: C(int v) : A(v * 3) {} // 构造函数初始化列表调用 A 的构造函数 void showMessage() const { cout << "C Class Message" << endl; } }; // D 同时继承自 B C class D : public B, public C { public: D(int v) : A(v), B(v), C(v) {} // 明确指定虚基类 A 的构造参数 void displayAllValues() const { showValue(); B::showMessage(); C::showMessage(); } }; int main() { D d(5); d.displayAllValues(); // 展示最终的结果 return 0; } ``` 这段代码演示了如何使用虚基类避免菱形继承带来的二义性问题[^4]。 --- #### 题目 3: 实现几何图形的面积计算 创建一个基类 `Shape` 并提供两个派生类 `Circle` `Rectangle`。分别实现它们各自的面积计算逻辑。 ```cpp #include <iostream> #include <cmath> // 提供 M_PI 常量 using namespace std; // 定义 Shape 抽象基类 class Shape { public: virtual double getArea() const = 0; // 纯虚函数用于获取面积 virtual ~Shape() {} // 虚析构函数确保安全删除指针 }; // Circle 是 Shape 的派生类 class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} double getArea() const override { // 重写基类的纯虚函数 return M_PI * pow(radius, 2); } void setRadius(double r) { radius = r; } }; // Rectangle 是 Shape 的另一个派生类 class Rectangle : public Shape { private: double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double getArea() const override { // 重写基类的纯虚函数 return width * height; } void setSize(double w, double h) { width = w; height = h; } }; int main() { Circle circle(2.0); // 创建半径为 2 的圆形 Rectangle rectangle(3.0, 4.0); // 创建宽高分别为 3 4 的矩形 cout << "Circle Area: " << circle.getArea() << endl; // 输出圆的面积 cout << "Rectangle Area: " << rectangle.getArea() << endl; // 输出矩形的面积 return 0; } ``` 该示例说明了如何通过继承扩展功能的同时保持良好的封装性[^5]。 --- #### 题目 4: 函数模板的应用——求数组最大值 编写一个通用的函数模板用来求解不同类型的一维数组的最大值。 ```cpp #include <iostream> using namespace std; template<typename T> T findMax(const T array[], int size) { T maxVal = array[0]; for (int i = 1; i < size; ++i) { if (array[i] > maxVal) { maxVal = array[i]; } } return maxVal; } int main() { int intArray[] = {1, 7, 3, 9, 4}; double doubleArray[] = {1.1, 7.7, 3.3, 9.9, 4.4}; cout << "Max Int: " << findMax(intArray, 5) << endl; // 查找整数数组的最大值 cout << "Max Double: " << findMax(doubleArray, 5) << endl; // 查找浮点数数组的最大值 return 0; } ``` 上述代码体现了泛型编程的思想,能够处理多种数据类型的输入[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值