C++primer Reading notes 11-polymorphic

Source: Internet
Author: User

Polymorphism is also an important aspect of C + +. Polymorphic and dynamic types, virtual functions essentially refer to the same thing.


1 Virtual functions
The function of a member function in a class preceded by the virtual surface is a virtual function. The purpose of a virtual function is to define the function again in the derived class that inherits it, so that the function of the derived class is invoked at execution time by a pointer or reference to the base class.




2 derived classes and virtual functions
A derived class would normally redefine the inherited virtual function. There are a few things to note.


The declaration of the <1> virtual function must be fully consistent with the function declaration prototype in the base class. The exception is when the base class returns a pointer to a base type or a reference. A derived class can derive a pointer to a class type or reference
<2>virtual keyword Plus does not matter, but generally to add, can see clearly this is a virtual function
Once the <3>virtual tag is prepended to the function in the base class. And I'll never go away


3 Triggering of dynamic bindings
The C + + function call does not use dynamic binding by default. Two necessary conditions are required to use dynamic binding
<1> invocation of a function by a pointer or reference to a base class type
<2> function is virtual function
It can also be explained here that when a function is called through a pointer or reference, even if the derived class redefined the version number of the base class. The version number of the derived class is not called. Because it does not trigger polymorphism.




4 covering mechanism of virtual function
Sometimes you don't want to trigger polymorphism by pointers or references. The ability to call the base class by adding a explicitly function is now possible

Derived D; Base *p = &d;p->base::func ();
Suppose that you want to call a function with the same name as the base class when the function definition in the derived class is called, or it will throw an infinite recursion.


5 virtual function and copy control
<1> usually sets destructors to virtual functions, because destructors run only their own parts when they run.

For example, if a destructor is called by a pointer or reference to a base class, then only the constructor of the base class is called. Constructors for derived classes will not be called.


The <2> constructor cannot be defined as a virtual function, because the constructor is executed before the object is completely constructed. When the constructor executes, the object's dynamic type is incomplete.


The <3> assignment operator can be set as a virtual function in principle. But it makes no sense to do so. Reasons such as the following:
Virtual functions require a uniform function prototype, which includes the consistency of the function parameter types, assuming we set operator = to virtual function. Then the parameters of the function in the base class are the base class type, and the parameter type of the derived class virtual function is still the base class type. The assignment operator, however, requires that the function parameter type be consistent with the class type. This creates something very easy to confuse.

Class base{public:virtual base& operator = (base& xx);} Class Derived:public base{public:virtual derived& operator = (base& xx);  Virtual functionderived& operator = (derived& xx);//real operator =}


6 virtual functions in constructors and destructors
When executing constructors and destructors. The object at this point is not a complete class. At this point the compiler takes object objects as a change during construction, assuming that the virtual function is called. Then the call will be the class itself type


The version number of the definition.
Cause: When the base class is constructed, the members of the derived class part are not initialized, assuming that the virtual function of the derived class is called at this point, then the virtual function of the derived class will have a problem visiting the class member.

The same is true of the destructor of the base class. Since the destructor


The release of the members of the straight tube itself, and the derived class calls its own destructor when it invokes the destructor. Then call the base class's destructor. Therefore, the destructor of the base class cannot invoke the virtual function of the derived class, since the members of the derived class have been released


Put The actual proportions are as follows:

Base.h#pragma once#include <iostream>using namespace Std;class base{public:base (void) {func ();}; ~base (void) {FUNP ();}; virtual void func () {cout<< "This was in Base constructor" <<ENDL;} virtual void FUNP () {cout<< "This was in Base destructor" <<endl;};};

Derived.h#pragma once#include "Base.h" class Derived:p ublic base{public:derived (void) {func ();} ~derived (void) {func ();} virtual void func () {cout<< "This was in Derived constructor" <<ENDL;} virtual void FUNP () {cout<< "This was in Derived destructor" <<endl;};};

Main.cpp#include "Derived.h" void Main () {Base x;derived Y;}

7 Pure virtual function
The shape of the virtual function followed by "= 0" indicates a pure virtual function. The following points need to be noted for pure virtual functions:
<1> pure virtual function has no function body;
<2> the Last "= 0" does not mean that the function returns a value of 0, it only acts formally. Tell the compilation system "This is pure virtual function";
<3> this is a declaration statement, and there should be a semicolon at the end.
<4> function cannot be called
<5> cannot create a class object with pure virtual functions
<6> assumes that a pure virtual function is declared in a class, and that the function is not defined in its derived class. The virtual function is still a pure virtual function in the derived class.


A class that contains one or more pure virtual functions is called an abstract class. Abstract classes cannot create objects. The meaning of an abstract class is to provide a functional base interface. It is then implemented by the derived class, and the polymorphism is implemented on this basis. But abstract classes can be used as references or pointers.

Instance programs such as the following:

Base.h#pragma once#include <iostream>using namespace Std;class base{public:base (void) {func ();}; ~base (void) {FUNP ();}; virtual void func () {cout<< "This was in Base constructor" <<ENDL;} virtual void FUNP () {cout<< "This was in Base destructor" <<endl;}; virtual void fuck () = 0;};

Derived.h#pragma once#include "Base.h" class Derived:p ublic base{public:derived (void) {func ();} ~derived (void) {func ();} virtual void func () {cout<< "This was in Derived constructor" <<ENDL;} virtual void FUNP () {cout<< "This was in Derived destructor" <<endl;}; virtual void fuck () {cout<< "What a fuck day it is!" <<endl;}};

Main.cpp#include "Derived.h" void Main () {Derived Y; base& X = Y;}




C++primer Reading notes 11-polymorphic

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.