1.什么是多重继承
??????? 一个派生类可以有两个或多个基类(父类)
2.多重继承方法
???????? 将多个基类用逗号隔开
#include<iostream>
#include<string>
using namespace std;
class Father {
public:
Father(string name, int age);
~Father();
private:
string name;
string age;
};
Father::Father(string name, int age) {
this->name = name;
this->age = age;
}
Father::~Father() {
}
class Monther {
public:
Monther(string food);
~Monther();
private:
string food;
};
Monther::Monther(string food) {
this->food = food;
}
Monther::~Monther() {
}
//继承顺序会决定派生类构造函数的调用顺序 先father后mother
class son :public Father, public Monther {
public:
son(string name, int age, string food, string game);
~son();
private:
string game;
};
son::son(string name, int age, string food, string game) :
Father(name, age), Monther(food) {
this->game = game;
}
son::~son() {
}
?多重继承的二义性问题
class Father {
public:
fun();
}
class Monther{
public:
fun();
}
class son: public Father ,public Monther{
}
//当派生类继承重名成员时
//第一种方法:使用“类名::”指定从哪个基类继承的方法
son s1;
s1.Father::fun();
class Father {
public:
fun();
}
class Monther{
public:
fun();
}
class son: public Father ,public Monther{
public:
void fun();
}
//当派生类继承重名成员时
//第二种方法:在子类中重实现这个同名方法,并在这个方法内部,使用基类名进行限定
void son::fun(){
Father::fun();
Monther::fun();
}
son s1;
s1.fun();
|