本文主要是介绍cpp中虚实继承问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.一个基类base,被类a虚继承,类a被其他的类继续继承,那么base中的初始化必须由派生类的最后一个完成,其中任意一个都不能代替完成基类,如果在最后一个派生类里不进行这个base的初始化,那么就会调用相应的无参构造,值是随机数,这个也适用于一条链中的任意一个
2.只要存在虚继承,那么虚继承就会首先被调用,存在多种虚继承,则是根据以下被调用的顺序
class son2 : public son3,public son1,public son4//顺序从这里来
#include<iostream>
using namespace std;
class base1
{
private:int a, b;
public:base1() { cout << "这儿是一个基类base1" << endl; show(); };base1(int a, int b) :a(a), b(b) {cout << "有参构造base1" << endl; show();};void show(){cout << a << " " << b << endl;}
};
class base2
{
private:int a, b;
public:base2() { cout << "这儿是一个基类base2" << endl; show(); };base2(int a, int b) :a(a), b(b) {cout << "有参构造base2" << endl; show();};void show(){cout << a << " " << b << endl;}
};class son1 :virtual public base1
{
private:int c;
public:son1() { cout << "这是儿子一号" << endl; };son1(int a, int b, int c) :base1(a, b), c(c) {cout << "进行son1有参构造" << endl; show();};void show(){cout << c << endl;base1::show();}
};
class son3 :virtual public base2
{
private:int c;
public:son3() { cout << "这是儿子三号" << endl; };son3(int a, int b, int c) :base2(a, b), c(c) {cout << "进行son3有参构造" << endl; show();};void show(){cout << c << endl;base2::show();}
};
class son4 :public base2
{
private:int c;
public:son4() { cout << "这是儿子三号" << endl; };son4(int a, int b, int c) :base2(a, b), c(c) {cout << "进行son4有参构造" << endl; show();};void show(){cout << c << endl;base2::show();}
};
class son2 : public son3,public son1,public son4//顺序从这里来
{
private:int c;
public:son2() { cout << "这是儿子二号" << endl; };son2(int a, int b, int c, int d, int e, int f, int g) :son3(d, e, f),son1(a,b,c),son4(d,e,f),c(g){cout << "进行son2有参构造" << endl; show();};void show(){cout << c << endl;son1::show();son3::show();son4::show();}
};
class me : public son2
{
public:me() { cout << "这是我" << endl; };me(int a, int b, int c, int d, int e, int f, int g) : son2(a, b, c, d, e, f, g),base2(a,b),base1(a,b) {};
};
int main()
{me i(1, 2, 3, 4, 5, 6, 7);}
#include<iostream>
using namespace std;
class base1
{
private:int a, b;
public:base1() { cout << "这儿是一个基类base1" << endl; show(); };base1(int a, int b) :a(a), b(b) {cout << "有参构造base1" << endl; show();};void show(){cout << a << " " << b << endl;}
};
class base2
{
private:int a, b;
public:base2() { cout << "这儿是一个基类base2" << endl; show(); };base2(int a, int b) :a(a), b(b) {cout << "有参构造base2" << endl; show();};void show(){cout << a << " " << b << endl;}
};class son1 : public base1
{
private:int c;
public:son1() { cout << "这是儿子一号" << endl; };son1(int a, int b, int c) :base1(a, b), c(c) {cout << "进行son1有参构造" << endl; show();};void show(){cout << c << endl;base1::show();}
};
class son3 :virtual public son1
{
private:int c;
public:son3() { cout << "这是儿子三号" << endl; };son3(int a, int b, int c) :son1(a, b,c), c(c) {cout << "进行son3有参构造" << endl; show();};void show(){cout << c << endl;son1::show();}
};
class son4 :public son3
{
private:int c;
public:son4() { cout << "这是儿子四号" << endl; };son4(int a, int b, int c) :son3(a, b,c), c(c) {cout << "进行son4有参构造" << endl; show();};void show(){cout << c << endl;son3::show();}
};
/*class son2 : public son1, public son3, public son4
{
private:int c;
public:son2() { cout << "这是儿子二号" << endl; };son2(int a, int b, int c, int d, int e, int f, int g) :son3(d, e, f),son1(a,b,c),son4(d,e,f),base1(a,b),base2(a,b),c(g){cout << "进行son2有参构造" << endl; show();};void show(){cout << c << endl;son1::show();son3::show();son4::show();}
};
class me : public son2
{
public:me() { cout << "这是我" << endl; };me(int a, int b, int c, int d, int e, int f, int g) : son2(a, b, c, d, e, f, g) {};
};*/
int main()
{son4 s(1, 2, 3);}
这篇关于cpp中虚实继承问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!