Implementation of C++ polymorphism and solutions to common problems

王林
Release: 2023-10-09 17:07:48
Original
1097 people have browsed it

Implementation of C++ polymorphism and solutions to common problems

The implementation of C polymorphism and solutions to common problems

Introduction:
In C programming, polymorphism is an important concept and feature . It allows us to use pointers or references of base classes to operate objects of derived classes, thereby achieving program flexibility and reusability. This article will introduce how polymorphism is implemented in C and discuss some common polymorphism problems and their solutions. Meanwhile, for better understanding, we will provide concrete code examples.

1. How to implement polymorphism
The implementation of polymorphism in C mainly relies on the mechanisms of inheritance and virtual functions. Let's take animals as an example to introduce a common polymorphism scenario:

#include  using namespace std; class Animal { public: virtual void sound() { cout << "Animal makes sound." << endl; } }; class Dog : public Animal { void sound() override { cout << "Dog barks." << endl; } }; class Cat : public Animal { void sound() override { cout << "Cat meows." << endl; } }; int main() { Animal* animal1 = new Dog(); animal1->sound(); Animal* animal2 = new Cat(); animal2->sound(); delete animal1; delete animal2; return 0; }
Copy after login

In the above code, we define a base class Animal, and its two derived classes Dog and Cat. The sound() method in the base class Animal is declared as a virtual function, and the corresponding method in its derived class is marked with the keyword "override".

In the main function, we point to the derived class objects Dog and Cat through the base class pointers animal1 and animal2 respectively, and achieve polymorphism by calling the sound() method. Depending on the specific derived class type, the program will dynamically call the sound() method in the corresponding derived class to produce different outputs.

2. Common polymorphism problems and solutions

  1. Base class pointer accesses derived class member functions:

In the above example, the base class The class pointers animal1 and animal2 call the sound() methods in the derived classes Dog and Cat respectively. This is because the sound() method is declared as a virtual function, thus achieving dynamic binding. Polymorphism cannot be achieved without declaring the sound() method as a virtual function.

  1. Virtual function coverage problem in multi-level derivation:

When a derived class derives a new derived class again, if the virtual function of the base class is overridden To write, the keyword "override" must be used to overwrite the previous virtual function. This ensures that the derived class correctly overrides the virtual functions of the base class.

class Animal { public: virtual void sound() { cout << "Animal makes sound." << endl; } }; class Dog : public Animal { void sound() override { cout << "Dog barks." << endl; } }; class Cat : public Dog { void sound() override { cout << "Cat meows." << endl; } }; int main() { Animal* animal = new Cat(); animal->sound(); // 输出 "Cat meows." delete animal; return 0; }
Copy after login

In the above code, the derived class Cat inherits Dog, and Dog inherits Animal. The override of the sound() method in Cat is marked with the keyword "override" to clearly indicate that the method is a correct override of the virtual function in the base class. The compiler will issue a warning if the "override" keyword is omitted.

  1. Polymorphism problem of destructor:

When using polymorphism, the destructor of the base class must be declared as a virtual destructor. In this way, when deleting a derived class object, the destructor of the derived class will be automatically called to avoid memory leaks.

class Animal { public: virtual ~Animal() { cout << "Animal destructor called." << endl; } }; class Dog : public Animal { public: ~Dog() override { cout << "Dog destructor called." << endl; } }; int main() { Animal* animal = new Dog(); delete animal; // 输出 "Dog destructor called." return 0; }
Copy after login

In the above code, the destructor of the base class Animal is declared as a virtual destructor. When we delete a derived class object through the base class pointer, the destructor of the derived class will be called correctly to destroy the derived class object.

Summary:
The implementation of C polymorphism mainly relies on the mechanisms of inheritance and virtual functions. Dynamic binding can be achieved by declaring the member functions of the base class as virtual functions and overriding them in the derived class using the "override" keyword, thereby achieving polymorphism. In addition, you need to pay attention to declaring the destructor of the base class as a virtual destructor to avoid memory leaks. In actual programming, polymorphism can improve the flexibility and scalability of the code, making the program easier to maintain and modify.

The above is the detailed content of Implementation of C++ polymorphism and solutions to common problems. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!