C++中的继承方式
目录
摘要
1. 公有继承(Public Inheritance)
2. 保护继承(Protected Inheritance)
3. 私有继承(Private Inheritance)
4. 多重继承(Multiple Inheritance)
继承列表的项数
摘要
在 C++ 中,继承是一种让一个类(派生类)从另一个类(基类)获得属性和方法的机制。我们在之前有通过这篇文章有聊到过继承的一些简单知识(C++中的封装,继承和多态-CSDN博客)。下面我们将继续介绍一些在实际项目中常用到的一些继承的方式。
1. 公有继承(Public Inheritance):基类的公有成员和保护成员在派生类中保持其访问权限,公有成员在派生类中仍然是公有的,保护成员在派生类中仍然是保护的。
2. 保护继承(Protected Inheritance):基类的公有成员和保护成员在派生类中都变成保护成员。
3. 私有继承(Private Inheritance):基类的公有成员和保护成员在派生类中都变成私有成员。
此外,C++ 还支持 多重继承(Multiple Inheritance),即一个类可以从多个基类继承。继承列表中的项数理论上没有限制,但过多的继承会导致代码复杂性增加和维护难度上升。
1. 公有继承(Public Inheritance)
在公有继承中,基类的公有成员在派生类中保持为公有成员,保护成员保持为保护成员,而私有成员仍然是私有的。
#include <iostream>
using namespace std;class Base {
public:void publicMethod() { cout << "Base public method" << endl; }
protected:void protectedMethod() { cout << "Base protected method" << endl; }
private:void privateMethod() { cout << "Base private method" << endl; }
};class DerivedPublic : public Base {
public:void derivedMethod() {publicMethod(); // 可以访问protectedMethod(); // 可以访问// privateMethod(); // 无法访问}
};int main() {DerivedPublic obj;obj.publicMethod(); // 可以访问// obj.protectedMethod(); // 无法访问// obj.privateMethod(); // 无法访问obj.derivedMethod();return 0;
}
2. 保护继承(Protected Inheritance)
在保护继承中,基类的公有成员和保护成员在派生类中都变成保护成员,而私有成员仍然是私有的。
#include <iostream>
using namespace std;class Base {
public:void publicMethod() { cout << "Base public method" << endl; }
protected:void protectedMethod() { cout << "Base protected method" << endl; }
private:void privateMethod() { cout << "Base private method" << endl; }
};class DerivedProtected : protected Base {
public:void derivedMethod() {publicMethod(); // 可以访问protectedMethod(); // 可以访问// privateMethod(); // 无法访问}
};int main() {DerivedProtected obj;// obj.publicMethod(); // 无法访问// obj.protectedMethod(); // 无法访问obj.derivedMethod();return 0;
}
3. 私有继承(Private Inheritance)
在私有继承中,基类的公有成员和保护成员在派生类中都变成私有成员,而私有成员仍然是私有的。
#include <iostream>
using namespace std;class Base {
public:void publicMethod() { cout << "Base public method" << endl; }
protected:void protectedMethod() { cout << "Base protected method" << endl; }
private:void privateMethod() { cout << "Base private method" << endl; }
};class DerivedPrivate : private Base {
public:void derivedMethod() {publicMethod(); // 可以访问protectedMethod(); // 可以访问// privateMethod(); // 无法访问}
};int main() {DerivedPrivate obj;// obj.publicMethod(); // 无法访问// obj.protectedMethod(); // 无法访问obj.derivedMethod();return 0;
}
4. 多重继承(Multiple Inheritance)
在多重继承中,一个派生类可以从多个基类继承。需要注意的是,多重继承可能会带来名字冲突和菱形继承等问题,需要小心处理。
#include <iostream>
using namespace std;class Base1 {
public:void methodBase1() { cout << "Base1 method" << endl; }
};class Base2 {
public:void methodBase2() { cout << "Base2 method" << endl; }
};class DerivedMultiple : public Base1, public Base2 {
public:void derivedMethod() {methodBase1();methodBase2();}
};int main() {DerivedMultiple obj;obj.methodBase1();obj.methodBase2();obj.derivedMethod();return 0;
}
继承列表的项数
理论上,继承列表的项数没有限制 -- 多重继承:
#include <iostream>
using namespace std;class Base1 {
public:void methodBase1() { cout << "Base1 method" << endl; }
};class Base2 {
public:void methodBase2() { cout << "Base2 method" << endl; }
};class Base3 {
public:void methodBase3() { cout << "Base3 method" << endl; }
};class DerivedMultiple : public Base1, public Base2, public Base3 {
public:void derivedMethod() {methodBase1();methodBase2();methodBase3();}
};int main() {DerivedMultiple obj;obj.methodBase1();obj.methodBase2();obj.methodBase3();obj.derivedMethod();return 0;
}