Defining pure virtual functions in abstract classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.