Loading lessons...
Inheritance & Public, Private, Protected Derivation
Inheritance & Public, Private, Protected Derivation
Inheritance is the mechanism of deriving a new class from an existing one. The new class gets the members of the old class and can add its own, so tested code is reused instead of rewritten. This lesson covers how a derived class is defined, the protected access specifier, and the most asked table in Unit III: what is visible after public, private and protected derivation.
1. Base and Derived Classes
| Term | Also called | Meaning |
|---|---|---|
| Base class | parent class, super class | the existing class whose members are inherited |
| Derived class | child class, sub class | the new class that inherits them |
A derived class is a kind of its base class: a Car is a Vehicle, a Manager is an Employee. This "is-a" relationship is the test for when inheritance is the right tool.
Why inheritance?
- Reusability: existing, tested code is used again without copying it.
- Extensibility: new features are added in the derived class without touching the base class.
- Hierarchical classification: classes can be organised the way the real world is (animal → bird → sparrow).
- Polymorphism: a base-class pointer can refer to derived objects, which makes run-time polymorphism possible (Lesson 28).
- Less code, easier maintenance: a fix in the base class benefits every derived class.
2. Defining a Derived Class
The visibility mode (public, private or protected) is optional. The default is private for a class (and public for a struct).
class ABC : private XYZ {
...
}; // private derivation
class ABC : public XYZ {
...
}; // public derivation
class ABC : XYZ {
...
}; // private derivation by defaultPrivate members of the base class are never inherited in an accessible way: they exist inside every derived object, but the derived class's own functions cannot name them. They can be reached only through the base class's public or protected member functions.
3. The protected Access Specifier
private members are hidden even from derived classes. Sometimes a derived class should be able to use a member directly while the outside world still cannot. That is what protected is for: a protected member is accessible to member functions and friends of its own class and of any class derived from it, but not to other code.
| Member of the base class is | in the base class | in a derived class | outside (through an object) |
|---|---|---|---|
private | ✅ | ❌ | ❌ |
protected | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ |
4. Visibility of Inherited Members
The visibility mode decides how the inherited members appear in the derived class, and therefore whether a class derived further, or outside code, can use them.
| Base class member | public derivation | protected derivation | private derivation |
|---|---|---|---|
private | not inherited (inaccessible) | not inherited (inaccessible) | not inherited (inaccessible) |
protected | protected | protected | private |
public | public | protected | private |
Visibility of base-class members under public, protected and private derivation
Read the table as: inherited members become no more visible than the visibility mode.
- Public derivation: public stays public, protected stays protected. Objects of the derived class can call the base's public functions. This is the normal "is-a" inheritance.
- Protected derivation: public and protected both become protected. Further derived classes can use them; outside code cannot.
- Private derivation: public and protected both become private. The derived class can use them internally, but neither outside code nor any further derived class can. This means "implemented in terms of", not "is-a".
#include <iostream>
using namespace std;
class Base {
private:
int a; // never directly accessible in derived classes
protected:
int b;
public:
int c;
Base() {
a = 1;
b = 2;
c = 3;
}
int getA() {
return a;
} // public route to the private member
};
class PubDerived : public Base {
public:
void show() {
// cout << a; // error: a is private in Base
cout << "PubDerived: a = " << getA() << ", b = " << b << ", c = " << c << endl;
}
};
class ProDerived : protected Base {
public:
void show() {
cout << "ProDerived: b = " << b << ", c = " << c << endl;
}
};
class PriDerived : private Base {
public:
void show() {
cout << "PriDerived: b = " << b << ", c = " << c << endl;
}
};
class Grandchild : public ProDerived {
public:
void test() {
cout << "Grandchild of ProDerived can still use c = " << c << endl;
}
};
int main() {
PubDerived x;
x.show();
cout << "x.c from outside = " << x.c << endl; // public stays public
ProDerived y;
y.show();
// cout << y.c; // error: c is protected in ProDerived
PriDerived z;
z.show();
// cout << z.c; // error: c is private in PriDerived
Grandchild g;
g.test();
return 0;
}Output:
PubDerived: a = 1, b = 2, c = 3
x.c from outside = 3
ProDerived: b = 2, c = 3
PriDerived: b = 2, c = 3
Grandchild of ProDerived can still use c = 35. A Complete Example: Single Inheritance (Public)
#include <iostream>
using namespace std;
class Employee {
protected:
int id;
float basic;
public:
void getData(int i, float b) {
id = i;
basic = b;
}
void showData() {
cout << "ID: " << id << ", Basic: " << basic << endl;
}
};
class Manager : public Employee { // Manager IS-A Employee
float allowance;
public:
void setAllowance(float a) {
allowance = a;
}
float salary() {
return basic + allowance;
} // uses protected 'basic'
void showAll() {
showData(); // inherited public function
cout << "Allowance: " << allowance << ", Salary: " << salary() << endl;
}
};
int main() {
Manager m;
m.getData(501, 60000); // inherited from Employee
m.setAllowance(15000); // Manager's own
m.showAll();
return 0;
}Output:
ID: 501, Basic: 60000
Allowance: 15000, Salary: 750006. Single Inheritance with Private Derivation
With private derivation, the base class's public functions become private in the derived class. Objects of the derived class can no longer call them directly; the derived class must call them inside its own member functions.
#include <iostream>
using namespace std;
class B {
int a; // private: not inheritable
public:
int b; // public: ready for inheritance
void getAB() {
a = 5;
b = 10;
}
int getA() {
return a;
}
void showA() {
cout << "a = " << a << endl;
}
};
class D : private B { // private derivation
int c;
public:
void mul() {
getAB(); // OK inside D: getAB is private here
c = b * getA();
}
void display() {
showA();
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
};
int main() {
D d;
// d.getAB(); // error: getAB() is private in D
d.mul();
d.display();
return 0;
}Output:
a = 5
b = 10
c = 507. What Is Not Inherited
A derived class does not inherit:
- Constructors and destructors (but they are called automatically; Lesson 24);
- the assignment operator
operator=(a default one is generated for the derived class); - friend functions and friend classes of the base (friendship is not inherited);
- access to the base's private members (they are present in the object but not accessible).
8. Making a Private Member Inheritable
There are two ways to let a derived class use a base data member:
| Way | Effect |
|---|---|
Make it public | the derived class can use it, but so can everybody: data hiding is lost |
Make it protected | the derived class can use it, outside code still cannot: preferred |
Key points to remember
- Syntax:
class D : public B { ... };; the default mode for a class is private. - Base private members are never accessible in the derived class; use
protectedfor members derived classes need. - Public derivation keeps public/protected as they are; protected derivation makes both protected; private derivation makes both private.
- Constructors, destructors,
operator=and friendships are not inherited. - Use public inheritance for "is-a" relationships.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is inheritance? Explain the visibility of inherited members under public, private and protected derivation. (10 Marks)
- Write: the definition and benefits, the syntax, the 3 × 3 visibility table, and the
Base/PubDerived/ProDerived/PriDerivedprogram. - Q2. What is the
protectedaccess specifier? How is it different fromprivate? (5 Marks) - Write: the access table and when to use protected.
- Q3. Write a program for single inheritance using private derivation. (5 Marks)
- Write: the
B/Dprogram; explain whyd.getAB()is an error. - Q4. What is not inherited by a derived class? (2.5 Marks)
- Q5. Give the advantages of inheritance. (2.5 Marks)