Details of the inheritance and derivation instances of C++ class

  • 2020-05-26 09:40:30
  • OfStack

Examples of inheritance and derivation of the C++ class

Inheritance is one of the most important features of object-oriented programming, which makes software reusable. C++ provides a class inheritance mechanism.

The concept of inheritance and derivation

When a new class acquires an existing property from an existing class, this phenomenon is called class inheritance. It can also be said that a new class is derived from an existing class. Class A inherits class B which is class B and derives class A. So the relation between inheritance and derivation is like the relation between the word and the word in elementary school. When you have inheritance and derivation, you have the parent class/base class and the subclass/derived class. C++ calls class B the parent class/base class and class A the subclass/derived class.

Declaration of derived class:


#include <iostream>

using namespace std;

class Student  // Declaration of the base class 
{
public:
  Student()
  {
    num = 1;
    name = 'Z';
    sex = 'm';
  }
  void myshow()
  {
    cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl;
  }
private:
  int num;
  char name;
  char sex;
};
class Student1:public Student // Declaration of a derived class 
{
public:
  Student1()
  {
   age = 16;
  }
  void myshow1()
  {
   myshow();
   cout<<"age:"<<age<<endl;
  }
private:
  int age;
};
int main()
{
  Student stu;
  stu.myshow();
  Student1 stu1;
  stu1.myshow1();
  getchar();
  return 0;
}

Operation results:


num:1 
name:Z 
sex:m 
age:16 
num:1 
name:Z 
sex:m

From the above code, we can see the form and characteristics of class inheritance:

1. Class inheritance is in the form of class Student1:public Student{}, Student is the base class, Student1 is the derived class, and public (public inheritance) is the mode of inheritance.

2. Derived classes need to receive all the members of the base class (excluding constructors and destructors), not just part 1.

3. Class inheritance methods include: public (public inheritance), private (private inheritance), and protected (protected), and the different inheritance methods will affect the access properties of the members of the derived class.

Public inheritance

If common inheritance is adopted, the access properties in the base class remain unchanged in the derived class, that is:

Private properties in a base class - private properties in a derived class

Common properties in base classes - properties Shared by derived classes

Protected properties in a base class - protected properties in a derived class


class Student  // Declaration of the base class 
{
public:
  Student()
  {
    num = 1;
    name = 'Z';
    sex = 'm';
  }
  void myshow()
  {
    cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl;
  }
private:
  int num;
  char name;
  char sex;
};
class Student1:public Student // Declaration of a derived class 
{
public:
  Student1()
  {
   age = 16;
  }
  void myshow1()
  {
   myshow();
   cout<<"age:"<<age<<endl;
  }
private:
  int age;
};

In this example, the private member of the derived class Student1 is:


  int num;
  char name;
  char sex;
  int age;

The co-members are:


 myshow();
  myshow1();

Private inheritance

If private inheritance is used, the access properties in the base class change in the derived class as follows:

Private properties in a base class - not accessible in a derived class

Common properties in base classes - private properties in derived classes

Protected properties in a base class - private properties in a derived class


class Student  // Declaration of the base class 
{
public:
  Student()
  {
    num = 1;
    name = 'Z';
    sex = 'm';
  }
  void myshow()
  {
    cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl;
  }
private:
  int num;
  char name;
  char sex;
};
class Student1:private Student // Declaration of a derived class 
{
public:
  Student1()
  {
   age = 16;
  }
  void myshow1()
  {
   myshow();
   cout<<"age:"<<age<<endl;
  }
private:
  int age;
};

In this example, the private member of the derived class Student1 is:


 int age;// A subclass of new 
  myshow();// Inherits from the parent class 

The co-members are:


  myshow1(); // A subclass of new 

Subclass not accessible:


  int num;
  char name;
  char sex;

Protect the inheritance

Protected members cannot be accessed outside the class. This 1 point is similar to a private member and can be considered private to the user of the class. However, there is one difference from private members: protected members can be referenced by member functions of derived classes.

If protected inheritance is used, the access property in the base class changes in the derived class as follows:

Private properties in a base class - not accessible in a derived class

Common properties in base classes - protected properties in derived classes

Protected properties in a base class - protected properties in a derived class


class Student
{
public:
  Student()
  {
    num = 1;
    name = 'Z';
    sex = 'm';
  }
 protected:
  void myshow()
  {
    cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl;
  }
private:
  int num;
  char name;
  char sex;
};
class Student1:protected Student
{
public:
  Student1()
  {
    age = 16;
  }
  void myshow1()
  {
    cout<<"age:"<<age<<endl;
  }
private:
  int age;
};

As shown in the above example (of course, this example does not make any sense, since 1 class data is private, the protected function has no interface with the outside world, which is just an example).

In this example, the private member of the derived class Student1 is:


num:1 
name:Z 
sex:m 
age:16 
num:1 
name:Z 
sex:m
0

The co-members are:


num:1 
name:Z 
sex:m 
age:16 
num:1 
name:Z 
sex:m
1

The protected members are:


num:1 
name:Z 
sex:m 
age:16 
num:1 
name:Z 
sex:m
2

Subclass not accessible:


  int num;
  char name;
  char sex;

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: