How to Use Default Parameters for Functions in C++
- Use Default Parameters to Define A Function in C++
- Use Default Parameters to Implement Class Constructor in C++
This article will explain several methods of how to use default parameters for functions in C++.
Use Default Parameters to Define A Function in C++
The concept of default parameters makes it possible to have default argument values specified in a function definition used in case the user does not pass any argument in their places. Thus, functions can have optional arguments, which internally may use some default value to initialize some objects during the function block. Default parameters are specified in a function prototype after each argument name using the =
symbol and the corresponding value. E.g., the following code snippet implements a sumNumbers
function template that can sum four numbers at most. Nevertheless, the user is not required to supply all four values. On the contrary, only two integers are sufficient enough for the operation.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
template <typename T>
T sumNumbers(T x, T y, T z = 0, T w = 0) {
return (x + y + z + w);
}
int main() {
vector<int> vec = {1, 4, 8, 16, 20, 24, 28, 32};
for (size_t i = 0; i < vec.size(); ++i) {
cout << sumNumbers(vec[i], vec[i + 1]) << ": ";
}
cout << endl;
for (size_t i = 0; i < vec.size(); ++i) {
cout << sumNumbers(vec[i], vec[i + 1], vec[i + 2]) << ": ";
}
cout << endl;
for (size_t i = 0; i < vec.size(); ++i) {
cout << sumNumbers(vec[i], vec[i + 1], vec[i + 2], vec[i + 3]) << ": ";
}
cout << endl;
return EXIT_SUCCESS;
}
Output:
5: 12: 24: 36: 44: 52: 60: 32
13: 28: 44: 60: 72: 84: 60: 32:
29: 48: 68: 88: 104: 84: 60: 4145:
Use Default Parameters to Implement Class Constructor in C++
Another useful scenario where the concept of default parameters can be utilized is the class constructor. Sometimes, the class constructors may need to initialize some data members with default values if the user does not provide the arguments. We can implement a constructor with the default parameters, which calls another constructor with default values, as demonstrated in the following example code.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
class MyClass {
string name;
int height;
int width;
public:
MyClass(string s, int h, int w) : name(std::move(s)), height(h), width(w) {}
MyClass() : MyClass("", 0, 0) {}
explicit MyClass(string s) : MyClass(std::move(s), 0, 0) {}
void printMyClass() {
cout << "name: " << name << endl
<< "height: " << height << endl
<< "width: " << width << endl;
}
};
int main() {
MyClass M1;
MyClass M2("Jay", 12, 103);
M1.printMyClass();
cout << endl;
M2.printMyClass();
return EXIT_SUCCESS;
}
Output:
name:
height: 0
width: 0
name: Jay
height: 12
width: 103
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook