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;
    }
  • 相关阅读:
    datagridview中读取数据判断+考勤每月上班天数判断
    dateTimePicker日期比较+时间段内查询+员工查询薪资步骤+datagridview
    c#word 存取
    位图去空白
    过桥问题
    Dominos 2(DFS)(容器)
    poj 3421(三分)
    poj 3186(DP)
    安装Ubuntu
    poj 3273(二分)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/9920989.html
Copyright © 2011-2022 走看看