The whole C + + programming is done around the object-oriented way, the inheritance of the class is a very very important mechanism of C + +, the inheritance attribute can make a new class get the operation and data structure of its parent class, the programmer just adds the component in the new class that is not in the original class.
It can be said that the content of this chapter is the key to C + + object-oriented programming.
Let's briefly say the concept of inheritance, first look at the following figure:
The image above is an abstract description of the attribute inheritance table
Transport is a base class (also known as the parent), usually all vehicles have the common characteristics of speed and rated the number of manned, but in accordance with the normal life, we continue to classify the transport, we will think of cars and aircraft, and so on, Cars and flying also have the same speed and rated the number of manned passengers such characteristics, and these characteristics are common to all vehicles, then when building cars and aircraft classes we do not need to define the existing data members of the base class, but only need to describe the car class and aircraft specific characteristics can be, The characteristics of aircraft and automobiles are based on the original characteristics of the vehicle type, then the aircraft class and car class is the vehicle class (also known as subclasses). And so on, as the layers increment, the notion that the seed class obtains the parent attribute is inherited.
Here's what we'll see in the diagram above, like the following code:
#include <iostream>
using namespace Std;
Class Vehicle
{
Public
void Editsc (float speed,int total);
Protected
Float speed;//Speed
int total;//Maximum manned capacity
};
void Vehicle::editsc (float speed,int total)
{
Vehicle::speed = speed;
Vehicle::total = total;
}
Class Car:public Vehicle//car classes inherit the attributes of Vehicle, the car class is a Vehicle derived class
{
Public
Car ()
{
Aird=0;
}
Protected
int aird;//Displacement
};
Class Plane:public Vehicle
{
Protected
Float wingspan;//Wing Exhibition
};
void Main ()
{
Car A;
A.EDITSC (150,4);
Cin.get ();
}
The definition of a derived class can be defined by adding a colon public null Geggaky class name after the class name, such as Class Car:public Vehicle in the code above.