Home  >  Article  >  Backend Development  >  Detailed explanation of C++ function inheritance: How to use virtual functions to achieve code reuse?

Detailed explanation of C++ function inheritance: How to use virtual functions to achieve code reuse?

王林
王林Original
2024-05-04 10:54:011008browse

Function inheritance allows derived classes to inherit and reuse functions of the base class. By using virtual functions, code reuse can be achieved, in which derived classes can override functions of the same name of the base class and dynamically execute different function versions based on the type of the calling object.

C++ 函数继承详解:如何使用虚函数实现代码复用?

Detailed explanation of C function inheritance: how to use virtual functions to achieve code reuse

Introduction

Function inheritance is an important technique in object-oriented programming (OOP), which allows derived classes to inherit and reuse functions of the base class. By using virtual functions, more flexible and scalable code reuse can be achieved.

What is a virtual function?

Virtual function is a special member function that allows a derived class to override the function of the same name of the base class. When a virtual function is called, the actual function version executed is dynamically determined based on the calling object type.

Syntax

Virtual functions are declared using the keyword virtual, for example:

class Base {
public:
  virtual void print() const; // 虚函数
};

Code reuse

By using virtual functions, we can achieve code reuse. For example, suppose we have a base class Shape that contains a draw() function:

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

We can derive different Shape classes, such as Circle and Rectangle, and implement its own draw() function for each shape: <pre class='brush:cpp;toolbar:false;'>class Circle : public Shape { public: void draw() const override; }; class Rectangle : public Shape { public: void draw() const override; };</pre> When we create When

Circle

and Rectangle objects are called and their draw() function is called, the corresponding derived class function will be executed to achieve different drawing behaviors.

Practical Case

Consider the following code snippet, which demonstrates how to use virtual functions to achieve code reuse:

#include <iostream>

using namespace std;

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

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

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

int main() {
  Shape* shapes[] = {new Circle(), new Rectangle()};

  for (int i = 0; i < 2; i++) {
    shapes[i]->draw();
  }

  return 0;
}

Running this code will Prints the following output:

Drawing a circle
Drawing a rectangle

In this case, the

Shape

class is the base class and it contains the virtual function draw(). Circle and Rectangle are derived classes that override the draw() function to provide shape-specific drawing behavior. By using virtual functions, we can easily call draw() functions of different shapes without knowing their specific types.

The above is the detailed content of Detailed explanation of C++ function inheritance: How to use virtual functions to achieve code reuse?. 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