The virtual function is pretty good.

Source: Internet
Author: User

Introduction

Has never written a concept of the article, because I think these conceptual things in the book have and said also very detailed written also useless, today's whim want to write a write, the following discussion with you about virtual base class, virtual function and pure virtual function, a look at the name let people easily feel confused. But it doesn't matter that you will understand it after you finish reading this article.

Body

Virtual base class
Look at a piece of code before explaining its role

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
}


It can be seen 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 that Class D has a member with the same name Ivalue (one inherits from Class B and one inherits 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 who inherited it from Ivalue so it produces a two semantic problem. The correct approach should be to add the scope qualifier D.b::ivalue Represents the invocation of a ivalue inherited from Class B. However, instances of Class D have more than one instance of Ivalue, which takes up memory space. So 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
}


The inherited class is preceded by the virtual keyword to indicate that the inherited class is a virtual base class whose inherited members retain only one instance in the derived class. For example Ivalue this member, from the perspective of Class D, it 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 functions
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; //defines a pointer to the base class
    a a;
    B b;
    p=&a;
    p->funprint ();
    p=&b;
    p->funprint ();
}


What do you think the output of this piece of code is? Some people might answer right away. Funprint of Class A and funprint of Class B because the first output is an instance of Class A, the second output is a reference to the instance of Class B. So I'm telling you this is wrong, the answer is funprint of Class A and funprint of Class A as to why the output of such a result is not within the scope of this discussion; you remember, Regardless of which class the referenced instance is, the system invokes the method of the left-hand object that the class belongs to when you call it. For example, the above code class A B has a funprint function, because p is a pointer to Class A, so whether you point the P pointer to class A or to Class B, the function that is ultimately called is the Funprint function of Class A. This is the static union, the compiler at the time of compilation has been determined. But what if I want to dynamically decide which function to invoke according to the different instances? This requires the use of virtual functions (that is, dynamic linking)

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 the base class
    a a;
    B b;
    p=&a;
    p->funprint ();
    p=&b;
    p->funprint ();
}


Before the member function of the base class, add the virtual keyword to indicate that this function is a virtual function, so-called virtual function is at compile time not sure which function to call, but to dynamically decide which function to invoke, to implement the virtual function must be derived class function name and base class, parameter type parameters such as the same as the base class. However, the virtual keyword in a derived class can be omitted, which also means that this is a virtual function. Here to solve the code, declare a pointer to a base class (must be a base class, and vice versa) p, the P point to Class A, call the Funprint function, then the system will determine the type of the instance p points to, if it is an instance of Class A call the Class A funprint function, If it is an instance of Class B, call the Funprint function of Class B.

Pure virtual function
Rather than a pure virtual function is called an abstract class, it just declares a function but does not implement it, let the derived class to implement it, in fact, this is also very well understood.

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" <<endl;};
};

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


As the code above, defines a vehicle class (Vehicle), the class has a function to print out the number of vehicles tires, but the number of vehicles a lot of tires is naturally uncertain, so it is defined as pure virtual function, that is, the light definition function name does not implement it, The class Camion inherits the vehicle and implements the code inside, printing out 4 tires. The same is true of the bike class. One thing to note is that pure virtual functions cannot be manifested, but pointers 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 sub-object, and a non-virtual base class produces its own child object.
3, the virtual base class sub-object is initialized by the constructor of the most derived class by calling the virtual base class's constructor.
4, the most derived class refers to the class that is specified when an object is created in the inheritance structure.
5, a call to a virtual base class constructor must be listed in the member initialization list of a derived class's constructor, or a default constructor that uses the virtual base class if it is not listed.
6, a call to the virtual base class constructor is listed in the member initialization list of 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 calls to the virtual base class's constructors listed in all base classes of the derived class are ignored in execution, guaranteeing that the virtual base class sub-object is initialized only once.
7, when a call to a virtual base class and a non-virtual base class constructor occurs at the same time in a member initialization list, the constructor of the virtual base class is executed before the constructor of the non-virtual base class.

Virtual functions
1, virtual functions are non-static, non-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 a class definition, not when the function body of a member function is implemented.
3, no matter how many times a virtual function is inherited by the public, it still retains the characteristics of its virtual function.
4, if a member function in a class is described as a virtual function, the member function may have different implementations in the derived class. When you use this member function to manipulate a pointer or reference an object that is identified, a dynamic union can be used for that member function call.
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 object it points to, invoking different versions of the member functions, and these actions are implemented dynamically at run time. Virtual function fully embodies the dynamic polymorphism of object-oriented programming. The member functions of the pure virtual function version, and these actions are implemented dynamically at run time. Virtual function fully embodies the dynamic polymorphism of object-oriented programming.

Pure virtual function
1, when a meaningful implementation of a virtual function cannot be given 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 pure virtual function is to provide a consistent interface for derived classes.
3, pure virtual functions cannot be manifested, but pointers can be declared.

The virtual function is pretty good.

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.