当基类由派生类在继承的帮助下派生时,派生类对基类的可访问性由可见性模式控制。派生类不继承对私有数据成员的访问。但是,它确实继承了一个完整的父对象,其中包含该类声明的任何私有成员。
// 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’ 是public ,它的可见性将对所有人开放。这意味着任何类都可以访问和使用这个 x。这就是“b.x”中没有错误的原因。
- A 中的成员’y’ 是protected ,其可见性仅对派生类可见。这意味着任何派生类都可以访问和使用这个 y。
- A 中的成员’z’ 是private ,它的可见性不会对任何其他类开放。这意味着任何派生类都不能访问和使用这个 z。
可见性模式的类型:
可见性模式分为三种类型:
- 公共可见性模式:如果我们从公共基类派生子类。然后基类的公共成员在派生类中变为公共成员,基类的受保护成员在派生类中变为受保护成员。
// 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; ^
- 受保护的可见性模式:如果我们从受保护的基类派生子类。然后基类的公共成员和受保护成员都将在派生类中成为受保护的。
// 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; ^
- 私有可见性模式:如果我们从私有基类派生子类。然后基类的公共成员和受保护成员在派生类中都将变为私有。
// 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; ^
继承后如何更改可见性模式?
在特定 Visibility 模式的帮助下继承基类后,成员将自动更改其可见性,如上所述。但是为了在这个继承后改变可见性,我们需要手动进行。
句法:
:
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 等的准备工作,请参阅完整的面试准备课程。