In order not to blur the concept, we will briefly describe it hereClass DecompositionIn the previous tutorial, we focused on class inheritance. The Inheritance feature is that the derived class inherits the features of the base class and expands the structure, the process of gradually decomposing different features in each derived class is actually the decomposition of classes.
The decomposition process I don't want to elaborate on the code too much here. I mean, the idea of gradual decomposition and gradual expansion depends on everyone's own thinking.
Taking the procedures of transportation tools for consideration, the vehicle and aircraft derived from transportation tools are described with more specific characteristics, its features are fuzzy and extensive. If it is meaningless to create a Transportation Tool Object, c ++ introducesAbstract classAbstract class constraints are defined by pure virtual functions.
A member function of a class is a pure virtual function. It makes sense for c ++ to know that the function is meaningless. Its function is only to reload and retain the position of the virtual function for the derived class.
The definition of pure virtual functions is to add the "= 0" mark after the declaration of the class member functions, once a pure virtual function is defined in a class, this class can no longer create such objects. We call this class an abstract class.
The sample code of the abstract class is as follows:
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Using namespace std;
Class Vehicle
{
Public:
Vehicle (float speed, int total)
{
Vehicle: speed = speed;
Vehicle: total = total;
}
Virtual void ShowMember () = 0; // definition of pure virtual function
Protected:
Float speed;
Int total;
};
Class Car: public Vehicle
{
Public:
Car (int aird, float speed, int total): Vehicle (speed, total)
{
Car: aird = aird;
}
Virtual void ShowMember () // member function overload of the derived class
{
Cout <speed <"|" <total <"|" <aird <endl;
}
Protected:
Int aird;
};
Int main ()
{
// Vehicle a (); // error. The abstract class cannot create an object.
Car B (250,150, 4 );
B. ShowMember ();
System ("pause ");
}