4.6 C + + abstract base class and pure virtual member functions

Source: Internet
Author: User

Reference: http://www.weixueyuan.net/view/6376.html

Summarize:

In C + +, the common interface can be implemented by abstracting the base class

A pure virtual member function has no function body, only a function declaration, and a "= 0" at the end of a pure virtual function declaration indicates that the function is a pure virtual member function.

A class that contains a pure virtual member function is an abstract base class that is abstract because it cannot be instantiated or used to create objects.

A pure virtual member function can be inherited by a derived class, and if the derived class does not redefine all of the abstract base class (with more than one of several) pure virtual member functions, the derived class also becomes an abstract base class and therefore cannot be used to create objects.

A pure virtual member function can make a class an abstract base class, but the abstract base class can contain other member functions or member variables in addition to pure virtual member functions.

Only virtual functions in a class can be declared as pure virtual member functions, neither ordinary member functions nor top-level functions can be declared as pure virtual member functions.

Abstract base classes can be used to implement a common interface, a purely virtual member function declared in an abstract base class, and derived classes must all redefine these pure virtual member functions if they want to be able to create objects.

--------------------------

A public interface is a collection of member functions that must be redefined by the class that supports the interface, or the object cannot be created. in C + +, the public interface can be implemented by abstracting the base class . To introduce the abstract base class, we need to look at the pure virtual member function first.

The declaration syntax for a pure virtual member function is as follows:
virtual function return type function name (function parameter) = 0;

A pure virtual member function has no function body, only a function declaration, and a "= 0" at the end of a pure virtual function declaration indicates that the function is a pure virtual member function.

A class that contains a pure virtual member function is an abstract base class that is abstract because it cannot be instantiated or used to create objects.

Example 1:

#include <iostream>using namespace Std;class base{public:    virtual void display () = 0;    //......}; int main () {    base b;//compile error    return 0;}

As shown in this example, only one base class is defined in this example, a pure virtual member function is declared, and a class containing a pure virtual member function is an abstract base class, so the base class is an abstract base class. Abstract base classes are not used to create objects, and in the main function we try to create objects of the base class, which is not allowed, and the compiler hints for syntax errors.

A pure virtual member function can be inherited by a derived class, and if the derived class does not redefine all of the abstract base class (with more than one of several) pure virtual member functions, the derived class also becomes an abstract base class and therefore cannot be used to create objects.

Example 2:

 #include <iostream>using namespace Std;class base{public:base () {    x = 0;}    Base (int a) {x = A;}    virtual void display () = 0; int Getx () {return x;} Private:int x;}; Class Derived1:public base{public:derived1 (int a) {y = A;} Private:int y;};    Class Derived2:public Base{public:derived2 (int a, int b): base (a) {z = b;}    void display () {cout<<getx () << "" <<z<<endl; }private:int Z;}    int main () {base B;   Compile Error derived1 D1 (5);    Compile error Derived2 D2 (5,6);    D2.display (); return 0;} 

The

defines three classes in this example, a base class, an integer member variable x in the base class, a member function with two constructors, a getx ordinary member function, and a pure virtual member function display. A derived1 class is then defined that inherits the base class, adds a member variable y of an integral type to the class, and defines a constructor. After defining a Derived2 class, this class also adds an integer member variable z, defines a constructor with a parameter, and explicitly invokes the constructor in the base class, in addition to the Derived2 class, which also redefined the pure virtual member function display in the base class. The display function in a derived class is overridden with a pure virtual member function in the base class. Let's take a look at the case in the main function, where the main function first attempts to create an object of the base class, because the base class contains a pure virtual member function, so it is an abstract base class and cannot create an object. After attempting to create an Derived1 object, the Derived1 class inherits the pure virtual member function from base class base and does not redefine the function, so the Derived1 class is a derived class of the base class, but it is still an abstract base class, so it is also not possible to create an object. After attempting to create an object of the Derived2 class, which is also a derived class of the base class, inherits the pure virtual member function display from the base class, but the function is also redefined in the class, thus overriding the pure virtual member function of the base class, which is not an abstract base class, so you can create an object. When you create an object of the Derived2 class, you call the parameter constructor in the class, and then you call the display function through the object to print out the values of the member variables x and Y.

A pure virtual member function can make a class an abstract base class, but in addition to the pure virtual member function, . As shown in Example 2 in the base class, in addition to the pure virtual member function, the class contains a private member variable x and two constructors and a normal member function getx.

only virtual functions in a class can be declared as pure virtual member functions, neither ordinary member functions nor top-level functions can be declared as pure virtual member functions. The in Example 3 attempts to declare a top-level function and a normal member function as pure virtual member functions, which are not allowed.

Example 3:

void fun () = 0;   Compile errorclass base{public:    void display () = 0;  Compile error    //...};

Abstract base classes can be used to implement a common interface, a purely virtual member function declared in an abstract base class, and derived classes must all redefine these pure virtual member functions if they want to be able to create objects.

4.6 C + + abstract base class and pure virtual member functions

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.