The implementation of abstract classes and pure virtual functions in C + +

Source: Internet
Author: User

1. Abstract classes containing pure virtual functions

2. Pure virtual Functions in abstract classes (usually base classes) are not related to the implementation of the function body or not, and the system automatically ignores

3. Inheriting from a subclass of an abstract class, you must implement a pure virtual function of the parent class to instantiate the object

4. Abstract classes do not allow instantiating objects and can only be used as a base class or virtual interface

5. Pointers to abstract classes can point to different derived class objects (functions of virtual functions)

class father{    public:        virtualvoid0int10 ; }; // pure virtual function in abstract class, function body implementation is also equivalent to not implemented, automatic ignoring     };    Father F; // error, abstract class (class containing pure virtual function), not allowed to instantiate

6. Ordinary function, friend function, constructor can not be virtual function, other member function, destructor can be virtual function

Analysis:

A. Ordinary functions and friend functions are not members of a class, so there is no concept of virtual functions

B. If the constructor is a virtual function, in the case of inheritance, only the subclass will be constructed and the parent class will not be constructed, obviously not

C. In the case of virtual inheritance, where multiple inheritance produces diamond inheritance, virtual inheritance can prevent the common base class from being repeatedly constructed

D. A virtual destructor is a destructor for a derived class that is called when an object of a derived class is deleted with a pointer to a base class.

The implementation of abstract classes and pure virtual functions in C + +

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.