Inheritance and derivation
If you hear this name for the first time, you should first think about the meaning of these two words. Let’s talk about inheritance first. This is similar to the meaning of inheritance in real life. For example, one person inherits another person’s property, and a son inherits his father’s inheritance. The process of ownership is called inheritance. Similarly, in C++, for example, if there are two classes, the new class has all the attributes of the original class, which is called inheritance ! The process of generating a new class from an original class is called derivation ! And we call the original class the parent class or base class , and the class derived from the base class is called a derived class or a subclass . Everyone can tell their relationship from their names.
So what are the benefits of inheritance and derivation? Why does C++ have this mechanism?
1. Reflect object-oriented programming ideas and better express the relationship between various types.
2. In addition to inheriting all the information of the base class, the derived class can also add its own different and differentiated information. Just like the principle of biological evolution, the derived class will update on the basis of having all the information of the base class. powerful.
3. The members inherited from the derived class to the base class are automatically and hiddenly owned, that is, we do not need to redefine them. This saves a lot of code and embodies the software engineering idea of code reuse.
Next, let's look at the implementation of the actual code, how to inherit and implement derived classes:
/************************************//Des: C++ tutorial demo//Author: Huang//Copyright:www.dotcpp.com//Date:2018/6/20********************************** *******/#include<iostream>usingnamespacestd;classClock{private:intH;intM;intS;public:intSetTime(inth,intm,ints){this->H=h;this->M=m; this->S=s;return0;}intShowTime(){cout<<Now:<<H<<:<<M<<:<<S<<endl;return0;}};classAlarmClock:publicClock{private:intAH ;intAM;public:intSetAlarm(intAH,intAM){this->AH=AH;this->AM=AM;return0;}intShowAlarm(){cout<<AlarmTime:<<AH<<:<<AM<<endl ;return0;}};intmain(){AlarmClockA;A.SetTime(19,15,50);A.ShowTime();A.SetAlarm(5,30);A.ShowAlarm();return0;}
The running screenshot is as follows:
Please read and understand the code. Clock is the base class and AlarmClock is the derived class. Pay attention to the statement defining the derived class:
classAlarmClock:publicClock
Inheritance is represented by a colon , where public represents public inheritance, private inheritance and protected inheritance, which we will expand on later. In the code, we define object A of the derived class AlarmClock in the main function, and then we can call the SetTime and ShowTime methods from the base class and use them the same as our own. At the same time, we have added and defined our own SetAlarm and ShowAlarm methods in the derived class. Please understand the code and experiment on the computer.
So far, we have introduced the basic concepts of inheritance and derivation. After understanding this, you need to experiment with coding on a computer to understand under what circumstances two classes that have commonalities and are related can have this kind of relationship, and weigh their use.