Inheritance In C++

Inheritance means getting something from the parent. In C++ also, inheritance means the same. But who is the parent?
 
Remember that classes and objects are a key feature in C++. When we talk of inheritance in C++, we talk of classes. We will have the parent class and the derived class. The derived class inherits all properties of the parent class.
 
The parent class is known as the base class. All the classes that arise from the base class are known as derived classes. These derived classes inherit all non-private parts of the base class.
 
When any data is made private in a class, it can be accessed ONLY by member functions of that class. A derived class CANNOT access the private area of its base class.
 
Let’s take a look at the syntax for making a derived class.
 
Syntax :
 
The syntax is easy. Just create the base class as you normally would. Then when you want to create the derived class, just start with the following line:
 
class d1 : public b1
{
body of d1;
};
 
where d1 is the name of the derived class and b1 is the name of the base class.
 
 
Out put of the program
 
 
We think you might have just one question. What’s the meaning of protected? Well, as We said earlier, private data of a base class cannot be inherited by it’s derived class.
 
So the question arises as to how to make some data available to both the base and derived class?
 
The answer is to make use of protected keyword.
 
Protected data will be inherited by the derived classes.
 
The rest of the program is as normal.
Scroll to Top