Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++多重继承

作者:渚寒烟淡   发布日期:2025-10-25   浏览:65

#include <iostream>
using namespace std;

// 基类1
class Base1 {
public:
    void showBase1() {
        cout << "This is Base1" << endl;
    }
};

// 基类2
class Base2 {
public:
    void showBase2() {
        cout << "This is Base2" << endl;
    }
};

// 派生类,多重继承自Base1和Base2
class Derived : public Base1, public Base2 {
public:
    void showDerived() {
        cout << "This is Derived class" << endl;
    }
};

int main() {
    Derived d;
    d.showBase1();  // 调用Base1的方法
    d.showBase2();  // 调用Base2的方法
    d.showDerived();  // 调用Derived的方法

    return 0;
}

解释说明:

  • Base1Base2 是两个独立的基类,每个基类都有自己的成员函数 showBase1showBase2
  • Derived 类同时继承了 Base1Base2,形成了多重继承。
  • Derived 类中,可以访问来自 Base1Base2 的所有公共成员。
  • main 函数中,创建了一个 Derived 类的对象 d,并通过该对象调用了来自不同基类的方法。

这种结构展示了 C++ 中如何使用多重继承来组合多个基类的功能。

上一篇:c++ make_shared

下一篇:c++sort函数头文件

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++格式化字符串

c++ orm框架

队列c++

c++freopen怎么用

进制转换c++代码

c++ 获取系统时间

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站