Pure virtual function and abstract class of C + + Foundation
Introduction
The position of pure virtual function in C + + programming is very important, it is related to the concept of "interface" in design pattern.
Grammar
Syntax for pure virtual functions:
1. Declare the member function as virtual
2, after adding = 0
3. The function does not have a function body
1 Class < category name >2 {3 virtual0; 4 ... .. 5 };
For example:
1 class Cmdhandler 2 {3 Virtual void OnCommand (char0; 4 ... .. 5 };
In many cases, there is no meaningful implementation of the virtual function in the base class, but it is described as pure virtual function, and its implementation is left to the derived class of the base class to do. This is the function of pure virtual functions.
Abstract class
The class containing pure virtual function is called abstract class (Pure virtual Class), abstract class is a special class, it is established for abstract and design purposes, it is in the upper layer of inheritance hierarchy.
An abstract class cannot be instantiated, that is, an object of that class cannot be created.
Cmdhandler ch; Compile Error!!
Cmdhandler *p = new Cmdhandler (); Compile Error!!
In practice, in order to emphasize that a class is an abstract class, the constructor of the class can be described as a protected access control permission.
The primary function of an abstract class is to associate the organization in an inheritance hierarchy by providing it with a common root, derived from the root of the dependent subclass.
Abstract classes describe the general semantics of the operation interfaces of a set of subclasses, which are also passed to subclasses. In general, abstract classes describe only the common operating interfaces of this set of subclasses, and the complete implementation is left to subclasses.
Abstract classes can only be used as base classes, and implementations of their pure virtual functions are given by derived classes. If a derived class does not redefine a pure virtual function, and the derived class simply inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of the base class pure virtual function is given in the derived class, then the derived class is no longer an abstract class, it is a concrete class that can be used to create objects.
Interface
Practical use: act as an "interface function"
(equivalent to interface syntax in Java)
(used to replace the use of callback functions in C)
Interface Specification: Any class that follows this specification must implement the specified function interface, usually a series of interfaces.
The abstract class defined above can be understood as: any class that follows the Cmdhandler specification must implement the specified function interface: OnCommand ().
An instance
Requirements: The user enters a line of commands, press ENTER to complete the input. Requires parsing the input of the command and processing it.
Design :
Cmdinput: Input for receiving users
Cmdhandler: Specifies a range of function interfaces
Cmdparser: Implementation of interfaces, actual processing classes for parsing
Description :
Visible, cmdinput only receive input content, and Cmdparser used to parse the input content, two classes each do each, do not interfere, the two do not know the other side of the existence, but through the abstract class Cmdhandler act as an "interface" to connect.
Code :
//main.cpp#include "CmdInput.h" #include "CmdParser.h "intMainvoid) {Cmdinput cinput; Cmdparser Cparser; Cinput.sethandler (&cparser); Cinput.run (); return 0;} //CmdHandler.h/*Cmdhandler Interface Class*/classcmdhandler{ Public: Virtual~cmdhandler () {}//destructors declared as virtual Virtual voidOnCommand (Char* CmdLine) =0;//Pure virtual function};//CmdInput.h#include "CmdHandler.h"classcmdinput{ Public: Cmdinput (); voidSetHandler (cmdhandler*Pchandler); intRun ();Private: Cmdhandler*M_pchandler;}; //CmdInput.cpp#include "CmdInput.h" Cmdinput::cmdinput () {M_pchandler=NULL;}voidCmdinput:: SetHandler (cmdhandler*Pchandler) {M_pchandler=Pchandler;}intCmdinput::run () {Charcmdline[ the]; memset (CmdLine,0, the); while(1) {printf (">");//inputgets (cmdline); if(strcmp (CmdLine,"Exit") ==0)//Exit { Break; } if(M_handler)//parsing and execution{M_handler-OnCommand (cmdline); } } return 0;}//CmdParser.h#include "CmdHandler.h"/*Myparser: A class that follows the Cmdhandler interface*/classMyparser: Publiccmdhandler{ Public: Myparser (); Public: Virtual voidOnCommand (Char* cmdline);//function Interface SetPrivate: intSplit (CharText[],Char* parts[]);//Parse Command};//CmdParser.cpp#include <stdio.h>#include "CmdParser.h" Cmdparser::cmdparser () {}voidCmdparser::oncommand (Char*cmdline) { Char* argv[ -]; intARGC =Split (CMDLINE,ARGV); if(Argc >0) {printf ("command:%s \ n", argv[0]); printf ("Parameters:"); for(intI=1; i<argc; i++) {printf ("%s", Argv[i]); } printf ("\ n"); }} intCmdparser::split (CharText[],Char*parts[]) { intCount =0;//Number of segments intStart =0;//the first address of each segment intFlag =0;//traverse text to identify whether it is currently in a valid character intStop =0;//whether to reach the end for(intI=0;!stop; i++) { CharCH =Text[i]; if(ch = =0) Stop=1;//End Loop if(ch = =','|| ch = =' /'|| ch = =' '|| ch = ='\ t' ) { if(flag)//The delimiter is encountered and the current state is flag=1{flag=0; Text[i]=0;//Modify to Terminator, complete segmentParts[count] = text + start;//record the first addressCount + +; } } Else { if(!flag)//a valid character is encountered and the current state is flag=0{flag=1; Start=i; } } } returnCount//returns the number of segments}
Summary
1. Definition of pure virtual function
2, abstract class and its substantive role: interface specification, because it represents only a specification, and not specifically implemented, so it cannot be instantiated.
3, abstract classes are often multiple inheritance, for example, a common class, the implementation of multiple sets of interface specifications, but also inherited from the original parent class.
4. The destructor of the abstract class should be declared as virtual because it is involved in inheritance.
C + + Fundamentals (pure virtual functions and abstract classes)