亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 三种继承方式深度解析:public、protected 与 private

C++ 三种继承方式深度解析:public、protected 与 private

本教程通过代码示例与访问性对比表,详细阐述 C++ public、protected、private 继承对基类成员在派生类中的可见性影响。

C++ 继承中,派生类可用不同访问模式从基类派生:

class Base { .... };
class Derived : public Base { .... };

public 表示公有继承。也可用 protected 或 private 继承。这三个关键字在继承上下文中称为访问说明符。


三种继承方式的成员可见性规则

注意: 基类 private 成员对派生类始终不可见。

class Base {
public: int x;
protected: int y;
private: int z;
};

class PublicDerived: public Base {
    // x: public
    // y: protected
    // z: 不可访问
};

class ProtectedDerived: protected Base {
    // x: protected
    // y: protected
    // z: 不可访问
};

class PrivateDerived: private Base {
    // x: private
    // y: private
    // z: 不可访问
}

示例 1:public 继承

// public 继承演示
#include <iostream>
using namespace std;

class Base {
private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
int getPVT() { return pvt; }
};

class PublicDerived : public Base {
public:
    int getProt() { return prot; }
};

int main() {
    PublicDerived object1;
    cout << "Private = " << object1.getPVT() << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.pub << endl;
    return 0;
}

输出

Private = 1
Protected = 2
Public = 3

PublicDerived 以 public 方式继承 Base。结果:

main() 中无法直接访问 private/protected 成员,需通过公有函数:

// 错误:Base::pvt 不可访问
cout << "Private = " << object1.pvt;
// 错误:Base::prot 不可访问
cout << "Protected = " << object1.prot;

getPVT() 定义在 Base 内(访问私有成员),getProt() 定义在 PublicDerived 内(可访问 protected 成员)。


public 继承访问性总结表

可访问性private 成员protected 成员public 成员
基类是是是
派生类否是是

示例 2:protected 继承

// protected 继承演示
#include <iostream>
using namespace std;

class Base {
private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
int getPVT() { return pvt; }
};

class ProtectedDerived : protected Base {
public:
    int getProt() { return prot; }
    int getPub() { return pub; }
};

int main() {
    ProtectedDerived object1;
    cout << "Private cannot be accessed." << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.getPub() << endl;
    return 0;
}

输出

Private cannot be accessed.
Protected = 2
Public = 3

ProtectedDerived 以 protected 方式继承。结果:

protected 成员类外不可直接访问,故 main() 中不能调用 getPVT() 或直接访问 pub,需在派生类中提供公有包装函数 getPub()。


protected 继承访问性总结表

可访问性private 成员protected 成员public 成员
基类是是是
派生类否是是(继承为 protected)

示例 3:private 继承

// private 继承演示
#include <iostream>
using namespace std;

class Base {
private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
int getPVT() { return pvt; }
};

class PrivateDerived : private Base {
public:
    int getProt() { return prot; }
    int getPub() { return pub; }
};

int main() {
    PrivateDerived object1;
    cout << "Private cannot be accessed." << endl;
    cout << "Protected = " << object1.getProt() << endl;
    cout << "Public = " << object1.getPub() << endl;
    return 0;
}

输出

Private cannot be accessed.
Protected = 2
Public = 3

PrivateDerived 以 private 方式继承。结果:

private 成员类外不可访问,故需在派生类中提供 getPub() 访问 pub,getPVT() 也不可直接调用。


private 继承访问性总结表

可访问性private 成员protected 成员public 成员
基类是是是
派生类否是(继承为 private)是(继承为 private)

C语言

  1. C++结构体与类:嵌入式开发者实战指南
  2. Sigfox 推出专用区域网络,增强地理定位,拓展物联网解决方案
  3. 公有云、私有云、混合云:如何选择适合的云策略
  4. 公有云决策全景:核心收益、潜在风险与关键考量因子
  5. 私有云全景解析:核心优势、局限性与数据资产保护实战
  6. 掌握 C++ 变量、字面量与常量:核心概念与实战指南
  7. C++类与对象:面向对象编程实战指南
  8. C++ 动态内存管理:new 与 delete 专家级实战指南
  9. C++ 友元函数与友元类:突破访问控制的利器
  10. C++ 运算符全解析:类型、示例与示例程序
  11. C++ Struct 与 Class 核心差异、语法及最佳实践
  12. C++日期时间完全指南:ctime库、tm结构体与格式化输出