Understanding the concepts of virtual base class, virtual function and pure virtual function __ function

Source: Internet
Author: User
Tags class definition function prototype

Introduction

     has never written a conceptual article, because I think that these conceptual things are written in the books and are very detailed and useless, and today I have a whim to write, Here is a discussion of the virtual base class, virtual functions and pure virtual functions, a look at the name of the people can easily feel confused. But don't worry, you'll understand when you've finished reading this.

Body

       Virtual base class
       read a piece of code before stating its usefulness

Class a
{
Public:
    int ivalue;
};

Class b:public a
{
Public:
    void bprintf () {cout<< "This is class b" <<endl;};
};

Class c:public a
{
Public:
    void cprintf () {cout<< "This is class c" <<endl;};
};

Class d:public b,public c
{
Public:
    void dprintf ( ) {cout<< "This is class d" <<endl;};
};

Void main ()
{
    d d;
    cout<< d.ivalue<<endl; //error, ambiguous access
    cout<<d.a::ivalue<<endl; // Correct
    cout<<d.b::ivalue<<endl; //correct
    cout <<d.c::ivalue<<endl; //correct
}

The


can see from the code that Class B C inherits the Ivalue member of Class A. So Class B C has a member variable ivalue, and Class D inherits B C, so Class D has a member of the name Ivalue (one inherited from Class B and one from class C). Call D.ivalue in the main function because Class D has a member with the same name ivalue the compiler does not know the call from who inherited the ivalue so the problem of two semantics arises. The correct approach should be to add a scoping qualifier D.b::ivalue Represents the invocation of a ivalue inherited from Class B. However, instances of Class D have multiple instances of ivalue that occupy memory space. Therefore, the concept of virtual base class is referenced in C + + to solve this problem.

Class a
{
Public:
    int ivalue;
};

Class b:virtual public a
{
Public:
    void bprintf () {cout<< "This is class b" <<endl;};
};

Class c:virtual public a
{
Public:
    void cprintf () {cout<< "This is class c" <<endl;};
};

Class d:public b,public c
{
Public:
    void dprintf ( ) {cout<< "This is class d" <<endl;};
};

Void main ()
{
    d d;
    cout<< d.ivalue<<endl; //correct
}


Preceding the inherited class with the virtual keyword indicates that the inherited class is a virtual base class whose inherited members retain only one instance in the derived class. For example, this member of Ivalue, from the point of view of Class D, inherits from Class B and Class C, and Class B C is inherited from Class A, but they keep only one copy. Therefore, when calling D.ivalue in the main function, no error is generated.

virtual function
Or look at the code first.

Class A
{
Public
void Funprint () {cout<< "Funprint of Class A" <<endl;};
};

Class B:public A
{
Public
void Funprint () {cout<< "Funprint of Class B" <<endl;};
};

void Main ()
{
A *p; To define a pointer to a base class
A A;
b b;
p=&a;
P->funprint ();
p=&b;
P->funprint ();
}


What you think the output of this piece of code is. Some people may immediately answer funprint of Class A and funprint of Class B because the first output is an instance of the reference Class A, the second output is an instance of reference to Class B. Then I tell you it's wrong, the answer is Funprint ofclass A and funprint of Class A as to why the output of such a result is not within the scope of this article; just remember, Regardless of which class the referenced instance is, the system invokes the method of the class of the left-valued object when you call it. For example, the above code class A B has a funprint function, because p is a pointer to a class A, so whether you point the P pointer to class A or Class B, the final call function is the Funprint function of Class A. This is the static joint, the compiler is in the compile time has been determined. But if I want to implement a different instance, dynamically decide which function to call. This requires the use of virtual functions (i.e., dynamic joint)

Class a
{
Public:
    virtual void funprint () {cout<< " Funprint of class a "<<endl;};
};

Class b:public a
{
Public:
    virtual void funprint ( ) {cout<< "Funprint of class b" <<endl;};
};

Void main ()
{
    a *p; //defines a pointer to a base class
     a a;
    B b;
    p=&a;
    p->funprint ();
    p=&b;
    p->funprint ();
}


Adding the virtual keyword before the member function of the base class indicates that the function is a virtual function, and that the so-called virtual function is not sure which function to call at compile time, but dynamically determines which function to call, and the function name of the class must be derived to implement the virtual function is the same as the base Parameter Name argument types are also the same as base classes. However, the virtual keyword in a derived class can be omitted, which means that it is a virtual function. Here's how to solve the code, declare a pointer to a base class (must be a base class, not vice versa) p, point p to instance a of Class A, call the Funprint function, then the system will judge the type of the instance that P points to, and if it is an instance of Class A, call the Funprint function of Class A. If it is an instance of Class B, the Funprint function of Class B is invoked.

     Pure virtual function  
     rather than a pure virtual function, it is called an abstract class, it simply declares a function but does not implement it, and lets the derived class implement it. In fact, this is also very good understanding.  

Class Vehicle
{
Public
virtual void Printtyre () = 0; Pure virtual functions are defined in this way
};

Class Camion:public Vehicle
{
Public
virtual void Printtyre () {cout<< "Camion Tyre Four" <<endl;};
};

Class Bike:public Vehicle
{
Public
virtual void Printtyre () {cout<< "Bike Tyre Two" <<endl;};
};

void Main ()
{
Camion C;
Bike b;
B.printtyre ();
C.printtyre ();
}


As the code, defined a vehicle class (Vehicle), the class has a function to print out the number of vehicles, but the number of vehicles many tires are naturally uncertain, so it is defined as a pure virtual function, that is, the name of the light definition function does not realize it, The class Camion inherits the vehicle and implements the code inside, printing out 4 tires. The same is true of the bike class. It is important to note that a pure virtual function cannot be materialized, but a pointer can be declared.


Summarize

Virtual base class
1, a class can be used both as a virtual base class and as a non-virtual base class in a class family.
2, in the object of the derived class, the virtual base class with the same name produces only one virtual base class child object, while a non-virtual base class produces its own child objects.
3, the virtual base class child object is initialized by the constructor of the most derived class by invoking the constructor of the virtual base class.
4, the most derived class is the class that is specified when an object is established in an inheritance structure.
5, a call to the virtual base class constructor must be listed in the member initialization list of the constructor of the derived class, or a default constructor that uses the virtual base class if it is not listed.
6, calls to a virtual base class constructor are listed in the member initialization list for constructors in derived classes that derive directly or indirectly from the virtual base class. However, only the constructor of the most derived class that is used to establish the object calls the constructor of the virtual base class, and the call to the constructor of the virtual base class that is listed in all base classes of the derived class is ignored in execution, guaranteeing that the virtual base class child object is initialized only once.
7, a constructor for a virtual base class is performed before a constructor of a non-virtual base class when a call to a virtual base class and a non-virtual base class constructor occurs simultaneously in a member initialization list.

virtual function
1, virtual functions are non-static, not inline member functions, not friend functions, but virtual functions can be declared as friend functions in another class.
2, a virtual function declaration can only appear in the function prototype declaration of the class definition, not when the function body of the member function is implemented.
3, a virtual function, regardless of how many times it is inherited by the public, still retains its virtual function's properties.
4, if a member function in a class is described as a virtual function, the member function may have a different implementation in the derived class. When you use this member function to manipulate a pointer or to reference an object that is identified, a dynamic binder can be used to invoke the member function.
5, when a virtual function is defined, a pointer to the base class declared in the program can point to its derived class. During execution, the function can constantly change the objects it points to, call different versions of member functions, and these actions are dynamically implemented at run time. Virtual function fully embodies the dynamic polymorphism of object-oriented programming. The member functions of a pure virtual function version, and these actions are dynamically implemented at run time. Virtual function fully embodies the dynamic polymorphism of object-oriented programming.

Pure virtual function
1, when a meaningful implementation cannot be given to a virtual function in a base class, it can be declared as a pure virtual function, and its implementation is left to the derived class.
2, the function of a pure virtual function is to provide a consistent interface for derived classes.
    3, pure virtual functions are not materialized, but can declare pointers.

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.