forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72-basic_rectangle.cpp
133 lines (107 loc) · 3.24 KB
/
72-basic_rectangle.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <stdexcept>
#include <string>
class Rectangle
{
public:
//Task: Add a default constructor (which initializes the rectangle
// to have width = height = 0) and a parameterized constructor
// which takes a width and height (and verifies that they are
// valid).
Rectangle()
{
std::cout << "Default no-arg constructor" << std::endl;
width = 0;
height = 0;
}
Rectangle(double side)
{
std::cout << "Constructor for a square" << std::endl;
if (side < 0)
{
throw std::runtime_error{"Invalid square"};
}
width = height = side;
}
Rectangle(double w, double h)
{
std::cout << "Constructor with w and h" << std::endl;
if (w < 0 || h < 0)
{
throw std::runtime_error{"Invalid rect w or h"};
}
width = w;
height = h;
}
//Task: Add get_width and get_height functions which return the width
// and height of this rectangle
double get_width()
{
return width;
}
double get_height()
{
return height;
}
void set_width(double w)
{
if (w < 0)
{
throw std::runtime_error{"Invalid width"};
}
width = w;
}
void set_height(double h)
{
if (h < 0)
{
throw std::runtime_error{"Invalid height"};
}
height = h;
}
// double get_area()
// {
// return width * height;
// }
double get_area()
{
return get_width() * get_height();
}
//Task: Add a get_area function which returns the area of this rectangle
//Task: Add a static member function create_unit_square which creates
// and returns a rectangle with width and height equal to 1
static Rectangle create_unit_square()
{
// Rectangle res{1, 1};
// Rectangle res{1};
Rectangle res{};
res.width = res.height = 1;
return res;
}
private:
double width;
double height;
};
int main()
{
Rectangle R{}; //This uses the default constructor
Rectangle square{10}; //This uses the default constructor
Rectangle R2{6, 10}; //This uses the parameterized constructor
R.set_width(4);
R.set_height(30);
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;
std::cout << "square has width " << square.get_width() << std::endl;
std::cout << "square has height " << square.get_height() << std::endl;
std::cout << "square has area " << square.get_area() << std::endl;
Rectangle usquare{Rectangle::create_unit_square()};
// Rectangle usquare{R2.create_unit_square()};
std::cout << "usquare has width " << usquare.get_width() << std::endl;
std::cout << "usquare has height " << usquare.get_height() << std::endl;
std::cout << "usquare has area " << usquare.get_area() << std::endl;
return 0;
}