Delegating Constructors
A delegating constructor is a constructor that calls another constructor from the same class. This is useful when you want to reuse code from another constructor in the same class. By delegating to another constructor, you can avoid duplicating code and keep your code clean and concise.
Here is an example of how delegating constructors can be used in C++:
#include <iostream>
class Rectangle {
public:
Rectangle(int width, int height) : width_(width), height_(height) {}
Rectangle(int side) : Rectangle(side, side) {}
int width() const { return width_; }
int height() const { return height_; }
private:
int width_;
int height_;
};
int main() {
Rectangle rect1(10, 20);
std::cout << "Rectangle 1: " << rect1.width() << "x" << rect1.height() << '\n';
Rectangle rect2(30);
std::cout << "Rectangle 2: " << rect2.width() << "x" << rect2.height() << '\n';
return 0;
}
In this example, the Rectangle class has two constructors: one that takes two arguments (width and height) and another that takes a single argument (side). The second constructor delegates to the first constructor by calling Rectangle(side, side). This results in a square with the same width and height equal to the side argument.
Non-Static Data Member Initializers
Non-static data member initializers are a C++11 feature that allows you to initialize data members directly in the class definition. This is a convenient way to set default values for data members, and it can be useful when you want to avoid duplicating code in constructors.
Here is an example of how non-static data member initializers can be used in C++:
#include <iostream>
class Rectangle {
public:
Rectangle(int width, int height) : width_(width), height_(height) {}
int width() const { return width_; }
int height() const { return height_; }
private:
int width_ = 10;
int height_ = 20;
};
int main() {
Rectangle rect;
std::cout << "Rectangle: " << rect.width() << "x" << rect.height() << '\n';
return 0;
}
In this example, the width_ and height_ data members are initialized directly in the class definition with the default values of 10 and 20, respectively. This means that if you create an object of the Rectangle class without passing any arguments to the constructor, the width and height will be set to the default values.
Default Function Arguments
Default function arguments are a way to provide default values for function parameters. This is useful when you want to allow the caller to provide an argument or use a default value if no argument is provided.
Here is an example of how default function arguments can be used in C++:
#include <iostream>
int add(int a, int b = 10) {
return a + b;
}
int main() {
std::cout << "Sum 1: " << add(1) << '\n';
std::cout << "Sum 2: " << add(1, 2) << '\n';
return 0;
}
In this example, the add function takes two arguments, a and b. The second argument, b, has a default value of 10, so if no value is provided for b, it will default to 10. This means that if you call add(1), the result will be 11, and if you call add(1, 2), the result will be 3.