Inheritance is a fundamental concept in object-oriented programming (OOP) that allows the creation of new classes based on existing classes. In C++, inheritance is achieved using the “extends” keyword. In this tutorial, we’ll take a closer look at C++ inheritance and provide easy-to-learn examples to illustrate the concept.
C++ Inheritance Tutorial: Understanding the Basics
Inheritance in C++ allows us to create a new class (derived class) from an existing class (base class) by inheriting its properties and methods. The derived class can then add its own properties and methods on top of those inherited from the base class.
For example, let’s say we have a base class called “Animal” that has properties such as “name” and “age”, and methods such as “eat” and “sleep”. We can then create a derived class called “Dog” that inherits all the properties and methods of the Animal class and adds its own methods such as “bark” and “fetch”.
To create a derived class in C++, we use the “class” keyword followed by the name of the derived class, then the “extends” keyword followed by the name of the base class. Here’s an example:
class Animal {
public:
string name;
int age;
void eat() {
cout << "Eating..." << endl;
}
void sleep() {
cout << "Sleeping..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Woof!" << endl;
}
void fetch() {
cout << "Fetching..." << endl;
}
};
In the above example, the Dog class extends the Animal class and adds its own methods “bark” and “fetch”.
Inheritance Types in C++
There are several types of inheritance in C++, including single inheritance, multiple inheritance, hierarchical inheritance, and multilevel inheritance. Let’s take a look at each one briefly:
- Single Inheritance: A derived class is derived from a single base class.
- Multiple Inheritance: A derived class is derived from multiple base classes.
- Hierarchical Inheritance: Multiple derived classes are derived from a single base class.
- Multilevel Inheritance: A derived class is derived from another derived class.
Here’s an example of multiple inheritances:
class A {
public:
void methodA() {
cout << "Method A" << endl;
}
};
class B {
public:
void methodB() {
cout << "Method B" << endl;
}
};
class C : public A, public B {
public:
void methodC() {
cout << "Method C" << endl;
}
};
In the above example, class C is derived from both class A and class B.
Benefits of Inheritance in OOP
Inheritance provides several benefits in OOP, including code reuse, modularity, and extensibility. By inheriting properties and methods from a base class, we can reuse existing code and reduce duplication. In addition, we can create modular code by dividing classes into smaller, more specialized classes. Finally, we can easily extend existing classes by creating derived classes that inherit their properties and methods.
Conclusion
Inheritance is an essential concept in OOP that allows us to create new classes based on existing classes. C++ inheritance provides several benefits, including code reuse, modularity, and extensibility. In this tutorial, we’ve covered the basics of C++ inheritance, including inheritance types and benefits. We’ve also provided easy-to-learn examples to illustrate the concept. With this knowledge, you can start using inheritance in your own C++ programs to create modular and reusable.
you can also learn about c++ programming in my blog.