Inheritance 2
Inheritance 2
Protected and
Private Inheritance
In C++ inheritance, we can derive a child class from the base class in different
access modes. For example,
class Base {
.... ... ....
};
This means that we have created a derived class from the base class in public
mode. Alternatively, we can also derive classes in protected or private modes.
These 3 keywords (public, protected, and private) are known as access
specifiers in C++ inheritance.
Note: private members of the base class are inaccessible to the derived
class.
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
#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;
}
};
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
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.
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
Derived Class No Yes (inherited as private variables) Yes (inherited as private variables)
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Output
Derived Function
Here, the same function print() is defined in both Base and Derived classes.
So, when we call print() from the Derived object derived1,
the print() from Derived is executed by overriding the function in Base.
Working of function overriding in
C++
As we can see, the function was overridden because we called the function
from an object of the Derived class.
Had we called the print() function from an object of the Base class, the function
would not have been overridden.
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
return 0;
}
Output
Derived Function
Base Function
derived2.Base::print();
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Output
Derived Function
Base Function
Notice the code Base::print();, which calls the overridden function inside
the Derived class.
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Output
Base Function
In this program, we have created a pointer of Base type named ptr. This pointer
points to the Derived object derived1.