📜  带有示例的C++中的可见性模式

📅  最后修改于: 2021-05-30 05:23:57             🧑  作者: Mango

当一个基类是通过与继承的帮助下派生类中派生,基类的派生类中的辅助功能由可见性模式控制。派生类不继承对私有数据成员的访问。但是,它确实继承了完整的父对象,该对象包含该类声明的所有私有成员。

// C++ implementation to show
// Visibility modes
  
#include 
using namespace std;
  
// Base class
// Class A will be inherited
class A {
public:
    int x;
  
protected:
    int y;
  
private:
    int z;
};
  
// Derived class
// Class B will inherit Class A
class B : public A {
};
  
// main function
int main()
{
    B b;
  
    // x is public
    // so its value will be printed
    cout << b.x << endl;
  
    // y is protected
    // so it will give visibility error
    cout << b.y << endl;
  
    // z is not accessible from B
    // so it will give visibility error
    cout << b.z << endl;
};

编译错误:

prog.cpp: In function 'int main()':
prog.cpp:14:6: error: 'int A::y' is protected
  int y;
      ^
prog.cpp:34:12: error: within this context
  cout << b.y << endl;
            ^
prog.cpp:17:6: error: 'int A::z' is private
  int z;
      ^
prog.cpp:37:12: error: within this context
  cout << b.z << endl;
            ^

可见性在此程序中意味着什么?

  • 由于A中的成员“ x”是公共的,因此其可见性将向所有人开放。这意味着任何类都可以访问和使用此x。这就是’b.x’没有错误的原因。
  • A中的成员y受保护,其可见性仅对派生类而言。这意味着任何派生类都可以访问和使用此y。
  • A中的成员“ z”是私有的,它的可见性不会对其他任何类别开放。这意味着任何派生类都无法访问和使用此z。

可见性模式的类型:

可见性模式分为三种:

  1. 公共可见性模式:如果我们从公共基类派生子类。然后,基类的公共成员将在派生类中成为公共成员,而基类的受保护成员将在派生类中成为受保护成员。
    // C++ implementation to show
    // Public Visibility mode
      
    #include 
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Public Visibility mode
    class B : public A {
    };
      
    // main function
    int main()
    {
        B b;
      
        // x is public and it will remain public
        // so its value will be printed
        cout << b.x << endl;
      
        // y is protected and it will remain protected
        // so it will give visibility error
        cout << b.y << endl;
      
        // z is not accessible from B as
        // z is private and it will remain private
        // so it will give visibility error
        cout << b.z << endl;
    };
    

    编译错误:

    prog.cpp: In function 'int main()':
    prog.cpp:14:9: error: 'int A::y' is protected
         int y;
             ^
    prog.cpp:37:15: error: within this context
         cout << b.y << endl;
                   ^
    prog.cpp:17:9: error: 'int A::z' is private
         int z;
             ^
    prog.cpp:42:15: error: within this context
         cout << b.z << endl;
                   ^
    
  2. 受保护的可见性模式:如果我们从受保护的基类派生一个子类。然后,基类的公共成员和受保护的成员都将在派生类中受到保护。
    // C++ implementation to show
    // Protected Visibility mode
      
    #include 
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Protected Visibility mode
    class B : protected A {
    };
      
    // main function
    int main()
    {
        B b;
      
        // x is public and it will become protected
        // so it will give visibility error
        cout << b.x << endl;
      
        // y is protected and it will remain protected
        // so it will give visibility error
        cout << b.y << endl;
      
        // z is not accessible from B as
        // z is private and it will remain private
        // so it will give visibility error
        cout << b.z << endl;
    };
    

    编译错误:

    prog.cpp: In function 'int main()':
    prog.cpp:11:9: error: 'int A::x' is inaccessible
         int x;
             ^
    prog.cpp:33:15: error: within this context
         cout << b.x << endl;
                   ^
    prog.cpp:14:9: error: 'int A::y' is protected
         int y;
             ^
    prog.cpp:37:15: error: within this context
         cout << b.y << endl;
                   ^
    prog.cpp:17:9: error: 'int A::z' is private
         int z;
             ^
    prog.cpp:42:15: error: within this context
         cout << b.z << endl;
                   ^
    
  3. 私有可见性模式:如果我们从私有基类派生子类。然后,基类的公共成员和受保护的成员都将在派生类中变为私有成员。
    // C++ implementation to show
    // Private Visibility mode
      
    #include 
    using namespace std;
      
    // Base class
    // Class A will be inherited
    class A {
    public:
        int x;
      
    protected:
        int y;
      
    private:
        int z;
    };
      
    // Derived class
    // Class B will inherit Class A
    // using Private Visibility mode
    class B : private A {
    };
      
    // main function
    int main()
    {
        B b;
      
        // x is public and it will become private
        // so it will give visibility error
        cout << b.x << endl;
      
        // y is protected and it will become private
        // so it will give visibility error
        cout << b.y << endl;
      
        // z is not accessible from B as
        // z is private and it will remain private
        // so it will give visibility error
        cout << b.z << endl;
    };
    

    编译错误:

    prog.cpp: In function 'int main()':
    prog.cpp:11:9: error: 'int A::x' is inaccessible
         int x;
             ^
    prog.cpp:33:15: error: within this context
         cout << b.x << endl;
                   ^
    prog.cpp:14:9: error: 'int A::y' is protected
         int y;
             ^
    prog.cpp:37:15: error: within this context
         cout << b.y << endl;
                   ^
    prog.cpp:17:9: error: 'int A::z' is private
         int z;
             ^
    prog.cpp:42:15: error: within this context
         cout << b.z << endl;
                   ^
    

继承后如何更改“可见性”模式?

在特定的“可见性”模式的帮助下继承基类后,成员将如上所述自动更改其可见性。但是为了在继承后更改可见性,我们需要手动进行操作。

句法:

:
    using base::;

例如:

// inorder to change the visibility of x to public
:
    using base::;

示例:考虑一个包含公共成员“ a”,受保护成员“ b”和“ c”以及私有成员“ d”和“ e”的基类。下面的程序说明了如何将“ b”的可见性从受保护更改为公开。

// C++ implementation to show how to
// change the Visibility modes
  
#include 
using namespace std;
  
// Base class
// Class A will be inherited
class BaseClass {
  
    // Public members
public:
    int a;
  
    // Protected members
protected:
    int b;
    int c;
  
    // Private members
private:
    int d;
    int e;
};
  
// BaseClass will be inherited as private
// to change all the members to private first
class DerivedClass : private BaseClass {
  
    // Now change the visibility of b
    // from private to public
public:
    using BaseClass::b;
};
  
// main function
int main()
{
    DerivedClass derivedClass;
  
    // d must be private and
    // hence generate visibility error
    cout << derivedClass.d << endl;
  
    // b must be now pubic and hence should
    // not generate visibility error
    cout << derivedClass.b << endl;
  
    return 0;
};

编译错误:

prog.cpp: In function 'int main()':
prog.cpp:22:9: error: 'int BaseClass::d' is private
     int d;
         ^
prog.cpp:47:26: error: within this context
     cout << derivedClass.d << endl;
                          ^
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”