In order not to blur the concept here we will simply elaborate on the decomposition of the class, in the previous tutorial we focused on the inheritance of the class, which is characterized by the inheritance of the attributes of the base class, the expansion of the structure, the gradual expansion of the process of decomposing the different characteristics of each other in the derived classes is actually the decomposition of the class.
Decomposition process I do not want to take the code to do too much elaboration analysis, meaning that, for the gradual decomposition of the idea of gradual expansion on the thought of everyone on their own.
Think of the procedure in front of the vehicle class, vehicles derived from vehicles, aircraft, are more specific characteristics of the description of the class, and for the transport of a base class, its characteristics are vague, extensive, if the establishment of a vehicle class objects and has no practical significance, In order to constrain the class which is not necessary to establish the object, C + + introduces the attribute of abstract class, and the constraint control of abstract class comes from the definition of pure virtual function.
Life a member function of a class is a pure virtual function that makes C + + aware that the function is meaningless, and its role is simply to hold the location of the virtual function overload for the derived class.
The definition of a pure virtual function is to add the "= 0" tag after the declaration of the member function of the class, and once the class has the definition of a pure virtual function, it is no longer possible to create objects of this class, and we call this class an abstract class.
The sample code for the abstract class is as follows:
Program Author: Junning
Site: www.cndev-lab.com
All manuscripts have copyright, if you want to reprint, please be sure to indicate the 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 ()//derived class member function overload
{
cout<<speed<< "|" <<total<< "|" <<aird<<endl;
}
Protected
int Aird;
};
int main ()
{
Vehicle A (100,4);//error, abstract class cannot create object
Car B (250,150,4);
B.showmember ();
System ("pause");
}