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 Polygon that 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 Polygon class you have created in previous section, create a class Triangle which inherits from Polygon

  • The Triangle class 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 Triangle will 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 the Polygon::getArea() function, and returns the area of a triangle described by the following formula:

A=p∗(p−a)∗(p−b)∗(p−c)A = \sqrt{p* (p-a) * (p-b) * (p-c)}

Where,

  • A is the area of the triangle

  • p is half the perimeter of the triangle

  • a, b, and c are the respective lengths of each edge

Part 2 - Rectangle

  • Using the Polygon class you have created in previous section, create a class Rectangle which inherits from Polygon

  • The Rectangle class 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 the Polygon::getArea() function, and returns the area of a rectangle described by the following formula:

A=a∗bA = a * b

Where,

  • A is the area of the rectangle

  • a and b are 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 Rectangle class from the previous section, create a class Square which inherits from Rectangle

  • The constructor for the Square class should accept only 1 integer representing the length of each side of the square.

    • Use the list initialiser syntax to call the Rectangle constructor when constructing the Square

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?