Chapter 10 - Worked Exercises 1
Exercise 1
In this example you will learn the basics of inheritance to implement a class hierarchy of polygons.
A polygon can be defined as a shape of three or more edges. A triangle is a polygon of 3 edges, a square is a polygon of 4 edges, etcetera.
Part 0 - Polygon
Implement a class
Polygonthat stores the edges as a vector of integer values which represents each edge's length.Create a
Polygon::getPerimeter()function that calculates and returns the perimeter of the polygon; this is simply the sum of all the lengths of the edges of the polygon.Create a
Polygon::getArea()that simply returns-1
Part 1 - Triangle
Using the
Polygonclass you have created in previous section, create a classTrianglewhich inherits fromPolygonThe
Triangleclass should have a constructor that accepts 3 integer values representing the three edges of a triangle.You can assume that the values passed into the constructor for
Trianglewill always be valid
Without any further modifications, the below code should properly return the perimeter of the triangle:
int main() {
Triangle tri(5, 5, 5);
std::cout << tri.getPerimeter() << std::endl; // This should print 15
}Create a
Triangle::getArea()function which overrides thePolygon::getArea()function, and returns the area of a triangle described by the following formula:
Where,
Ais the area of the trianglepis half the perimeter of the trianglea,b, andcare the respective lengths of each edge
Part 2 - Rectangle
Using the
Polygonclass you have created in previous section, create a classRectanglewhich inherits fromPolygonThe
Rectangleclass should have a constructor that accepts 2 integer values representing the width and the height of the rectangle.
Without any further modifications, the below code should properly return the perimeter of the rectangle:
int main() {
Rectangle rect(5, 10);
std::cout << rect.getPerimeter() << std::endl; // This should print 30
}Create a
Rectangle::getArea()function which overrides thePolygon::getArea()function, and returns the area of a rectangle described by the following formula:
Where,
Ais the area of the rectangleaandbare the lengths and widths of the rectangle
Part 3 - Square
Recall that all squares are rectangles, but not all rectangles are squares. This implies that squares are more specific than rectangles.
Using the
Rectangleclass from the previous section, create a classSquarewhich inherits fromRectangleThe constructor for the
Squareclass should accept only 1 integer representing the length of each side of the square.Use the list initialiser syntax to call the
Rectangleconstructor when constructing theSquare
Without any further modifications, the below code should properly return the perimeter of the square:
int main() {
Square square(10);
std::cout << square.getPerimeter() << std::endl; // This should print 40
}You do not need to implement the Square::getArea() function for Square , since the formula to calculate the area for a Square is the same as that to a Rectangle.
The following function should just work:
int main() {
Square square(10);
std::cout << square.getArea() << std::endl; // This should print 100
}Part 4 - Describe
Now that you have a couple of classes derived from Polygon, let's create a function that will use instances of Polygon (which include Square, Rectangle, and Square) as parameters.
Consider the following function definition:
void describe(Polygon polygon) {
std::cout << "This polygon has a perimeter of " << polygon.getPerimeter() << std::endl;
std::cout << "This poltgon has an area of " << polygon.getArea() << std::endl;
};Will the following program compile? Explain your answer.
int main() {
Triangle tri(5, 5, 5);
describe(tri);
Rectangle rect(10, 5);
describe(rect);
Square square(5);
describe(square);
}Does the above program output the result that you expect? Explain your observations.
If it does not behave as you expect, how can this be fixed in such a way that it will?
Last updated
Was this helpful?