C + + pure virtual function and abstract base class

Source: Internet
Author: User

In C + +, virtual functions can be very well implemented polymorphism, but there are certain limitations, this limitation is that the virtual function must be defined in the base class, even if it is an empty implementation.

For example, the following code compiles with an error:

Class base{virtual void print ();}; Class Derived:base{void print () {cout << "Derived" <<endl;}};

Because the virtual function in the base class, print () is not defined.

In the actual programming we often encounter the inability to give the base class function definition, for example, we use the graph as the base class, circle, Rectangle, square and so on are its derived classes, and design a member function to find the area of the graph.

Double Getarea ();

So when the base class graph is not inherited, we cannot give the area definition of "graph", then we can define the function of area to be pure virtual function, so that it only needs to declare in the base class and not need to define.

The method of defining a pure virtual function is to add = 0 after the function.

For example, the following code implements the base class of the graph:

Class Graph{public:virtual Double Getarea () const = 0;//define pure virtual function};class circle:graph{public:circle (): Radius (0) {}circle ( Double _radius): Radius (_radius) {}double Getarea ()                            //derived class implements pure virtual function {return 3.14 * radius * RADIUS;} Private:double radius;}; Class Rectangle:graph{public:rectangle (): Width (0), height (0) {}rectangle (double _width, double _height): Width (_width ), height (_height) {}double Getarea () {return width * height;} Private:double width,height;};

  

If a class contains pure virtual functions, then the class becomes an abstract base class, and the abstract base class cannot create a concrete object, so you cannot define a "graph" object. You can only define its derived classes, circles, rectangles, squares, etc.

It is also important to note that if a class inherits from an abstract base class but does not define all the pure virtual functions in the abstract base class, then the class is still an abstract base class.

For example:

classabc{Virtual voidFUN1 () =0; Virtual voidFun2 () =0; Two pure virtual functions defined}classderived:abc{voidfun1 () {cout<<"fun1"<<Endl; Implemented only a pure virtual function}};intMain () {Derived D1;//Compile error, cannot instantiate object with abstract base classreturn 0;}

Pure virtual function is a supplement to virtual function, we can use it to help us to achieve the requirements.

C + + pure virtual function and abstract base class

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.