forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path74-rectangle1_separate.cpp
73 lines (57 loc) · 1.45 KB
/
74-rectangle1_separate.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
#include <iostream>
#include <stdexcept>
#include <string>
class Rectangle
{
public:
Rectangle();
Rectangle(double initial_width, double initial_height);
double get_width();
double get_height();
double get_area();
static Rectangle create_unit_square();
private:
double width;
double height;
};
Rectangle::Rectangle() : width{0}, height{0}
{
std::cout << "Rectangle Default Constructor" << std::endl;
}
Rectangle::Rectangle(double w, double h) : width{w}, height{h}
{
std::cout << "Rectangle Parameterized Constructor" << std::endl;
if (w < 0 || h < 0)
{
throw std::runtime_error{"Invalid dimensions"};
}
}
double Rectangle::get_width()
{
return width;
}
double Rectangle::get_height()
{
return height;
}
double Rectangle::get_area()
{
return get_width() * get_height();
}
Rectangle Rectangle::create_unit_square()
{
Rectangle result{1, 1};
return result;
}
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;
}