In C++, one of the most important parts of object-oriented programming is inheritance.
It allows us to make a new class from an existing class. This is called a “derived class
” (a base class).
The derived class has the same features as the base class and can also have its own features.
In this tutorial, we will learn what inheritance in C++ is and why it is important to learn and master.
In this article, I also provide the best examples to make your learning easy and fast.
What is Inheritance in C++?
Inheritance is a way to reuse
and add to existing classes without changing them.
This creates a hierarchy between the classes. Inheritance is similar to putting an object inside of a class.
For Example:
class Animal {
// eat() function
// sleep() function
};
class Cat : public Animal {
// Meow() function
};
The Cat
class is a subclass of the Animal
class. Since Cat is a subclass of Animal, Cat can access members of Animal.
Notice how the word “public
” is used when Cat inherits from Animal.
class Cat : public Animal {...};
Instead of public
, we can also use the words private
and protected
. We will find out later in this tutorial what the differences are between private, public, and protected.
C++ Aggregation (HAS-A Relationship)
In C++, aggregation is when one class defines another class as any entity reference.
This is another way to reuse the class. It’s a form of association that shows a HAS-A relationship
.
Inheritance is a case of an is-a relationship. We use inheritance only if there is an is-a relationship between the two classes.
Here are some examples:
- A taxi is a vehicle.
- Orange is a color.
- A dermatologist is a doctor.
- A cat is an animal.
C++ program to demonstrate inheritance
// C++ program to demonstrate inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Cat : public Animal {
public:
void Meow() {
cout << "I can Meow! Meow Meow!!" << endl;
}
};
int main() {
// Create object of the Cat class
Cat cat1;
// Calling members of the base class
cat1.eat();
Output:
I can eat!
I can sleep!
I can Meow! Meow Meow!!
Here, cat1
, which is an object of the derived class Cat
, can use the base class Animal’s features.
Cat
came from Animal
, so it has the same traits.
// Calling members of the Animal class
cat1.eat();
cat1.sleep();#include <iostream>
using namespace std;
class Base {
protected :
int number = 26;
};
class Derived : public Base {
public :
void func() {
cout << "The value of num is: " << number;
}
};
int main() {
Derived obj;
obj.func();
return 0;
}
Accessing protected members in a C++ derived class
In C++, a class has public
, private
, and protected
sections that hold the members of that class.
Like private members, protected members of a class can’t be accessed from outside the class.
But derived classes or child classes can access them, while private members can’t.
The following is a C++ program that shows how to get to protected
data members in a derived class.
Example:
#include <iostream>
using namespace std;
class Base {
protected :
int number = 26;
};
class Derived : public Base {
public :
void func() {
cout << "The value of num is: " << number;
}
};
int main() {
Derived obj;
obj.func();
return 0;
}
Output:
The value of num is: 26
Now, let’s figure out what the above program means.
The protected or private data member of the class Base is number
. The class Base is passed down to the class Derived.
The value of a number is printed by the function func()
. Here is a piece of code that shows how to do this.
class Base {
protected :
int number = 26;
};
class Derived : public Base {
public :
void func() {
cout << "The value of num is: " <<< number;
}
};
The object obj of class Derived is made in the function main(). Then it calls the function func(). Here is a small piece of code that shows how to do this.
int main() {
Derived obj;
obj.func();
return 0;
}
Access Modes in C++ Inheritance
So far, we’ve used the public void keyword to make a class that inherits properties from a base class member that already existed.
We can also inherit classes by using private and protected keywords.
Example:
class Animal {
// code
};
class Cat : private Animal {
// code
};
class Fish : protected Animal {
// code
};
The different ways we can get to classes are called “access modes
.” These access modes have the following effects:
Below is a list of the 3 ways of access modes:
- public – If a derived class is declared in public mode, the members of the base class are inherited exactly as they are.
- private – In this situation, all the members of the base class become private members of the derived class.
- protected – In the derived class, the public members of the base class become protected members.
The derived class always keeps the private members of the base class.
Member Function Overriding in Inheritance
Function overriding is a feature that lets us have the same function in a child class that is already present in the parent class.
A child class inherits the data members and member functions of its parent class.
However, you can use function overriding to change the way a function works in a child class.
What are the 5 types of inheritance?
Inheritance in C++ is mostly made up of five types. Here’s what they are:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Why do we use inheritance?
With inheritance, we can define a class in terms of another class, which makes it easier to create and maintain an application.
This also lets you reuse code functionality and is easy to set up.
What is the importance of inheritance?
In Object-Oriented Programming, inheritance is one of the most important concepts (OOP).
The key to understanding inheritance is it allows code to be used more than once.
Instead of writing the same code over and over, we can simply copy the properties of one class into another.
What is the syntax of inheritance of class?
First should be the keyword class, then the name of the derived class.
A colon is followed by the access from which the base class must be derived and then the name of the base class, which will return 0.
What is inheritance in OOP?
In OOP, inheritance is when one class is based on another class.
All of the public and protected properties and methods of the parent class will be passed on to the child class.
It can also have its own properties and ways of doing things. The extends keyword is used to define a class that is based on another class.
Summary
This article discusses C++ Inheritance.
It also tackles protected members, access modes, member function overriding, types of inheritance, use of inheritance, the importance of inheritance, syntax of inheritance, and inheritance in OOP.
I hope this lesson has helped you learn a lot.
Check out my previous and latest articles for more life-changing tutorials which could help you a lot.