Home  >  Article  >  Backend Development  >  Virtual functions and dynamic binding in C++ function overloading and rewriting

Virtual functions and dynamic binding in C++ function overloading and rewriting

WBOY
WBOYOriginal
2024-04-20 14:09:01658browse

Function overloading and rewriting: Function overloading allows the creation of functions of the same name with different parameter lists in the same class, while function rewriting is to override the function of the same name of the base class with a subclass in the inheritance relationship. Virtual functions and dynamic binding: Virtual functions allow subclasses to implement parent class methods in their own way and declare them with the virtual keyword in the function signature. Dynamic binding determines the called function implementation at runtime, and it occurs when a parent class pointer points to a child class object. In the case of overriding, even if the virtual function is called through the parent class pointer, the implementation of the subclass will also be called, which reflects the advantages of dynamic binding.

C++ 函数重载和重写中的虚函数与动态绑定

Function overloading and rewriting in C: The role of virtual functions and dynamic binding

In C, functions Overloading and rewriting are two key concepts that enable code scalability and flexibility. The difference between them is:

  • Function overloading: In the same class, functions with different parameter lists have the same name.
  • Function rewriting: In an inheritance relationship, a function in a subclass overrides a function in the base class with the same name and parameter list.

Virtual functions and dynamic binding play a vital role in function rewriting.

Virtual functions

Virtual functions are special functions that allow subclasses to implement parent class methods in different ways. They are declared by specifying the virtual keyword in the function signature. For example:

class Base {
public:
    virtual void print() { cout << "Base class print" << endl; }
};

Dynamic Binding

Dynamic binding is the process of determining at run time which implementation version of a function to call. Dynamic binding occurs when a parent class pointer points to a child class object. For example:

Base* basePtr = new Derived();  // 指向 Derived 对象的 Base 指针
basePtr->print();  // 调用 Derived::print()

In the case of overriding, the subclass's implementation of the virtual function will be called, even if it is called through the parent class pointer, which is one of the advantages of dynamic binding.

Practical Case

Consider the following code, which shows how function overriding and virtual functions work together in a real-world scenario:

class Animal {
public:
    virtual string speak() { return "Default animal sound"; }
};

class Dog : public Animal {
public:
    virtual string speak() override { return "Woof"; }
};

class Cat : public Animal {
public:
    virtual string speak() override { return "Meow"; }
};

int main() {
    Animal* animalPtr;  // 声明父类指针

    // 分别创建 Dog 和 Cat 对象并将其分配给 animalPtr
    animalPtr = new Dog();
    cout << animalPtr->speak() << endl;  // 输出 "Woof"

    animalPtr = new Cat();
    cout << animalPtr->speak() << endl;  // 输出 "Meow"

    return 0;
}

In this In the example, the Animal class contains a virtual function speak(), which is overridden by the subclasses Dog and Cat. When animalPtr points to different subclass objects, calling speak() will be dynamically bound according to the actual type of the object, thereby outputting different sounds.

By using virtual functions and dynamic binding, we can write flexible and extensible code, and can choose different function implementations according to the type of object at runtime.

The above is the detailed content of Virtual functions and dynamic binding in C++ function overloading and rewriting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn