C++ Inheritance(Why inheritance should be used?)

C++ Inheritance


Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class(base class).
The derived class inherits all the features from the base class and can have additional features of its own.

Why inheritance should be used?

Suppose, in your game, you want three characters - a maths teacher, a footballer and a businessman.
Since, all of the characters are persons, they can walk and talk. However, they also have some special skills. A maths teacher can teach maths, a footballer can play football and a businessman can run a business.
You can individually create three classes who can walk, talk and perform their special skill as shown in the figure below.
Solving a problem without inheritance in C++
In each of the classes, you would be copying the same code for a walk and talk for each character.
If you want to add a new feature - eat, you need to implement the same code for each character. This can easily become error-prone (when copying) and duplicate codes.
Using inheritance, now you don't implement the same code for walk and talk for each class. You just need to inherit them.
So, for Maths teacher (derived class), you inherit all features of a Person (base class) and add a new feature TeachMaths. Likewise, for a footballer, you inherit all the features of a Person and add a new feature PlayFootball and so on.
This makes your code cleaner, understandable and extendable.
It is important to remember: When working with inheritance, each derived class should satisfy the condition whether it "is a" base class or not. In the example above, Maths teacher is a Person, Footballer is a Person. You cannot have: Businessman is a Business.

Implementation of Inheritance in C++ Programming

class Person 
{
  ... .. ...
};

class MathsTeacher : public Person 
{
  ... .. ...
};

class Footballer : public Person
{
  .... .. ...
};
In the above example, class Person is a base class and classes MathsTeacher and Footballer are the derived from Person.
The derived class appears with the declaration of a class followed by a colon, the keyword public and the name of base class from which it is derived.
Since, MathsTeacher and Footballer are derived from Person, all data member and member function of Person can be accessible from them.

Example: Inheritance in C++ Programming

Create game characters using the concept of inheritance.
  1. #include <iostream>
  2. using namespace std;
  3. class Person
  4. {
  5. public:
  6. string profession;
  7. int age;
  8. Person(): profession("unemployed"), age(16) { }
  9. void display()
  10. {
  11. cout << "My profession is: " << profession << endl;
  12. cout << "My age is: " << age << endl;
  13. walk();
  14. talk();
  15. }
  16. void walk() { cout << "I can walk." << endl; }
  17. void talk() { cout << "I can talk." << endl; }
  18. };
  19. // MathsTeacher class is derived from base class Person.
  20. class MathsTeacher : public Person
  21. {
  22. public:
  23. void teachMaths() { cout << "I can teach Maths." << endl; }
  24. };
  25. // Footballer class is derived from base class Person.
  26. class Footballer : public Person
  27. {
  28. public:
  29. void playFootball() { cout << "I can play Football." << endl; }
  30. };
  31. int main()
  32. {
  33. MathsTeacher teacher;
  34. teacher.profession = "Teacher";
  35. teacher.age = 23;
  36. teacher.display();
  37. teacher.teachMaths();
  38. Footballer footballer;
  39. footballer.profession = "Footballer";
  40. footballer.age = 19;
  41. footballer.display();
  42. footballer.playFootball();
  43. return 0;
  44. }
Output
My profession is: Teacher
My age is: 23
I can walk.
I can talk.
I can teach Maths.
My profession is: Footballer
My age is: 19
I can walk.
I can talk.
I can play Football.
In this program, Person is a base class, while MathsTeacher and Footballer are derived from Person.
Person class has two data members - profession and age. It also has two member functions - walk() and talk().
Both MathsTeacher and Footballer can access all data members and member functions of Person.
However, MathsTeacher and Footballer have their own member functions as well: teachMaths() and playFootball() respectively. These functions are only accessed by their own class.
In the main() function, a new MathsTeacher object teacher is created.
Since, it has access to Person's data members, profession and age of teacher is set. This data is displayed using the display() function defined in the Person class. Also, the teachMaths() function is called, defined in the MathsTeacher class.
Likewise, a new Footballer object footballer is also created. It has access to Person's data members as well, which is displayed by invoking the display() function. The playFootball() function only accessible by the footballer is called then after.

Access specifiers in Inheritance

When creating a derived class from a base class, you can use different access specifiers to inherit the data members of the base class.
These can be public, protected or private.
In the above example, the base class Person has been inherited public-ly by MathsTeacher and Footballer.
Learn more about Public, Protected and Private Inheritance in C++.

Member Function Overriding in Inheritance

Suppose, base class and derived class have member functions with same name and arguments.
If you create an object of the derived class and try to access that member function, the member function in derived class is only invoked.
The member function of derived class overrides the member function of base class.
It'd be a lot easier if we had a Person class with basic features like talk, walk, eat, sleep, and add special skills to those features as per our characters. This is done using inheritance.
Solving a problem with inheritance in C++

Comments

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website