Home > Backend Development > C++ > C++ function overloading and function default parameters

C++ function overloading and function default parameters

王林
Release: 2024-04-13 18:03:02
Original
768 people have browsed it

Yes, C allows function overloading and function default parameters. Function overloading creates functions with the same name but different parameter lists, and the compiler decides which overload to call based on the parameter types. Function default parameters can provide default values ​​for some parameters and use default values ​​when no parameters are provided.

C++ 函数重载和函数默认参数

C function overloading and function default parameters

Function overloading

Function overloading allows us to define multiple functions in the same class with the same name but different parameter lists. The compiler will decide which overloaded function to call based on the parameter types provided when calling.

Syntax:

return_type function_name(parameter_list_1);
return_type function_name(parameter_list_2);
...
Copy after login

Function default parameters

Function default parameters allow us to provide certain parameters when declaring a function default value. If these parameters are not provided when calling, default values ​​are used.

Grammar:

return_type function_name(parameter_type parameter_name = default_value);
Copy after login

Practical case

Suppose we have a function calculateArea, this function You can calculate the area of ​​a circle or rectangle. We can do this using function overloading:

#include <iostream>

using namespace std;

// 计算圆形的面积
double calculateArea(double radius) {
  return 3.14159 * radius * radius;
}

// 计算矩形的面积
double calculateArea(double length, double width) {
  return length * width;
}

int main() {
  double radius, length, width;

  // 计算圆形的面积
  cout << "Enter the radius: ";
  cin >> radius;
  cout << "The area of the circle is: " << calculateArea(radius) << endl;

  // 计算矩形的面积
  cout << "Enter the length and width of the rectangle: ";
  cin >> length >> width;
  cout << "The area of the rectangle is: " << calculateArea(length, width) << endl;

  return 0;
}
Copy after login

In this case, the calculateArea function has two overloads:

  • calculateArea(double radius) is used to calculate the area of ​​a circle, and uses the function default argument to specify a default value of 0 for radius.
  • calculateArea(double length, double width) Used to calculate the area of ​​a rectangle without using the function's default parameters.

The above is the detailed content of C++ function overloading and function default parameters. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template