First look at a piece of code:
Class Instrument{public:virtualvoid play () const=0//illegal definition defines a pure virtual function in an abstract class {cout<< "Instrument play\n";}}; Class Wind:public Instrument{void play () const{cout<< "Wind play\n";}; void Main () {wind s;instrument &p = S;p.play ();}
The following is the definition of a pure virtual function in an abstract class:
#include <iostream> #include <string>using namespace std;class instrument{public:virtual void play () const = 0;//Pure virtual function This abstract class virtual function pointer is empty //Can not implement pure virtual function inline, but can be implemented outside the class};//in the base class as the abstract class as the pure virtual function of its definition is possible, This allows some common code to call void instrument in some or all of the derived classes::p Lay () const{cout<< "instrument play () \ n";} /* The following methods can also be inline void instrument::p lay () const{cout<< "instrument play () \ n";} */class wind:public instrument{void Play () Const{instrument::p Lay ();cout<< "wind Play () \ n";}; void Main () {wind s;instrument &p = S;p.play ();}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Defining pure virtual functions in abstract classes