Visibility Modes in C++ with Examples
When a base class is derived by a derived class with the help of inheritance, the accessibility of base class by the derived class is controlled by visibility modes. The derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.
CPP
#include <bits/stdc++.h>
using namespace std;
class A {
public :
int x;
protected :
int y;
private :
int z;
};
class B : public A {
};
int main()
{
B b;
cout << b.x << endl;
cout << b.y << endl;
cout << b.z << endl;
};
|
Compile Errors:
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;
^
What does visibility means in this program?
- Since the member ‘x’ in A is public, its visibility will be open to all. It means that any class can access and use this x. That is the reason there is no error in ‘b.x’.
- The member ‘y’ in A is protected, its visibility will be only to the derived class. It means that any derived class can access and use this y.
- The member ‘z’ in A is private, its visibility will not be open to any other class. It means that any derived class cannot access and use this z.
Types of Visibility modes:
There are three types of Visibility modes:
- Public Visibility mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class.
CPP
#include <bits/stdc++.h>
using namespace std;
class A {
public :
int x;
protected :
int y;
private :
int z;
};
class B : public A {
};
int main()
{
B b;
cout << b.x << endl;
cout << b.y << endl;
cout << b.z << endl;
};
|
- Compile Errors:
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;
^
-
- Protected Visibility mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base class will become protected in the derived class.
CPP
#include <bits/stdc++.h>
using namespace std;
class A {
public :
int x;
protected :
int y;
private :
int z;
};
class B : protected A {
};
int main()
{
B b;
cout << b.x << endl;
cout << b.y << endl;
cout << b.z << endl;
};
|
- Compile Errors:
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;
^
-
- Private Visibility mode: If we derive a subclass from a Private base class. Then both public member and protected members of the base class will become Private in the derived class.
CPP
#include <bits/stdc++.h>
using namespace std;
class A {
public :
int x;
protected :
int y;
private :
int z;
};
class B : private A {
};
int main()
{
B b;
cout << b.x << endl;
cout << b.y << endl;
cout << b.z << endl;
};
|
- Compile Errors:
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;
^
-
How to change the Visibility mode after inheritance?
After inheriting a base class with the help of a specific Visibility mode, the members will automatically change its visibility as mentioned above. But inorder to change the visibility after this inheritance, we need to do it manually.
Syntax:
<visibility_mode>:
using base::<member>;
For example:
// inorder to change the visibility of x to public
<public>:
using base::<x>;
Example: Consider a base class containing a public member ‘a’, protected members ‘b’ and ‘c’, and private members ‘d’ and ‘e’. Below program explains how to change the visibility of ‘b’ from protected to public.
CPP
#include <bits/stdc++.h>
using namespace std;
class BaseClass {
public :
int a;
protected :
int b;
int c;
private :
int d;
int e;
};
class DerivedClass : private BaseClass {
public :
using BaseClass::b;
};
int main()
{
DerivedClass derivedClass;
cout << derivedClass.d << endl;
cout << derivedClass.b << endl;
return 0;
};
|
Compile Errors:
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;
^
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...