Skip to content

Commit ffb0d93

Browse files
committed
Adding the changes for lab 3
1 parent a20df8f commit ffb0d93

File tree

5 files changed

+152
-27
lines changed

5 files changed

+152
-27
lines changed

Shapes/Box.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,72 @@
11
#include "Box.hpp"
2+
#include "assert.h"
23

34

4-
5+
Box::Box()
6+
{
7+
this->setLength(0);
8+
this->setWidth(0);
9+
this->setHeight(0);
10+
}
511
Box::Box(std::istream& stream)
612
{
13+
int length;
14+
int width;
15+
int height;
16+
717
//Input from the stream
818
//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;
1322

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
1564
{
1665
return 2*(this->length*this->width +
1766
this->width*this->height +
1867
this->height*this->length);
1968
}
20-
int Box::getVolume()
69+
int Box::getVolume() const
2170
{
2271
return this->length*this->width*this->height;
2372
}

Shapes/Box.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef BOX_HPP
2+
#define BOX_HPP
3+
14
#include <iostream>
25

36

@@ -7,16 +10,28 @@ class Box
710
int width;
811
int height;
912
public:
10-
//Default constructors and destructors
11-
Box() = default;
13+
Box();
14+
Box(int length,int width, int height);
15+
Box(const Box& other);
1216
virtual ~Box() = default;
1317

18+
1419
//This constructor uses the stream
1520
//to find the dimensions of the box
1621
Box(std::istream& stream);
1722

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+
1831
//These functions do exactly what
1932
//they sound like
20-
int getSurfaceArea();
21-
int getVolume();
33+
int getSurfaceArea() const;
34+
int getVolume() const;
2235
};
36+
37+
#endif

Shapes/Rectangle.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,57 @@
11
#include "Rectangle.hpp"
22
#include <iostream>
3+
#include "assert.h"
34

45

5-
int Rectangle::getArea()
6+
int Rectangle::getArea() const
67
{
78
return this->length*this->width;
89
}
910

10-
int Rectangle::getPerimeter()
11+
int Rectangle::getPerimeter() const
1112
{
1213
return this->length*2 + this->width*2;
1314
}
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+
}
1433

1534
Rectangle::Rectangle(std::istream& stream)
1635
{
36+
int length;
37+
int width;
1738
//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+
{
2057
}

Shapes/Rectangle.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#ifndef RECTANGLE_HPP
2+
#define RECTANGLE_HPP
13
#include <iostream>
24

35

@@ -8,15 +10,24 @@ class Rectangle
810
public:
911
//These functions do exactly what
1012
//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);
1321

1422
//This constructor will consume
1523
//some chars from the input stream
1624
//to construct the box
1725
Rectangle(std::istream& stream);
1826

19-
//Default constructor and destructor
20-
Rectangle() = default;
27+
Rectangle();
28+
Rectangle(int l,int w);
29+
Rectangle(const Rectangle& other);
2130
virtual ~Rectangle() = default;
2231
};
32+
33+
#endif

Shapes/ShapesMain.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
#include "Box.hpp"
44
#include "Rectangle.hpp"
55

6+
void PrintRectangle(Rectangle rectangle)
7+
{
8+
std::cout << "Rectangle:\n"
9+
<< "\tLength: " << rectangle.getLength() << '\n'
10+
<< "\tWidth : " << rectangle.getWidth() << '\n'
11+
<< "\tArea: " << rectangle.getArea() << '\n'
12+
<< "\tPerimeter: " << rectangle.getPerimeter()
13+
<< std::endl << std::endl;
14+
}
15+
void PrintBox(Box box)
16+
{
17+
std::cout << "Box:\n"
18+
<< "\tLength: " << box.getLength() << '\n'
19+
<< "\tWidth : " << box.getWidth() << '\n'
20+
<< "\tHeight : " << box.getHeight() << '\n'
21+
<< "\tVolume: " << box.getVolume() << '\n'
22+
<< "\tSurface Area: " << box.getSurfaceArea()
23+
<< std::endl << std::endl;
24+
}
625

726
int main(int arc, char** arv)
827
{
@@ -33,18 +52,12 @@ int main(int arc, char** arv)
3352
if (type == 'R')
3453
{
3554
Rectangle rectangle(file);
36-
std::cout << "Rectangle:\n"
37-
<< "\tArea: " << rectangle.getArea() << '\n'
38-
<< "\tPerimeter: " << rectangle.getPerimeter()
39-
<< std::endl << std::endl;
55+
PrintRectangle(rectangle);
4056
}
4157
else
4258
{
4359
Box box(file);
44-
std::cout << "Box:\n"
45-
<< "\tVolume: " << box.getVolume() << '\n'
46-
<< "\tSurface Area: " << box.getSurfaceArea()
47-
<< std::endl << std::endl;
60+
PrintBox(box);
4861
}
4962
}
5063
}

0 commit comments

Comments
 (0)