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;
    }
  • 相关阅读:
    Java学习8.17
    Java学习8.16
    Java学习8.15
    Java学习8.14
    Java学习8.13
    Java学习8.12
    Java学习8.11
    131. Palindrome Partitioning 回文串分割
    40. Combination Sum II 不允许使用重复元素
    39. Combination Sum 凑出一个和,可以重复用元素(含duplicates)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/9920989.html
Copyright © 2011-2022 走看看