1 概述
通过继承机制,可以利用已有的对象类型来定义新对象类型。所定义的新对象类型不仅仅拥有新定义的成员,而且还同时拥有旧的成员。我们称已存在的用来派生新类的类为基类,又称为父类。由已存在的类派生出的新类称为派生类,又称为子类。。
2 对象访问作用域
作用域:
- public(公有作用域) 对象自身、派生类内部,对象外部都可以访问。
- protected(保护作用域) 对象自身、派生类内部可以访问。
- private(私有作用域) 只有对象自身可以访问。
说明:
- 友元函数和友元类不受上述作用域限制。
- protected(保护作用域)就是为了派生使用的。
3 继承
继承分类:
- public继承 派生类中父类public成员还是public成员。
- protected继承 派生类中父类public成员变成protected成员。
- private继承 派生类中父类public成员变成private成员。
- 多重继承 派生类可以从多个父类派生。
- 虚继承 - 虚继承 在多重继承,有共同父类时只保持有一个父类。
3.3 private继承
3.3.1 例子
#include <iostream>
struct BaseClass
{
BaseClass(int num) //构造函数
: number(num)
{
std::cout << "BaseClass is called" << std::endl;
}
~BaseClass() //析构函数
{
std::cout << "~BaseClass is called" << std::endl;
}
int getNumber() { return number; }//对外提供的接口
bool setNumber(int num){
if(num > 100)
return false;
number = num;
return true;
}
protected: //保护成员变量,外部无法访问
int number;
};
struct Child1Class : private BaseClass
{
Child1Class(int num) //构造函数
: BaseClass(num)
{
std::cout << "Child1Class is called" << std::endl;
}
~Child1Class() //析构函数
{
std::cout << "~Child1Class is called" << std::endl;
}
void printNumber()
{
std::cout << "child.number:" << getNumber() << std::endl;
}
bool setNumber(int num)//覆盖父类setNumber
{
if(num > 200)
return false;
number = num;
return true;
}
using BaseClass::getNumber;
};
struct Child2Class : Child1Class
{
Child2Class(int num) //构造函数
: Child1Class(num)
{
std::cout << "Child2Class is called" << std::endl;
}
~Child2Class() //析构函数
{
std::cout << "~Child2Class is called" << std::endl;
}
int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的
};
int main(int argc, char *argv[])
{
{
BaseClass base(20);
std::cout << "base.number: " << base.getNumber() << std::endl;
base.setNumber(150);
std::cout << "base.number: " << base.getNumber() << std::endl;
}
Child2Class child(40);
child.printNumber();
child.setNumber(150);
child.printNumber();
std::cout << "number: " << child.getNumber() << std::endl;//
return 0;
}
3.3.2 运行结果
编译结果:
private.cpp: In member function ‘int Child2Class::getNumber()’:
private.cpp:69:27: error: ‘struct BaseClass BaseClass::BaseClass’ is inaccessible within this context
69 | int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的
| ^~~~~~~~~
private.cpp:4:1: note: declared here
4 | {
| ^
private.cpp:69:48: error: ‘BaseClass’ is not an accessible base of ‘Child2Class’
69 | int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的
从编译结果看BaseClass::getNumber函数对派生类Child2Class来说是private.
修改代码如下:
struct Child2Class : Child1Class
{
Child2Class(int num) //构造函数
: Child1Class(num)
{
std::cout << "Child2Class is called" << std::endl;
}
~Child2Class() //析构函数
{
std::cout << "~Child2Class is called" << std::endl;
}
int getNumber() { return Child1Class::getNumber(); } //调用Child1Class::getNumber()
};
重新实现getNumber函数调用Child1Class::getNumber()。
运行结果:
BaseClass is called
base.number: 20
base.number: 20
~BaseClass is called
BaseClass is called
Child1Class is called
Child2Class is called
child.number:40
child.number:150
number: 150
~Child2Class is called
~Child1Class is called
~BaseClass is called