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

C++ 公共、受保护和私有继承

C++ 公有、受保护和私有继承

在本教程中,我们将通过示例学习在 C++ 中使用公共继承、保护继承和私有继承。

在C++继承中,我们可以通过不同的访问方式从基类派生出一个子类。例如,

class Base {
.... ... ....
};

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

注意关键字 public 在代码中

class Derived : public Base

这意味着我们在公共模式中从基类创建了一个派生类 .或者,我们也可以在 protected 中派生类 或私人 模式。

这 3 个关键字(public , protected , 和 private ) 被称为访问说明符 在 C++ 继承中。


C++ 中的公共、受保护和私有继承

公开 , 受保护,私人 继承具有以下特点:

注意: private 派生类无法访问基类的成员。

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

class PublicDerived: public Base {
    // x is public
    // y is protected
    // z is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {
    // x is protected
    // y is protected
    // z is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {
    // x is private
    // y is private
    // z is not accessible from PrivateDerived
}

示例 1:C++ 公共继承

// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class PublicDerived : public Base {
   public:
    // function to access protected member from Base
    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 来自 Base公共模式 .

结果,在 PublicDerived

由于私人 并且受保护 main() 无法访问成员 ,我们需要创建公共函数getPVT()getProt() 访问它们:

// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;

请注意,getPVT() 函数已在 Base 中定义 .但是 getProt() 函数已在 PublicDerived 中定义 .

这是因为 pvt ,这是私有的Base , PublicDerived 无法访问 .

但是,prot PublicDerived 可以访问 由于公共继承。所以,getProt() 可以从 PublicDerived 中访问受保护的变量 .


公共继承中的可访问性

辅助功能 私人会员 受保护的成员 公共成员
基类 是的 是的 是的
派生类 没有 是的 是的

示例 2:C++ 保护继承

// C++ program to demonstrate the working of protected inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class ProtectedDerived : protected Base {
   public:
    // function to access protected member from Base
    int getProt() {
        return prot;
    }

    // function to access public member from Base
    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 来自 Base 处于保护模式 .

结果,在 ProtectedDerived

众所周知,受保护 不能从班级外部直接访问成员。结果,我们不能使用 getPVT() 来自 ProtectedDerived .

这也是为什么我们需要创建 getPub() ProtectedDerived 中的函数 为了访问 pub 变量。

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

受保护继承中的可访问性

辅助功能 私人会员 受保护的成员 公共成员
基类 是的 是的 是的
派生类 没有 是的 是(作为保护变量继承)

示例 3:C++ 私有继承

// C++ program to demonstrate the working of private inheritance

#include <iostream>
using namespace std;

class Base {
   private:
    int pvt = 1;

   protected:
    int prot = 2;

   public:
    int pub = 3;

    // function to access private member
    int getPVT() {
        return pvt;
    }
};

class PrivateDerived : private Base {
   public:
    // function to access protected member from Base
    int getProt() {
        return prot;
    }

    // function to access private member
    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 来自 Base 私人模式 .

结果,在 PrivateDerived

众所周知,私有成员不能从类外部直接访问。结果,我们不能使用 getPVT() 来自 PrivateDerived .

这也是为什么我们需要创建 getPub() PrivateDerived 中的函数 为了访问 pub 变量。

// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;

私有继承中的可访问性

辅助功能 私人会员 受保护的成员 公共成员
基类 是的 是的 是的
派生类 没有 是(作为私有变量继承) 是(作为私有变量继承)

C语言

  1. 公共云与私有云与混合云
  2. 公共云的优缺点
  3. 私有云的优缺点
  4. C++ 变量、文字和常量
  5. C++ 类和对象
  6. C++ 内存管理:新建和删除
  7. C++朋友函数和朋友类
  8. C++ 中的结构和类
  9. 私有区域网络建立在 Sigfox 公共网络上
  10. C++ 中的运算符示例:什么是、类型和程序
  11. 结构和类的区别:用 C++ 示例解释
  12. C++ 日期和时间