[C++ Advanced Knowledge] C++ Polymorphism


C++ polymorphism

C++ is an object-based programming language with many powerful features, including polymorphism. Polymorphism means that different properties of objects can exhibit the same behavior. In C++, there are two types of polymorphism: compile-time polymorphism and run-time polymorphism.

compile time polymorphism

Compile-time polymorphism is achieved through function overloading and templates. Function overloading refers to the definition of multiple functions with the same name but different parameter types in the same scope. The C++ compiler will choose which function to call based on the parameter types in the function call. For example:

void print(int num) {
    
    
    cout << "Integer: " << num << endl;
}

void print(double num) {
    
    
    cout << "Double: " << num << endl;
}

int main() {
    
    
    int a = 5;
    double b = 3.14;
    print(a);
    print(b);
    return 0;
}

The output is:

Integer: 5
Double: 3.14

Templates are a general programming mechanism that can be used to create functions or classes that have the same code structure but different data types. For example:

template<typename T>
void print(T num) {
    
    
    cout << "Value: " << num << endl;
}

int main() {
    
    
    int a = 5;
    double b = 3.14;
    print(a);
    print(b);
    return 0;
}

The output is the same as the above example.

runtime polymorphism

Runtime polymorphism is achieved through virtual functions and inheritance. A virtual function refers to a function declared as a virtual function in the base class, which can be overridden in the derived class. When the derived class object calls this function, it will call the function in the derived class instead of the function in the base class. For example:

class Shape {
    
    
public:
    virtual void draw() {
    
    
        cout << "Drawing a generic shape." << endl;
    }
};

class Rectangle : public Shape {
    
    
public:
    void draw() {
    
    
        cout << "Drawing a rectangle." << endl;
    }
};

class Circle : public Shape {
    
    
public:
    void draw() {
    
    
        cout << "Drawing a circle." << endl;
    }
};

int main() {
    
    
    Shape* shape1 = new Rectangle();
    Shape* shape2 = new Circle();
    shape1->draw();
    shape2->draw();
    return 0;
}

The output is:

Drawing a rectangle.
Drawing a circle.

Inheritance means that derived classes can inherit member variables and member functions in the base class. For example:

class Animal {
    
    
public:
    void speak() {
    
    
        cout << "Generic animal sound." << endl;
    }
};

class Dog : public Animal {
    
    
public:
    void speak() {
    
    
        cout << "Bark!" << endl;
    }
};

int main() {
    
    
    Animal* animal1 = new Animal();
    Animal* animal2 = new Dog();
    animal1->speak();
    animal2->speak();
    return 0;
}

The output is:

Generic animal sound.
Bark!

Applications

Polymorphism can be applied to many scenarios, such as controls in graphical interface programming, characters and NPCs in game development, and so on. The following is a simple application case:

class Shape {
    
    
public:
    virtual void draw() = 0;
};

class Rectangle : public Shape {
    
    
public:
    void draw() {
    
    
        cout << "Drawing a rectangle." << endl;
    }
};

class Circle : public Shape {
    
    
public:
    void draw() {
    
    
        cout << "Drawing a circle." << endl;
    }
};

int main() {
    
    
    vector<Shape*> shapes;
    shapes.push_back(new Rectangle());
    shapes.push_back(new Circle());
    for (int i = 0; i < shapes.size(); i++) {
    
    
        shapes[i]->draw();
    }
    return 0;
}

The output is:

Drawing a rectangle.
Drawing a circle.

In this example, we have used pure virtual functions and vector containers to manage multiple shape objects. Whether it is a rectangle or a circle, it can be drawn by calling the same draw() function.

In summary, polymorphism in C++ is a very useful programming mechanism that increases code reusability and flexibility. It is recommended to use polymorphism under appropriate circumstances to optimize code structure and improve program efficiency.

Guess you like

Origin blog.csdn.net/duck251/article/details/130535284
Recommended