Home  >  Article  >  Backend Development  >  How do C++ function overloading and virtual functions work together?

How do C++ function overloading and virtual functions work together?

PHPz
PHPzOriginal
2024-04-26 10:09:02750browse

Function overloading in C allows defining different implementations for functions of the same name with different parameters, while virtual functions allow overriding base class functions in derived classes to achieve polymorphism. Function overloading and virtual functions can work together. By designing a virtual overloaded function in the base class, the derived class can only overload versions of specific parameter combinations, thereby providing more flexible polymorphism, such as calculating different types in practical cases The distance of the shape from its origin.

C++ 函数重载与虚函数如何协作?

Cooperation of function overloading and virtual functions in C

Introduction

C language provides two mechanisms to achieve polymorphism: function overloading and virtual functions. Function overloading allows defining multiple functions with the same name but different behavior based on parameter types. Virtual functions allow functions in a base class to be overridden in a derived class, thus supporting polymorphism in inheritance.

Function Overloading

Function overloading allows defining different implementations for multiple functions with the same name but different parameter lists. The compiler will choose the correct function based on the argument types when called. For example:

int add(int a, int b) {
  return a + b;
}
double add(double a, double b) {
  return a + b;
}

When used:

int sum1 = add(1, 2);  // 呼叫整數版本
double sum2 = add(1.5, 2.3);  // 呼叫浮點版本

Virtual function

Virtual function allows a derived class to override a function in a base class. When a virtual function is called through a base class pointer, the overridden version in the derived class is executed. For example:

class Shape {
public:
  virtual double getArea() const = 0;
};

class Circle : public Shape {
public:
  double getArea() const override {
    return 3.14 * radius * radius;
  }
private:
  double radius;
};

When used:

Shape* shape = new Circle(5.0);
double area = shape->getArea();  // 會呼叫 Circle::getArea()

Collaboration of function overloading and virtual functions

Function overloading and virtual functions can work together to Provides more flexible polymorphism. By designing a virtual overloaded function in the base class, a derived class can overload only versions with specific parameter combinations. For example:

class Shape {
public:
  virtual double getArea(bool isFilled) const {
    return 0.0;
  }
};

class Circle : public Shape {
public:
  double getArea(bool isFilled) const override {
    if (isFilled) {
      return 3.14 * radius * radius;
    } else {
      return 0.0;
    }
  }
};

When used:

Shape* shape = new Circle(5.0);
double filledArea = shape->getArea(true);  // 呼叫 Circle::getArea(bool)
double unfilledArea = shape->getArea(false);  // 呼叫 Shape::getArea(bool)

Practical case

The following is an example of using function overloading and virtual function collaboration in computational geometry Practical case:

class Shape {
public:
  virtual double distanceToOrigin() const = 0;
};

class Point : public Shape {
public:
  double distanceToOrigin() const override {
    return 0.0;
  }
};

class Circle : public Shape {
public:
  double distanceToOrigin() const override {
    return radius;
  }
};

class Rectangle : public Shape {
public:
  double distanceToOrigin() const override {
    return min(x, y);
  }
};

int main() {
  Shape* shapes[] = {new Point(), new Circle(5.0), new Rectangle(3.0, 4.0)};
  for (Shape* shape : shapes) {
    cout << "距離原點: " << shape->distanceToOrigin() << endl;
  }
}

This code demonstrates the use of function overloading and virtual functions to calculate the distance from the origin of different types of shapes.

The above is the detailed content of How do C++ function overloading and virtual functions work together?. 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