Differences between C ++ classes in inheritance: virtual functions, pure virtual functions, and common functions

Source: Internet
Author: User

Differences between C ++ classes in inheritance: virtual functions, pure virtual functions, and common functions
1. virtual functions (impure virtual) C ++ are mainly used for "Runtime polymorphism". The implementation of virtual functions is provided in the parent class, and default function implementation is provided for sub-classes. Sub-classes can rewrite the virtual functions of the parent class to realize the specialization of sub-classes. The following is a virtual function in the parent class:

class A{public:    virtual void out2(string s)    {        cout<<"A(out2):"<<s<<endl;    }}; 

 

2. Classes of pure virtual functions (pure virtual) C ++ are called "abstract classes ". Abstract classes cannot use new output objects. Only subclasses that implement this pure virtual function can generate new output objects. In C ++, pure virtual functions are more like "providing only declarations without implementation". They are constraints on sub-classes and are "interface inheritance ". Pure virtual functions in C ++ are also "Runtime polymorphism ". The following classes contain pure virtual functions, which are "abstract classes ":
class A{public:    virtual void out1(string s)=0;    virtual void out2(string s)    {        cout<<"A(out2):"<<s<<endl;    }};

 

3. normal Functions (no-virtual) are statically compiled without Runtime polymorphism. They only call their own common functions based on pointers or referenced "nominal value" class objects. A common function is a "forced implementation" provided by the parent class for the subclass ". Therefore, in the inheritance relationship, the subclass should not override the normal function of the parent class, because the function call is related to the literal value of the Class Object. 4. Integrated Program instance
# Include <iostream> using namespace std; class A {public: virtual void out1 () = 0; // virtual ~ A () {}; virtual void out2 () // default implementation {cout <"A (out2)" <endl;} void out3 () /// Force implement {cout <"A (out3)" <endl ;}}; class B: public A {public: virtual ~ B () {}; void out1 () {cout <"B (out1)" <endl;} void out2 () {cout <"B (out2) "<endl;} void out3 () {cout <" B (out3) "<endl ;}}; int main () {A * AB = new B; AB-> out1 (); AB-> out2 (); AB-> out3 (); cout <"************************" <endl; B * bb = new B; bb-> out1 (); bb-> out2 (); bb-> out3 (); delete AB; delete bb; return 0 ;}

 

 

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.