C++多继承的研究

#include "stdafx.h"
#include <iostream>
using namespace std;

class GrandMa {
public:
	GrandMa();
	void Show();
};
class Mather :public GrandMa {
public:
	Mather();
};
class Father {
public:
	Father();
	void Show();
};
class Boy :public Father, public Mather {

};
class Girl :public Mather, public Father {

};
GrandMa::GrandMa() {
	cout << "GrandMa" << endl;
}
void GrandMa::Show(){
	cout << "GrandMa' Show" << endl;
}
Mather::Mather() {
	cout << "Mather" << endl;
}
Father::Father(){
	cout << "Father" << endl;
}
void Father::Show() {
	cout << "Father's Show" << endl;
}
int main()
{
	Boy boy;
	//cout << "Boy:" << boy.Show() << endl;
	Girl girl;
    return 0;
}

问了解答两个问题:

1,多继承的时候先调用哪个父类的构造函数?

2,如果调用子类的一个方法,两个父类都有怎么办?

从上面的结果看出来构造是按照声明的顺序,如果一个方法多继承则无法调用

进一步的研究表明,如果Father也继承了GrandMa,那么GrandMa的构造函数会调用两次





你可能感兴趣的:(C++,继承,构造函数)