[C + + Notes] polymorphism

Keywords: PHP Java

Polymorphism is an important feature of object-oriented design in C + +. The same name has different functions, and the function calling process performs different functions. The principle of polymorphism is realized by a virtual table. Dynamic polymorphism will sacrifice some space and efficiency to achieve dynamic binding.

 

 

Static polymorphism

Function overload is static polymorphism. Binding occurs during compilation. Which function is called is determined according to the parameters of the function.

#include <iostream>

using namespace std;

void foo(int a, int b)
{
    cout << "foo(int a, int b)" << endl;
}

void foo(double a, double b)
{
    cout << "foo(float a, float b)" << endl;
}

int main()
{
    foo(1, 2);
    foo(1.1, 2.2);

    return 0;
}

 

Dynamic polymorphism

The dynamic polymorphism is not determined in the compilation phase, but in the program runtime, the virtual function of which class is called depends on the object pointed by the pointer (Reference) of the base class.

 

The conditions of dynamic polymorphism realization:

1. There are virtual functions in the base class.

2. The derived class overrides the virtual function in the base class.

3. The pointer of the parent class points to the child class object and calls the common interface.

 

Format of virtual function:

Class class name

{

    virtual function declaration;

};
#include <iostream>

using namespace std;

class Animal
{
public:
    Animal()
    {
        cout << "Animal()" << endl;
    }

    virtual void eating()
    {
        cout << "animal is eating!" << endl;
    }

    virtual ~Animal()  //Virtual deconstruction to ensure complete deconstruction
    {
        cout << "~Animal()" << endl;
    }
};

class Dog:public Animal
{
public:
    Dog()
    {
        cout << "Dog()" << endl;
    }

    void eating()
    {
        cout << "Dog is eating!" << endl;
    }

    ~Dog()
    {
        cout << "~Dog()" <<endl;
    }
};

int main()
{
    Animal *ani = new Dog;
    ani->eating();
    delete  ani;

    return 0;
}

Note: for classes with virtual functions, destructors should also be declared as virtual functions, which are virtual destructors. If the above code virtual ~Animal() is changed to ~ Animal(), the operation result is as follows:

The virtual destructor can call the destructor of the derived class to ensure the complete destructor.

 

 

Purely imaginary function

When a pure virtual function is declared in the base class, there is no implementation body. Add "= 0" after the function prototype, using the format:

Class class name
{

    virtual function declaration = 0;

};

 

1. Classes with pure virtual functions are called pure virtual base classes, also called abstract base classes. Abstract classes cannot be instantiated, can be inherited, and provide public interfaces for classes. They are implemented in derived classes, similar to interfaces in java.

2. If a pure virtual function is declared in the base class and the method is not implemented in the derived class, it is still a pure virtual function in the derived class and the derived class is still a pure virtual base class.

#include <iostream>

using namespace std;

class Animal
{
public:
    Animal()
    {
        cout << "Animal()" << endl;
    }

    virtual void eating() = 0;

    virtual ~Animal()
    {
        cout << "~Animal()" << endl;
    }
};

class Dog:public Animal
{
public:
    Dog()
    {
        cout << "Dog()" << endl;
    }

    void eating()
    {
        cout << "Dog is eating!" << endl;
    }

    ~Dog()
    {
        cout << "~Dog()" <<endl;
    }
};

int main()
{
    Animal *ani = new Dog;
    ani->eating();
    delete  ani;

    return 0;
}

Posted by AAFDesign on Sun, 03 Nov 2019 04:31:16 -0800