c_cpp C ++多层继承

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++多层继承相关的知识,希望对你有一定的参考价值。

#include <stdio.h>

class Base
{
public:
    Base()
    {
        printf("Base constructor\n");
    }
    
    ~Base()
    {
        printf("Base destructor\n");
    }
};

class Child1: virtual public Base
{
public:
    Child1()
    {
        printf("Child1 constructor\n");
    }
    
    ~Child1()
    {
        printf("Child1 destructor\n");
    }
};

class Child2: virtual public Base
{
public:
    Child2()
    {
        printf("Child2 constructor\n");
    }
    
    ~Child2()
    {
        printf("Child2 destructor\n");
    }
};

class Foo: public Child1, public Child2
{
public:
    Foo()
    {
        printf("Foo constructor\n");
    }
    
    ~Foo()
    {
        printf("Foo destructor\n");
    }
};

int main(int argc, char *argv[])
{
    Foo foo;
    
    return 0;
}

以上是关于c_cpp C ++多层继承的主要内容,如果未能解决你的问题,请参考以下文章