c ++ study notes _6

Introduction: This note corresponding courses for Chinese universities mooc in Beijing University of programming and algorithms (c) C ++ object-oriented programming , primarily for their own review of the use, and this note is based on use c and java on, only for unlike c and java to write

Polymorphism

The basic concept of virtual functions and polymorphism

  1. Virtual function form: before the class member functions plus virtualmembers of the keyword function is virtual function.
  2. virtualKeyword before the function declaration only, no need to write the function body at the time of writing specific virtualkeywords (like friendas)
  3. Constructors and static member functions can not be virtual. (But destructor)

  4. Polymorphism: polymorphism herein are not the same and java. Definition: (reference or base class) by the base pointer to call the base class and a derived class of the same name as a virtual function, when the base pointer reference is a base class object, it is called a base class virtual function; if base pointer reference is a class object that is called a derived class virtual function. (While normal call function with the same name base class pointer, calling a function in the base class)

Examples of polymorphic

  1. In a non-virtual function call the constructor, destructor member functions in non, is polymorphic.

Example:

class Base{
    public:
        void fun1() { fun2(); }
        //这里的fun2()相当于this->fun2(),而this指针代表的是Base类,是基类指针,因此是多态。所以最后的输出结果是derived
        vitural void fun2() { cout << base << endl; }
};

class Derived : public Base{
    public:
        vitural void fun2() { cout << derived << endl; }
};

int main(){
    Derived d;
    Base * p = & d;
    p->fun1();
    //输出结果是 derived
    return 0;
}
  1. Base and derived class virtual functions in the class with the same name as the parameter list, even without virtualautomatically become a virtual function.
  2. Calling the constructor and destructor are not polymorphic virtual function call is a function defined in their class (if not defined, then calls the same function in the base class defined)

The principle polymorphism

Each class has virtual functions (or have derived class virtual function) has a virtual function table pointer to any object in both class virtual function table, it is an object of class virtual function memory size will be more out of four bytes, four extra bytes is the virtual function table pointer.

Virtual destructor, and a pure abstract class destructor

  1. Pointer to the base class through deletean object of a derived class will only call the destructor of the base class. (Nature while the program exits to calling the destructor will be derived class, and then calls the base class destructor) in order to call the destructor for the derived class, and then call the destructor for the base class, the base class requires destructor declared virtual(this destructor derived class virtual function will become the default), to achieve the purpose.
  2. As long as there is a class virtual function, then it is recommended that it be written destructor virtual destructor.

Example:

#include <iostream>

using namespace std;

class Son{
    public:
        virtual ~Son(){
            cout << "bye from son" << endl;
        }
}; 

class GrandSon:public Son{
    public:
        ~GrandSon(){
            cout << "bye form grandson" << endl;
        }
};

int main(){
    GrandSon s;
    Son * p = & s; 
    delete p;
    return 0;
}
  1. Pure virtual function: no function virtual function member
    wording Example:virtual void Print() = 0 ;
  2. Abstract class: class contains pure virtual function called abstract class (with the use of the abstract class java)
  3. Abstract class only as a base class to derive a new class, you can not create objects of abstract classes.
  4. Abstract class member function can be called in a pure virtual function, but within the constructor or destructor not a pure virtual function call.

Guess you like

Origin www.cnblogs.com/fyunaru/p/11479558.html