Rectangle Sample SolutionΒΆ

#include "error.h"
#include <iostream>

using namespace std;

class Rectangle {
private:
    double width;
    double height;
public:
    Rectangle(double w, double h)
    : width(w), height(h)
    { }

    Rectangle(const Rectangle& other)
    : width(other.width), height(other.height)
    { }

    double get_width() const { return width; }
    double get_height() const { return height; }
    double get_area() const { return width * height; }
    double get_perimeter() const { return 2 * (width + height); }
};

int main() {
    Rectangle r{6, 2.4};  // 6 is the width, 2.4 is the height
                          // if either width or height is 0, or less,
                          // then Rectangle throws an error

    cout << "         Width = " << r.get_width() << endl
         << "        Height = " << r.get_height() << endl
         << "          Area = " << r.get_area() << endl
         << "     Perimiter = " << r.get_perimeter()
         << endl;

    Rectangle b{r};

    cout << "         Width = " << b.get_width() << endl
         << "        Height = " << b.get_height() << endl
         << "          Area = " << b.get_area() << endl
         << "     Perimiter = " << b.get_perimeter()
         << endl;
} // main