Inheritance in C++

One of the main goals of OOP is the recycling of the classes that you already created. It saves a lot of you time and efforts and makes your code to be more efficient. One of the way to re-use your classes is inheritance. The inheritance is an important concept of OOP. That's why everyone has to understand this concept. Let's start from an example. We have a class man that has that has the following structure
  1. #include <iostream>
  2. using namespace std;
  3. class man
  4. {
  5. public:
  6.     man(string _name, int _age, string _sex);
  7.     show_man();
  8. private:
  9.     string name;
  10.     int age;
  11.     string sex;
  12. };
And you need a class student, that must have the following variables:
  1. string university;
  2. double mark;
  3. string profession;
In this case you have to choose: create a new class student or create a derived class. Use of the inheritance allows you to exclude duplication of code in your program. To declare a derived class you have to use this form of declaration of class: class derived_class: access_specifier base_class In our case we will act in the next way:
  1. class student : public man
  2. {
  3. public:
  4.     student(string _name, int _age, string _sex,string _university,double _mark,string _profession);
  5.     show_student();
  6. private:
  7.     string university;
  8.     double mark;
  9.     string profession;
  10. }
When you derive a class from base class, private elements of base class are not available for the derived class. You can access them only through interface functions (set and get functions). table of access modes in different types of inheritance: acces And now we will implement our classes man and student to see how the inheritance can help you to avoid duplication of your code. The man implementation is
  1. man::man(string _name, int _age, string _sex)
  2. {
  3.     name = _name;
  4.     age = _age;
  5.     sex = _sex;
  6. }
  7. int man::show_man()
  8. {
  9.     cout << "1. Name " << name << endl;
  10.     cout << "2. Age " << age << endl;
  11.     cout << "3. Sex " << sex << endl;
  12.     return 0;
  13. }
it's really simple. The most interesting thing is the implementation of the student class:
  1. student::student(string _name, int _age, string _sex,string _university,double _mark,string _profession):man(_name, _age,_sex)
  2. {
  3.     university = _university;
  4.     mark = _mark;
  5.     profession = _profession;
  6. }
  7. int student::show_student()
  8. {
  9.     show_man();
  10.     cout << "4. From " << university<<endl;
  11.     cout << "5. Mark " << mark <<endl;
  12.     cout << "6. Profession " << profession << endl;
  13. }
The first line is a good example of the use of inheritance. Here
student::student(string _name, int _age, string _sex,string _university,double _mark,string _profession):man(_name, _age,_sex)
I call the constructor of base class from the constructor of the derived class. In the common view you use the next construction:  deriverd-class-c-tor(param1,param2,...,paramK,..paramn):base-class-c-tor(param1,param2,...,paramK) where parameters from #1 to K are common variables of these 2 classes. You can use these classes in your program in this way:
  1. int main()
  2. {
  3.     man man1("Peter",27,"M");
  4.     cout << "Data of man1" << endl;
  5.     man1.show_man();
  6.     student student1("John",21,"M","State University of Berlin",8.25,"Engineer");
  7.     cout << "Data of student1" << endl;
  8.     student1.show_student();
  9.     return 0;
  10. }
If you want to use a function from base class in the derived class, you can simply call it by it's name:
  1. int student::show_student()
  2. {
  3.     show_man();
  4.     //other code
  5. }
Sometimes, the derived and the base class can have the same names of any functions. In this case a conflict between namespaces can appear. For example, We can name the show_man() and  show_student with one name show_info() In this case we need to use  :: operator. It will look like this:
  1. int student::show_info()
  2. {
  3.     man::show_info();
  4.     //other code
  5. }
Another important moment is that a class can be derived not only from one base class.If you need to create a class, that contains variables from a number of other classes, you can do it in the following way: class derived_class: access_specifier1 base_class1,access_specifier2 base_class2,..,access_specifierN base_classN For example, you have next classes : wheel, engine, carcass. You need to create class car. You can do it in the following way:  class car : public wheel,public engine,protected carcass As a result, the described methods can be used to build a big hierarchy of classes and can be used everywhere.

Add new comment