INHERITENCE
TYPES OF INHERITANCE:
There are public, protected and private inheritance in C++.
Syntax:
1) public inheritance makes public members of the base class public in the
derived class, and the protected members of the base class
remain protected in the derived class.
2) protected inheritance makes the public and protected members of the base
class protected in the derived class.
3) private inheritance makes the public and protected members of the base
class private in the derived class.
Note: private members of the base class are inaccessible to the derived class.
EXAMPLE :
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
, // x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
};
Example 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;
}
};
main() {
, PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
}
OUTPUT
Private = 1
Protected = 2
Public = 3
NOTE : Since private and protected members are not accessible from main(), we
need to create public functions getPVT() and getProt() to access them:
Example 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
NOTE :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 public function getPub() in ProtectedDerived in
order to access the pub variable.
Example of private inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
TYPES OF INHERITANCE:
There are public, protected and private inheritance in C++.
Syntax:
1) public inheritance makes public members of the base class public in the
derived class, and the protected members of the base class
remain protected in the derived class.
2) protected inheritance makes the public and protected members of the base
class protected in the derived class.
3) private inheritance makes the public and protected members of the base
class private in the derived class.
Note: private members of the base class are inaccessible to the derived class.
EXAMPLE :
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
, // x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
};
Example 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;
}
};
main() {
, PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
}
OUTPUT
Private = 1
Protected = 2
Public = 3
NOTE : Since private and protected members are not accessible from main(), we
need to create public functions getPVT() and getProt() to access them:
Example 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
NOTE :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 public function getPub() in ProtectedDerived in
order to access the pub variable.
Example of private inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;