C ++ (forty) - polymorphism, virtual functions, virtual destructor, pure virtual function

 1, polymorphic

     Object-oriented programming, polymorphism as follows:

  (1) overloading polymorphism: function overloading, operator overloading;

  (2) Run polymorphism: calls the same function by a different derived class pointer to the base class (or reference), exhibit different behavior;

  (3) the template polymorphism: parametric polymorphism, to obtain different functions by a different class or template, having different characteristics and different behavior;

2, covered with the same name overloading

 (1) override (covering the same name)

  Will appear in the derived class, the prototype multiple functions are the same.

 (2) overload (overload)

   Range in the same scope, the number of parameters of different types or a plurality of functions composed of the same name.

3, virtual function

   Cause: Call to a member function through the pointer, accessible only to members of the same name of the base class. In the phenomenon of the same name covered by a class of objects (pointers and references), calls the same function, the compiler will call the static binding to the same name as a function of the class, that is, by the base class derived object pointer is not accessible a function of the same name of the class , even if the derived object pointer is initialized.

  Mechanism of C ++ virtual functions for implementing polymorphism (polymorphism) of. The core concept is the base class for the derived class accessible via the function definition.

  An opportunity for the virtual base class derived class with the same name access function pointer function ;

  Pointer to the base class of polymorphic its operation class object, based on the different classes of objects, which invoke the corresponding function, this function is a virtual function.

  We can say that the base class virtual function declaration in a derived class is a virtual function , even after use virtual keyword.

  Dynamic binding: call a class function is not to be determined at compile time, but is determined at run time. Since the preparation of the code can not be invoked to determine the function of which is a function of the derived class or base class, it is a "virtual" function.

   Only by means of a virtual function pointers or references to achieve the effect of polymorphism. But will introduce large overhead for the program, practice should be avoided.

  note:

  Declared in the base class virtual function in the constructor, a static member function may not be virtual , but can destructor .

  Use angle: virtual function call by a member of the subclass parent pointer, the constructor called when creating automatically, without parent pointer;

  An angular: virtual function corresponds to a pointer to the virtual function table pointer, the constructor vtable is initialized when the constructor is virtual, it is necessary to find the entry address by virtual constructor virtual function table, but this time no virtual function table Therefore constructor can not be virtual.

4, virtual destructor

   Only virtual destructor, no virtual constructor.

  When creating a derived class object, it calls the base class constructor -> derived class constructor -> derived class destructor -> base class destructor.

  If you create a derived class object with the new operator dynamic, and thus the object address initialize the base class pointer, construct no problem, but when you delete a derived class object with the delete operator, because the pointer is a pointer to the base class, by static binding, call base class destructor, do not call the derived class destructor so that the derived class can not perform some cleanup work, such as: memory derived class application system no chance to return.

  Virtual destructor: the base class virtual destructor is provided, is a derived class. At this time, when using the base object pointer to the derived class object is destroyed, will by dynamically build a derived class calls the destructor for cleanup derived class.

5, a pure virtual function

  The following statement represents a pure virtual function is a function of:

A class
{
public:
    Virtual void foo () = 0; // Flag = 0 a pure virtual function is a virtual function, not the function thereof, can not be instantiated, it can not be called.
};

  After a function is declared as pure virtual, meaning pure virtual function is: I am an abstract class! Do not I instantiate! Pure virtual function is used to regulate the behavior of the derived class, in fact, the so-called "Interface." It tells the user that I derived class will have this function.

  The introduction of a pure virtual function, is for two purposes:
  1, for safety , by avoiding any need to clear the result of an unknown but because of carelessness caused remind subclasses do realize that should be done. Actually limits the function of the derived class, standard interfaces, to achieve sub-class left.
  2, for efficiency , the efficiency of the program is not executed, but to the coding efficiency.

 6, application examples polymorphism runtime

 (1) header shape.h

#pragma once

//#ifdef SHAPE_H

#include <iostream>
using namespace std;

class Shape
{
public:
    virtual double getArea() const = 0;//纯虚函数
    void print() const;
    virtual ~Shape() {}                //虚析构函数
};

class Circle :public Shape
{
public:
    Circle(int xv= 0, int yv= 0, double rv= 0.0);
    double getArea() const;
    void print() const;
protected:
    int x, y;
    double r;
};

class Rectangle :public Shape
{
public:
    Rectangle(int av= 0, int bv = 0);
    double getArea() const;
    void print() const;
protected:
    int a, b;
};

//#endif // DEBUG

 

(2) function defines shape.c

#include <iostream>
using namespace std;
#include "shape.h"

void Shape::print() const
{
    cout << "Base class Object" << endl;
}

Circle::Circle(int xv, int yv, double rv)
{
    x = xv; y = yv;
    r = rv;
}
double Circle::getArea() const
{
    return 3.14*r*r;
}
void Circle::print() const
{
    cout << "center is" << x << "  " << y << endl;
}

Rectangle::Rectangle(int av, int bv)
{
    a = av; b = bv;
}
double Rectangle::getArea() const
{
    return a*b;
}
void Rectangle::print() const
{
    cout << "h is " << a << "  " << b << endl;
}

 

3, test file

#include <iostream>
using namespace std;
#include "shape.h"

void creat_object(Shape **ptr);
void display_area(Shape *ptr);
void delete_object(Shape *ptr);

void main()
{
    Shape *shape_ptr;
    creat_object(&shape_ptr);
    display_area(shape_ptr);
    delete_object(shape_ptr);
    system("pause");
}

void creat_object(Shape **ptr)
{
    chartype;
     * PTR = nullptr a;
     do { 
        COUT << " Create Object: " << endl; 
        CIN >> type;
         Switch (type) 
        { 
            Case  ' C ' : 
            { 
                int XX, YY; Double RR; 
                COUT << " Please input radius and center: " ; 
                CIN >> >> XX YY >> RR;
                 * PTR = new new Circle (XX, YY, RR);
                break;
            } 
            Case  ' R & lt ' : 
            { 
                int AA, BB; 
                COUT << " Please enter the length and width of the rectangle: " ; 
                CIN >> >> AA BB;
                 * PTR = new new the Rectangle (AA, BB);
                 BREAK ; 
            } 
            default : << COUT " select re \ n- " << endl; 
        } 
    } the while (* PTR == nullptr a); 
} 

void Display_Area (the Shape * PTR) 
{
    cout << ptr->getArea() << endl;
}

void delete_object(Shape *ptr)
{
    delete(ptr);
}

 

Guess you like

Origin www.cnblogs.com/eilearn/p/10954975.html