zoukankan      html  css  js  c++  java
  • c++ 多继承

    #include<iostream>
    using namespace std;
    
    class Parent {
    public:
        Parent():a(100),b(200),c(300)
        {
            cout << "parent 构造。。。
    ";
        }
        ~Parent()
        {
            cout << "Parent 析构。。。
    ";
        }
        int a;
        int b;
        int c;
        void p_print()
        {
            cout << "a b c is" << a << " " << b << " " << c << endl;
        }
    
    };
    class Child1 :virtual public Parent
    {
    public:
        Child1() :Parent(), a(0), b(0), c(0) { cout << "child1 构造
    "; }
        ~Child1()
        {
            cout << "child1 析构,,,
    ";
        }
        void c_print()
        {
            cout << "a b c is" << a << " " << b << " " << c << endl;
        }
        int a;
        int b;
        int c;
    };
    class Child2 :public Parent
    {
    public:
        Child2() :Parent(), a(1), b(2), c(3) { cout << "child 2构造
    "; }
        ~Child2()
        {
            cout << "child 2析构,,,
    ";
        }
        void c_print()
        {
            cout << "a b c is" << a << " " << b << " " << c << endl;
        }
        int a;
        int b;
        int c;
    };
    class Child3 :public Child1,public Child2
    {
    public:
        Child3() :Parent(),Child1(),Child2(), a(10), b(20), c(30)
           { cout << "child3 构造
    "; }//如果前面没有使用虚继承,这里初始化Parent构造函数将出错
        ~Child3()
        {
            cout << "child 3析构,,,
    ";
        }
        void c_print()
        {
            cout << "a b c is" << a << " " << b << " " << c << endl;
        }
        int a;
        int b;
        int c;
    };
    int main()
    {
        Child3 c3;
    
        return 0;
    }
  • 相关阅读:
    找水王
    哈利波特图书购买问题
    中序线索化二叉树[C语言实现及注释]
    第一篇随文。
    理解Python函数中的的return
    记录一款实时同步的软件——Lsyncd
    for循环
    while循环
    文件操作
    我的第一个博客
  • 原文地址:https://www.cnblogs.com/sea-stream/p/9920989.html
Copyright © 2011-2022 走看看