Min menu

Pages

VB

Inheritance in C-Plus Plus | C++

 


 

The concept of heredity in C Plus Plus   

In the beginning, the word  inherit  means to include the content of one class in another class.
in C++, a class can inherit from another class in order to obtain the functions and variables in it.

So the idea of ​​heredity is simple, but its usefulness is very strong. For example, if you want to create a new class and notice that there is a ready-made class that contains codes that may be useful to you, you can use them instead of writing them from scratch, that is, you can make the class that you defined inherit this class, and then you can use all the variables and functions that the new class inherited from the class ready made.

The concept of inheritance in C Plus Plus In the beginning, the word inheritance means to include the content of a class in another class. In C++, a class can inherit from another class to get the functions and variables in it. So the idea of ​​heredity is simple, but its usefulness is very strong. For example, if you want to create a new class and you notice that there is a ready-made class that contains codes that may be useful to you, you can use it instead of writing it from scratch, that is, you can make the class that you defined inherit this class, and then you can use all the variables and functions that the new class inherited from the class Ready-made.

technical terms

  • heredity is called Inheritance in English.

  • The class that inherits from another class is called the son class, and it is called Subclass And it is also called Derived ClassExtended Class or Child Class ).

  • The class that bequeaths its contents to another class is called the father class, and it is called Superclass And it is also called Base Class or Parent Class ).

Forms of Inheritance in C-Plus   

in C++ There are  4  forms of inheritance as in the following table.

Forms of Inheritance in C Plus Plus


So forms of inheritance in C++ They are as follows:

  • Single Inheritance:  It means a class that inherits from only one class.

  • Successive Inheritance:  It means a class that inherits from one class, and this class was originally inherited from another class.

  • Hierarchical inheritance:  means that a class is inherited by more than one class.

  • Multiple inheritance:  means that a class inherits from more than one class.

How to make a class inherit from another class in C++

To make a class inherit from another class, we follow the following method.

class  derived-class  :  access-specifier  base-class
{

}

  • derived-class :   means the name of the son-class.

  • base-class :   means the name of the father-class.

  • access-specifier :   It is intended to specify how to access the things that are included in the father class in the son class.


So, in principle, to make the class inherit from another class, we put two dots after the class name on top of each other, then the name of the class that we want it to inherit from.
The place of the word  access-specifier  You can put  public or  private or  protected not put any of them and then it will be considered that you put the word private.

What are the access words? C++

When you make a class inherit from another class you can use one of the access words (Access Specifier ) To determine how the things that the son's class inherited from the father's class can be accessed.

What are C++ keywords? When you inherit the class from another class, you can use one of the access words (Access Specifier) ​​to specify how to access the things that the child class inherited from the parent class.

What is the word  public in C++

It is used to specify that the things that the son's class will inherit from the father's class will be accessible from anywhere.

Example:  class B : public A
Here anything that the class inherits  B from the class  A will be considered as defined  public in the class B.


What is the word public in C++? Used to specify that the things that the child class inherits from the parent class will be accessible from anywhere. Example: class B: public A Here anything that class B inherits from class A will be considered as being defined public in class B.

What is the word  private in C++

It is used to specify that things that the son's class will inherit from the father's class can only be accessed from the same class.

What is private in C++ Used to specify that the things that the son class inherits from the parent class can only be accessed from the same class.

Example:  class B : private A
Here anything that the class inherits  B from the class  A will be considered as defined  private in the class B.



Explanation of the word  protected in C++

It is used to specify that the things that the son's class will inherit from the father's class can be accessed from the same class and from any other class he inherits.

Example:  class B : protected A
Here anything that the class inherits  B from the class  A will be considered as defined  protected in the class B.

Explanation of the word protected in C++ It is used to determine that the things that the son class inherits from the parent class can be accessed from the same class and from any other class it inherits. Example: class B: protected A Here anything that class B inherits from class A will be considered protected as protected in class B.

Applied Examples of Inheritance in C++


Example of haploid inheritance in C++

In the following example, we have defined a class whose name  A contains a variable named  x and a function named printMessage().
Then we created a class named  B it contains a variable named  y and inherits from the class A.

The form of Inheritance will be as follows.

Example of single inheritance in C++

The first example of individual Inheritance in C++

main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // printMessage and a function named x contains a variable named A. Here we have defined a class named
		  class A {
	  
		  public:
		  int x = 10;
	  
		  void printMessage()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // y contains a variable named A that inherits from class B. Here we have defined a class named
		  class B: public A {
	  
		  public:
		  int y = 20;
	  
		  };
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // b named B here we created an object from the class
		  B b;
	  
		  // B which is defined in class b in object y here we print the value of the variable
		  cout << "y = " << by << "\n";
	  
		  // A from class B inherited from class b in object x here we print the value of the variable
		  cout << "x = " << bx << "\n";
	  
		  // A from class B inherited from class b in the printMessage() object here we called the function
		  b.printMessage();
	  
		  return 0;
		  }
	

We will get the following result when running.

                    y = 20
		  x = 10
		  Hello from class A
	


Example of consecutive Inheritance in C++

In the following example, we have defined a class whose name  A contains a function whose name is printA().
Then we created a class whose name  B contains a function whose name  printB() it inherits from the class A.
Then we created a class whose name  C contains a function whose name  printC() it inherits from the class B.

The form of inheritance will be as follows.

Example of Cascading Inheritance in C++

The second example of successive Inheritance in C++

main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // printA contains a function named A. Here we have defined a class whose name is
		  class A {
	  
		  public:
		  void printA()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // printB contains a function named A that inherits from class B. Here we have defined a class named
		  class B: public A {
	  
		  public:
		  void printB()
		  {
		  cout << "Hello from class B \n";
		  }
	  
		  };
	  
		  // printC contains a function named B that inherits from class C. Here we have defined a class whose name is
		  class C: public B {
	  
		  public:
		  void printC()
		  {
		  cout << "Hello from class C \n";
		  }
	  
		  };
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // c named C here we created an object from the class
		  C c;
	  
		  // c Here we have called all the functions in the object
		  c.printA();
		  c.printB();
		  c.printC();
	  
		  return 0;
		  }
	

We will get the following result when running.

                    Hello from class A
		  Hello from class B
		  Hello from class C
	


Example of Multiple Inheritance in C++

In the following example, we have defined a class whose name  A contains a function whose name is printA().
Then we created a class named after  B a function named printB().
Then we created a class whose name  C contains a function whose name  printC() it inherits from the class  A and the class B.

The form of inheritance will be as follows.

Example of multiple inheritance in C++

The third example of multiple inheritance in C++

main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // printA contains a function named A. Here we have defined a class whose name is
		  class A {
	  
		  public:
		  void printA()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // printB contains a function named B. Here we have defined a class whose name is
		  class B {
	  
		  public:
		  void printB()
		  {
		  cout << "Hello from class B \n";
		  }
	  
		  };
	  
		  // printC contains a function named B that inherits from class A, inherits from class C, here we have defined a class named
		  class C: public A, public B {
	  
		  public:
		  void printC()
		  {
		  cout << "Hello from class C \n";
		  }
	  
		  };
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // c named C here we created an object from the class
		  C c;
	  
		  // c Here we have called all the functions in the object
		  c.printA();
		  c.printB();
		  c.printC();
	  
		  return 0;
		  }
	

We will get the following result when running.

                    Hello from class A
		  Hello from class B
		  Hello from class C
	


Example of Hierarchical Inheritance in C++

In the following example, we have defined a class whose name  A contains a function whose name is printA().
Then we created a class whose name  B contains a function whose name  printB() it inherits from the class A.
Then we created a class whose name  C contains a function whose name  is also printC() inherited from the class  A .

The form of inheritance will be as follows.

Example of hierarchical inheritance in C++

Fourth example of hierarchical inheritance in C++

main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // printA contains a function named A. Here we have defined a class whose name is
		  class A {
	  
		  public:
		  void printA()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // printB contains a function named A that inherits from class B. Here we have defined a class named
		  class B : public A {
	  
		  public:
		  void printB()
		  {
		  cout << "Hello from class B \n";
		  }
	  
		  };
	  
		  // printC contains a function named A that inherits from class C. Here we have defined a class named
		  class C: public A {
	  
		  public:
		  void printC()
		  {
		  cout << "Hello from class C \n";
		  }
	  
		  };
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // and call all the functions in it b named b here we have created an object of class
		  B b;
		  b.printA();
		  b.printB();
	  
		  // and call all the functions in it c named C here we created an object of class
		  C c;
		  c.printA();
		  c.printC();
	  
		  return 0;
		  }
	

We will get the following result when running.

                    Hello from class A
		  Hello from class B
		  Hello from class A
		  Hello from class C
	

Multiple Inheritance Problem in C++

When a class inherits more than one class at the same time, there is a high possibility that it will inherit properties and functions that have the same name, which leads to a name conflict, so you find that the compiler does not show you an error when writing the code, but rather an error appears when you run the code.


In the following example, we have defined a class whose name  A contains a function whose name is printMessage(), And its name class  B contains its name function  printMessage() as well.
Then we created a class named  C inherit from the class  A and the class B.

So here the class  C inherits a function whose name  printMessage() is from the class  A and a function whose name  printMessage() is from the class B, And the form of inheritance is as follows.

C++ Multiple Inheritance Problem

Logically, we will have a problem when trying to call the function  printMessage() from the class  C because the compiler will not know whether we want to call the function that it inherited from the class  A or the one it inherited from the class B.

An example of a multiple inheritance problem in C++

main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // printMessage contains a function named A. Here we have defined a class whose name is
		  class A {
	  
		  public:
		  void printMessage()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // printMessage contains a function named B. Here we have defined a class named B
		  class B {
	  
		  public:
		  void printMessage()
		  {
		  cout << "Hello from class A \n";
		  }
	  
		  };
	  
		  // B and class A inherit from class C. Here we have defined a class named
		  class C : public A, public B {
	  
		  };
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // c named C here we created an object from the class
		  C c;
	  
		  // which has two functions with this name c from the printMessage() object here we called the function
		  c.printMessage();
	  
		  return 0;
		  }
	

The following error will appear when running.

error: request for member 'printMessage' is ambiguous|

In the end, you should be very careful if you intend to create a class that inherits from more than one class at the same time so as not to run into this problem.