CPlusPlusThings/practical_exercises/10_day_practice/day5/虚拟继承/派生类初始化.cpp
Light-City a4d828bb4c update
2020-04-06 00:57:02 +08:00

34 lines
634 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
using namespace std;
class A {
int a;
public:
A(int x) {
a=x;
cout<<"Virtual Bass A..."<<endl;
}
};
class B:virtual public A {
public:
B(int i):A(i){ cout<<"Virtual Bass B..."<<endl; }
};
class C:virtual public A{
int x;
public:
C(int i):A(i){
cout<<"Constructing C..."<<endl;
x=i;
}
};
class ABC:public C, public B {
public:
//虚基类由最终派生类初始化
ABC(int i,int j,int k):C(i),B(j),A(i) //L1这里必须对A进行初始化
{ cout<<"Constructing ABC..."<<endl; }
};
int main(){
ABC obj(1,2,3);
system("pause");
return 0;
}