forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path75-rectangle1_alternate.cpp
69 lines (58 loc) · 1.5 KB
/
75-rectangle1_alternate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdexcept>
#include <string>
class Rectangle
{
public:
Rectangle() : width{0}, area{0}
{
std::cout << "Rectangle Default Constructor" << std::endl;
}
Rectangle(double initial_width, double initial_height)
{
std::cout << "Rectangle Parameterized Constructor" << std::endl;
if (initial_width < 0 || initial_height < 0)
{
throw std::runtime_error{"Invalid dimensions"};
}
width = initial_width;
area = initial_width * initial_height;
}
double get_width()
{
return width;
}
double get_height()
{
if (area == 0)
{
//Hmm...
return 0;
}
return get_area() / get_width();
}
double get_area()
{
return area;
}
static Rectangle create_unit_square()
{
Rectangle result{1, 1};
return result;
}
private:
double width{0};
double area{0};
};
int main()
{
Rectangle R{}; //This uses the default constructor
Rectangle R2{6, 10}; //This uses the parameterized constructor
std::cout << "R has width " << R.get_width() << std::endl;
std::cout << "R has height " << R.get_height() << std::endl;
std::cout << "R has area " << R.get_area() << std::endl;
std::cout << "R2 has width " << R2.get_width() << std::endl;
std::cout << "R2 has height " << R2.get_height() << std::endl;
std::cout << "R2 has area " << R2.get_area() << std::endl;
return 0;
}