Private inheritance and protected inheritance
Private inheritance (private)
-
Inherited access control
-
The public and protected members of the base class : both appear in the derived class as private ;
-
Private members of the base class: not directly accessible .
-
-
access permission
-
Member functions in derived classes: you can directly access the public and protected members of the base class, but you cannot directly access the private members of the base class;
-
Objects through derived classes: no direct access to any members inherited from the base class.
-
Private inheritance example
#ifndef _POINT_H
#define _POINT_H
class Point { //基类Point类的定义
public: //公有函数成员
void initPoint(float x = 0, float y = 0)
{ this->x = x; this->y = y;}
void move(float offX, float offY)
{ x += offX; y += offY; }
float getX() const { return x; }
float getY() const { return y; }
private: //私有数据成员
float x, y;
};
#endif //_POINT_H
#ifndef _RECTANGLE_H
#define _RECTANGLE_H
#include "Point.h"
class Rectangle: private Point { //派生类定义部分
public: //新增公有函数成员
void initRectangle(float x, float y, float w, float h) {
initPoint(x, y); //调用基类公有成员函数
this->w = w;
this->h = h;
}
void move(float offX, float offY) { Point::move(offX, offY); }
float getX() const { return Point::getX(); }
float getY() const { return Point::getY(); }
float getH() const { return h; }
float getW() const { return w; }
private: //新增私有数据成员
float w, h;
};
#endif //_RECTANGLE_H
#include <cmath>
using namespace std;
int main() {
Rectangle rect; //定义Rectangle类的对象
rect.initRectangle(2, 3, 20, 10); //设置矩形的数据
rect.move(3,2); //移动矩形位置
cout << "The data of rect(x,y,w,h): " << endl;
cout << rect.getX() <<", " //输出矩形的特征参数
<< rect.getY() << ", "
<< rect.getW() << ", "
<< rect.getH() << endl;
return 0;
}
Protected inheritance (protected)
-
Inherited access control
-
The public and protected members of the base class : both appear in the derived class as protected ;
-
Private members of the base class : not directly accessible .
-
-
access permission
-
Member functions in derived classes: you can directly access the public and protected members of the base class, but you cannot directly access the private members of the base class;
-
Objects through derived classes: no direct access to any members inherited from the base class.
-
-
Features and functions of protected members
-
For the module that creates the object of its class, it has the same nature as the private member.
-
For its derived classes, it has the same properties as public members.
-
It not only realizes data hiding, but also facilitates inheritance and realizes code reuse.
-
If the derived class has multiple base classes, that is, multiple inheritance, each base class can be inherited in different ways.
-
Examples of protected members
class A {
protected:
int x;
};
int main() {
A a;
a.x = 5;//错误
}
class A {
protected:
int x;
};
class B: public A{
public:
void function();
};
void B:function() {
x = 5; //正确
}
Examples of multiple inheritance
class A {
public:
void setA(int);
void showA() const;
private:
int a;
};
class B {
public:
void setB(int);
void showB() const;
private:
int b;
};
class C : public A, private B {
public:
void setC(int, int, int);
void showC() const;
private:
int c;
};
void A::setA(int x) {
a=x;
}
void B::setB(int x) {
b=x;
}
void C::setC(int x, int y, int z) {
//派生类成员直接访问基类的
//公有成员
setA(x);
setB(y);
c = z;
}
//其他函数实现略
int main() {
C obj;
obj.setA(5);
obj.showA();
obj.setC(6,7,9);
obj.showC();
// obj.setB(6); 错误
// obj.showB(); 错误
return 0;
}