File tree Expand file tree Collapse file tree 5 files changed +152
-27
lines changed Expand file tree Collapse file tree 5 files changed +152
-27
lines changed Original file line number Diff line number Diff line change 1
1
#include " Box.hpp"
2
+ #include " assert.h"
2
3
3
4
4
-
5
+ Box::Box ()
6
+ {
7
+ this ->setLength (0 );
8
+ this ->setWidth (0 );
9
+ this ->setHeight (0 );
10
+ }
5
11
Box::Box (std::istream& stream)
6
12
{
13
+ int length;
14
+ int width;
15
+ int height;
16
+
7
17
// Input from the stream
8
18
// the dimensions of the box
9
- stream >> this ->length ;
10
- stream >> this ->width ;
11
- stream >> this ->height ;
12
- }
19
+ stream >> length;
20
+ stream >> width;
21
+ stream >> height;
13
22
14
- int Box::getSurfaceArea ()
23
+ this ->setLength (length);
24
+ this ->setWidth (width);
25
+ this ->setHeight (height);
26
+ }
27
+ Box::Box (int l,int w,int h)
28
+ {
29
+ this ->setLength (l);
30
+ this ->setWidth (w);
31
+ this ->setHeight (h);
32
+ }
33
+ Box::Box (const Box& other) : length(other.length), width(other.width), height(other.height)
34
+ {
35
+ }
36
+ int Box::getLength () const
37
+ {
38
+ return this ->length ;
39
+ }
40
+ int Box::getWidth () const
41
+ {
42
+ return this ->width ;
43
+ }
44
+ int Box::getHeight () const
45
+ {
46
+ return this ->height ;
47
+ }
48
+ void Box::setLength (int value)
49
+ {
50
+ assert (value >= 0 );
51
+ this ->length = value;
52
+ }
53
+ void Box::setWidth (int value)
54
+ {
55
+ assert (value >= 0 );
56
+ this ->width = value;
57
+ }
58
+ void Box::setHeight (int value)
59
+ {
60
+ assert (value >= 0 );
61
+ this ->height = value;
62
+ }
63
+ int Box::getSurfaceArea () const
15
64
{
16
65
return 2 *(this ->length *this ->width +
17
66
this ->width *this ->height +
18
67
this ->height *this ->length );
19
68
}
20
- int Box::getVolume ()
69
+ int Box::getVolume () const
21
70
{
22
71
return this ->length *this ->width *this ->height ;
23
72
}
Original file line number Diff line number Diff line change
1
+ #ifndef BOX_HPP
2
+ #define BOX_HPP
3
+
1
4
#include < iostream>
2
5
3
6
@@ -7,16 +10,28 @@ class Box
7
10
int width;
8
11
int height;
9
12
public:
10
- // Default constructors and destructors
11
- Box () = default ;
13
+ Box ();
14
+ Box (int length,int width, int height);
15
+ Box (const Box& other);
12
16
virtual ~Box () = default ;
13
17
18
+
14
19
// This constructor uses the stream
15
20
// to find the dimensions of the box
16
21
Box (std::istream& stream);
17
22
23
+ int getLength () const ;
24
+ int getWidth () const ;
25
+ int getHeight () const ;
26
+
27
+ void setLength (int value);
28
+ void setWidth (int value);
29
+ void setHeight (int value);
30
+
18
31
// These functions do exactly what
19
32
// they sound like
20
- int getSurfaceArea ();
21
- int getVolume ();
33
+ int getSurfaceArea () const ;
34
+ int getVolume () const ;
22
35
};
36
+
37
+ #endif
Original file line number Diff line number Diff line change 1
1
#include " Rectangle.hpp"
2
2
#include < iostream>
3
+ #include " assert.h"
3
4
4
5
5
- int Rectangle::getArea ()
6
+ int Rectangle::getArea () const
6
7
{
7
8
return this ->length *this ->width ;
8
9
}
9
10
10
- int Rectangle::getPerimeter ()
11
+ int Rectangle::getPerimeter () const
11
12
{
12
13
return this ->length *2 + this ->width *2 ;
13
14
}
15
+ int Rectangle::getLength () const
16
+ {
17
+ return this ->length ;
18
+ }
19
+ int Rectangle::getWidth () const
20
+ {
21
+ return this ->width ;
22
+ }
23
+ void Rectangle::setLength (int value)
24
+ {
25
+ assert (value >= 0 );
26
+ this ->length = value;
27
+ }
28
+ void Rectangle::setWidth (int value)
29
+ {
30
+ assert (value >= 0 );
31
+ this ->width = value;
32
+ }
14
33
15
34
Rectangle::Rectangle (std::istream& stream)
16
35
{
36
+ int length;
37
+ int width;
17
38
// Input from the stream the length and width
18
- stream >> this ->length ;
19
- stream >> this ->width ;
39
+ stream >> length;
40
+ stream >> width;
41
+
42
+ this ->setLength (length);
43
+ this ->setWidth (width);
44
+ }
45
+ Rectangle::Rectangle ()
46
+ {
47
+ this ->setLength (0 );
48
+ this ->setWidth (0 );
49
+ }
50
+ Rectangle::Rectangle (int length, int width)
51
+ {
52
+ this ->setLength (length);
53
+ this ->setWidth (width);
54
+ }
55
+ Rectangle::Rectangle (const Rectangle& other) : length(other.length), width(other.width)
56
+ {
20
57
}
Original file line number Diff line number Diff line change
1
+ #ifndef RECTANGLE_HPP
2
+ #define RECTANGLE_HPP
1
3
#include < iostream>
2
4
3
5
@@ -8,15 +10,24 @@ class Rectangle
8
10
public:
9
11
// These functions do exactly what
10
12
// you think they do
11
- int getArea ();
12
- int getPerimeter ();
13
+ int getArea () const ;
14
+ int getPerimeter () const ;
15
+
16
+ int getLength () const ;
17
+ int getWidth () const ;
18
+
19
+ void setLength (int value);
20
+ void setWidth (int value);
13
21
14
22
// This constructor will consume
15
23
// some chars from the input stream
16
24
// to construct the box
17
25
Rectangle (std::istream& stream);
18
26
19
- // Default constructor and destructor
20
- Rectangle () = default ;
27
+ Rectangle ();
28
+ Rectangle (int l,int w);
29
+ Rectangle (const Rectangle& other);
21
30
virtual ~Rectangle () = default ;
22
31
};
32
+
33
+ #endif
Original file line number Diff line number Diff line change 3
3
#include " Box.hpp"
4
4
#include " Rectangle.hpp"
5
5
6
+ void PrintRectangle (Rectangle rectangle)
7
+ {
8
+ std::cout << " Rectangle:\n "
9
+ << " \t Length: " << rectangle.getLength () << ' \n '
10
+ << " \t Width : " << rectangle.getWidth () << ' \n '
11
+ << " \t Area: " << rectangle.getArea () << ' \n '
12
+ << " \t Perimeter: " << rectangle.getPerimeter ()
13
+ << std::endl << std::endl;
14
+ }
15
+ void PrintBox (Box box)
16
+ {
17
+ std::cout << " Box:\n "
18
+ << " \t Length: " << box.getLength () << ' \n '
19
+ << " \t Width : " << box.getWidth () << ' \n '
20
+ << " \t Height : " << box.getHeight () << ' \n '
21
+ << " \t Volume: " << box.getVolume () << ' \n '
22
+ << " \t Surface Area: " << box.getSurfaceArea ()
23
+ << std::endl << std::endl;
24
+ }
6
25
7
26
int main (int arc, char ** arv)
8
27
{
@@ -33,18 +52,12 @@ int main(int arc, char** arv)
33
52
if (type == ' R' )
34
53
{
35
54
Rectangle rectangle (file);
36
- std::cout << " Rectangle:\n "
37
- << " \t Area: " << rectangle.getArea () << ' \n '
38
- << " \t Perimeter: " << rectangle.getPerimeter ()
39
- << std::endl << std::endl;
55
+ PrintRectangle (rectangle);
40
56
}
41
57
else
42
58
{
43
59
Box box (file);
44
- std::cout << " Box:\n "
45
- << " \t Volume: " << box.getVolume () << ' \n '
46
- << " \t Surface Area: " << box.getSurfaceArea ()
47
- << std::endl << std::endl;
60
+ PrintBox (box);
48
61
}
49
62
}
50
63
}
You can’t perform that action at this time.
0 commit comments