C++
Beginner
1 min read
Class Design: Constructors and Member Initialiser Lists
Example
#include <iostream>
#include <string>
class Rectangle {
public:
// explicit prevents accidental implicit conversion
explicit Rectangle(double width, double height)
: width_(width), height_(height) // member initialiser list
{
if (width_ <= 0 || height_ <= 0)
throw std::invalid_argument("Dimensions must be positive");
}
// const member functions promise not to modify the object
double width() const { return width_; }
double height() const { return height_; }
double area() const { return width_ * height_; }
double perimeter() const { return 2 * (width_ + height_); }
// Mutators that enforce invariants
void setWidth(double w) {
if (w <= 0) throw std::invalid_argument("Width must be positive");
width_ = w;
}
void setHeight(double h) {
if (h <= 0) throw std::invalid_argument("Height must be positive");
height_ = h;
}
void print() const {
std::cout << "Rectangle(" << width_ << " x " << height_
<< ") area=" << area()
<< " perimeter=" << perimeter() << "\n";
}
private:
double width_;
double height_;
};
int main() {
Rectangle r(5.0, 3.0);
r.print();
r.setWidth(10.0);
r.print();
return 0;
}