0% found this document useful (0 votes)
9 views9 pages

Visibility Modes in C++

The document explains visibility modes in C++ inheritance, detailing how access to base class members is controlled by private, protected, and public visibility modes. It provides examples of each mode, illustrating how members are inherited and accessed in derived classes. Additionally, it addresses ambiguity resolution in multiple inheritance scenarios using class resolution operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Visibility Modes in C++

The document explains visibility modes in C++ inheritance, detailing how access to base class members is controlled by private, protected, and public visibility modes. It provides examples of each mode, illustrating how members are inherited and accessed in derived classes. Additionally, it addresses ambiguity resolution in multiple inheritance scenarios using class resolution operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Visibility Modes in C++

What is the visibility mode?


In inheritance whenever the derived class inherits from the base
class than which of the member of the parent class can be
accessible in the child class is controlled by the visibility mode. By
default visibility mode is always set to private.
Syntax is:
class derived_class_name :: visibility_mode base_class_name
{
//Lines of code
}
Types of Visibility Mode in C++
There are total 3 types of visibility mode in C++ that are:
1. Private visibility mode
2. Protected visibility mode
3. Public visibility mode
4. class Base {
5. public:
6. int x;
7. protected:
8. int y;
9. private:
10. int z;
11. };
12.
13. class PublicDerived: public Base {
14. // x is public
15. // y is protected
16. // z is not accessible from PublicDerived
17. };
18.
19. class ProtectedDerived: protected Base {
20. // x is protected
21. // y is protected
22. // z is not accessible from ProtectedDerived
23. };
24.
25. class PrivateDerived: private Base {
26. // x is private
27. // y is private
28. // z is not accessible from PrivateDerived
29. };
1. Private visibility mode:
When we inherit a derived class from the base class with private
visibility mode then the public and protected members of the base
class become private members of the derived class.
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : private X{
//As the visibility mode is private none of the member of base class is
inherited to derived class
};
int main()
{
//Nothing can be accessed using Class Y object because there are no
members in class Y.
return 0;
}

Example 1: C++ private Inheritance


// 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;
}
Output
Private cannot be accessed.
Protected = 2
Public = 3

Here, we have derived PrivateDerived from Base in private mode.


As a result, in PrivateDerived :
 prot , pub and getPVT() are inherited as private.
 pvt is inaccessible since it is private in Base .
As we know, private members cannot be directly accessed from outside the
class. As a result, we cannot use getPVT() from PrivateDerived .
That is also why we need to create the getPub() function
in PrivateDerived in order to access the pub variable.
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible


cout << "Public = " << object1.pub;

Accessibility in private Inheritance


private
Accessibility protected members public members
members

Base Class Yes Yes Yes

Derived Yes (inherited as private Yes (inherited as private


No
Class variables) variables)

2. Protected visibility mode:


When we inherit a derived class from a base class with protected
visibility mode the protected and public members of the base class
become protected members of the derived class
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : protected X{
//As the visibility mode is protected the protected and public members
of class X becomes the protected members of Class Y
//protected: int b,int c inherited from class X
};
int main()
{
//As the members in the class Y are protected they cannot be accessed
in main using Class Y object.
return 0;
}

Example 2: C++ protected Inheritance


// 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;
}
Output
Private cannot be accessed.
Protected = 2
Public = 3

Here, we have derived ProtectedDerived from Base in protected mode.


As a result, in ProtectedDerived :
 prot , pub and getPVT() are inherited as protected.
 pvt is inaccessible since it is private in Base .
As we know, protected members cannot be directly accessed from outside
the class. As a result, we cannot use getPVT() from ProtectedDerived .
That is also why we need to create the getPub() function
in ProtectedDerived in order to access the pub variable.
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible


cout << "Public = " << object1.pub;

Accessibility in protected Inheritance


Accessibility private members protected members public members

Base Class Yes Yes Yes

Derived Class No Yes Yes (inherited as protected variables)

3. Public mode:
When we inherit a derived class from a base class with public
visibility mode, the public members and protected members of the
base class will be inherited as public members and protected
members respectively of the derived class.
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : public X{
//As the visibility mode is public the protected members of class X
becomes protected member for class Y and public members of class X becomes
public member for class Y
//protected: int b; inherited from class X
//public: int c; inherited from class X
};
int main()
{
//Only int c can be accessed in main function using Class Y object as
it is public;
Y obj;
std::cout<<obj.c;
return 0;
}

Example 1: C++ public Inheritance


// 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;
}
Output
Private = 1
Protected = 2
Public = 3

Here, we have derived PublicDerived from Base in public mode.


As a result, in PublicDerived :
 prot is inherited as protected.
 pub and getPVT() are inherited as public.
 pvt is inaccessible since it is private in Base .
Since private and protected members are not accessible from main() , we
need to create public functions getPVT() and getProt() to access them:
// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible


cout << "Protected = " << object1.prot;

Notice that the getPVT() function has been defined inside Base . But
the getProt() function has been defined inside PublicDerived .
This is because pvt , which is private in Base , is inaccessible
to PublicDerived .
However, prot is accessible to PublicDerived due to public inheritance.
So, getProt() can access the protected variable from within PublicDerived .
Accessibility in public Inheritance
private members protected members public members
Accessibility

Base Class Yes Yes Yes

Derived Class No Yes Yes

Ambiquity Resolution in Inheritance


Ambiguity can be occurred in using the multiple inheritance when a
function with the same name occurs in more than one base class.
Let's understand this through an example:
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6. void display()
7. {
8. std::cout << "Class A" << std::endl;
9. }
10.};
11. class B
12.{
13. public:
14. void display()
15. {
16. std::cout << "Class B" << std::endl;
17. }
18.};
19. class C : public A, public B
20.{
21. void view()
22. {
23. display();
24. }
25. };
26.int main()
27. {
28. C c;
29. c.display();
30. return 0;
31. }
Output:
error: reference to 'display' is ambiguous
display();
o The above issue can be resolved by using the class resolution operator
with the function. In the above example, the derived class code can be
rewritten as:
1. class C : public A, public B
2. {
3. void view()
4. {
5. A :: display(); // Calling the display() function of class A.
6. B :: display(); // Calling the display() function of class B.
7.
8. }
9. };

You might also like