📜  C++编程中的公共,受保护和私有继承

📅  最后修改于: 2020-09-25 05:07:39             🧑  作者: Mango

在本教程中,我们将借助示例来学习在C++中使用公共继承,受保护继承和私有继承。

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

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

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

注意代码中的关键字public

class Derived : public Base

这意味着我们已经在公共模式下从基类创建了派生类。另外,我们还可以在受保护私有模式下派生类。

这3个关键字( publicprotectedprivate )在C++继承中称为访问说明符

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

publicprotectedprivate继承具有以下功能:

注意:派生类无法访问基类的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 
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

在这里,我们以public模式Base派生PublicDerived

结果,在PublicDerived

由于私有成员和受保护成员不可访问,因此我们需要创建公共函数getPVT()getProt()来访问它们:

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

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

公共继承中的可访问性

示例2:C++保护的继承

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

#include 
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

在这里,我们以保护模式Base派生了ProtectedDerived

结果,在ProtectedDerived

我们知道,不能直接访问受保护的成员。

其结果是,我们不能使用getPVT()ProtectedDerived 。这就是为什么我们需要在ProtectedDerived中创建getPub() 函数以访问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 
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

在这里,我们以私有模式Base派生PrivateDerived

结果,在PrivateDerived

众所周知,私有成员无法直接访问。

其结果是,我们不能使用getPVT()PrivateDerived 。这就是为什么我们需要在PrivateDerived中创建getPub() 函数以访问pub变量的原因。

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

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

私有继承中的可访问性