Public Protected Private Inheritance
Public Protected Private Inheritance
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
1|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
#include <iostream>
using namespace std;
class Base {
private: Accessibility in public Inheritance
int pvt = 1;
Accessibility private members protected members public members
protected:
int prot = 2;
Base Class Yes Yes Yes
Derived Class No Yes Yes
public: Drive Class Obj. No No Yes
int pub = 3;
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
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:
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.
2|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
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;
3|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
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;
class A {
... .. ...
};
class B: public A {
... .. ...
};
class C: public B {
... ... ...
};
Here, class B is derived from the base class A and the class C is derived from the derived class B.
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
In this program, class C is derived from class B (which is derived from base class A).
The obj object of class C is defined in the main() function.
When the display() function is called, display() in class A is executed. It's because there is
no display() function in class C and class B.
The compiler first looks for the display() function in class C. Since the function doesn't exist there,
it looks for the function in class B (as C is derived from B).
The function also doesn't exist in class B, so the compiler looks for it in class A (as B is derived
from A).
If display() function exists in C, the compiler overrides display() of class A (because of member
function overriding).
5|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
int main() {
Bat b1;
return 0;
}
int main() {
derived obj;
obj.someFunction() // Error!
}
This problem can be solved using the scope resolution function to specify which function
to class either base1or base2
int main() {
derived obj;
obj.base1::someFunction( ); // Function of base1 class is called
obj.base2::someFunction(); // Function of base2 class is called.
}
6|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
// base class
class Animal {
public:
void info() {
cout << "I am an animal." << endl;
}
};
// derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I am a Dog. Woof woof." << endl;
}
};
// derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I am a Cat. Meow." << endl;
}
};
int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info(); // Parent Class function
dog1.bark();
7|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
These three keywords are also used in a completely different context to specify the visibility inheritance
model.
This table gathers all of the possible combinations of the component declaration and inheritance model
presenting the resulting access to the components when the subclass is completely defined.
The table above is interpreted in the following way (take a look at the first row):
if a component is declared as public and its class is inherited as public the resulting access is public.
An example:
class Super {
public: int p;
private: int q;
protected: int r;
};
int storage;
public:
void put(int val) { storage = val; }
int get(void) { return storage; }
};
int main(void) {
Super object;
object.put(100);
object.put(object.get());
cout << object.get() << endl;
return 0;
}
Now lets define a subclass:
int main(void) {
Sub object;
object.put(100);
object.put(object.get());
cout << object.get() << endl;
return 0;
}
The defined class named Sub which is a subclass of class named Super or that Sub class is derived from
the Super class. The Sub class introduces neither new variables nor new functions. Does it mean that any
object of the Sub class inherits all the traits after the Super class being in fact a copy of a Super class’ objects?
No. It doesn’t.
If we compile the following code, we will get nothing but compilation errors saying
that put and get methods are inaccessible. Why?
When we omit the visibility specifier, the compiler assumes that we are going to apply the so-
called private inheritance. It means that all public superclass components turn into private access, private
superclass components won't be accessible at all. It consequently means that you are not allowed to use
the latter inside the subclass.
We have to inform the compiler that we want to preserve the previously used access policy.
Yes.
The third access level is called protected. The keyword protected means that the component marked with
it behaves like a public one when used by any of the subclasses and looks like a private one to the rest of the
world. -- This is true only for the publicly inherited classes (like the Super class in our example) --
9|P ag e
Govt. Graduate Islamia College, Gujranwala. Prof. Usman Ahmad.
class Super {
protected:
int storage;
public:
void put(int val) { storage = val; }
int get(void) { return storage; }
};
int main(void) {
Sub object;
object.put(100);
object.put(object.get() + 1);
object.print();
return 0;
}
As you see in the example code we a new functionality to the Sub class and it does one important thing: it
accesses the storage variable from the Super class.
It wouldn’t be possible if the variable was declared as private. In the main function scope the variable
remains hidden anyway so if you write anything like:
object.storage = 0;
The compiler will inform you that it is an error: 'int Super::storage' is protected.
Finally, the last program will produce the following output:
storage = 101
10 | P a g e