[Notes] C ++ polymorphism

C ++, polymorphism is an important feature of object-oriented design ideas, the same name has a different function function, the function call to perform different functions. Polymorphic principle is achieved by a virtual function table (Virtual Table). Dynamic polymorphism will sacrifice some space and efficiency to ultimately achieve dynamic binding.

 

 

Static polymorphism

Function overloading is a static polymorphism, binding occurs during compilation to determine which function according to the parameters of the function call.

#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

Dynamic polymorphism is not determined at compile time, but when the program runs in accordance with the base class pointer (reference) points of the object to determine which class virtual function call.

 

Operation conditions to achieve polymorphism:

1. The base class has virtual functions.

2. A derived class override base class virtual function.

3. The parent pointer pointing subclass object, call the common interface.

 

Virtual functions using the format:

class 类名

{

    virtual 函数声明;

};
#include <iostream>

using namespace std; class Animal { public: Animal() { cout << "Animal()" << endl; } virtual void eating() { cout << "animal is eating!" << endl; } 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; } 

Note: a class contains a virtual function, the destructor also declared virtual as a virtual destructor. If the code on virtual ~ Animal () to ~ Animal (), results are as follows:

Obviously incomplete destruction, can call the virtual destructor destructor derived class, to ensure complete destruction.

 

 

Pure virtual function

When pure virtual functions declared in the base class virtual function, the body is not achieved, plus "= 0" after the function prototype, using the format:

class 类名
{

    virtual 函数声明 = 0;

};

 

Class 1 containing pure virtual function is referred to as a pure virtual base class, the base class is also called an abstract (Abstract Base Class), abstract class can not be instantiated, it can be inherited to provide the public interface, implemented in a derived class, similar to the java the Interface.

2. If the base class declares a pure virtual function, derived class does not implement this method, in a derived class remains a pure virtual function, the derived class virtual base class remains pure.

#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; }

Guess you like

Origin www.cnblogs.com/woniu201/p/11109609.html