Shapes
π Background Information
In this lab we will be calculating the perimeter and area of a variety of shapes.
Circle
- The area of a circle with radius \(r\) is given by \(A = \pi r^2\).
- The circumference of a circle with radius \(r\) is given by \(C = 2 \pi r\).
Rectangle
- The area of a rectangle with sides \(\ell\) and \(w\) is given by \(A = \ell w\).
- The perimeter of a rectangle with sides \(\ell\) and \(w\) is given by \(P = 2 (\ell + w)\).
Right Triangle
- The area of a right triangle with legs \(\ell\) and \(w\) is given by \(A =\frac{1}{2} \ell w\).
- The perimeter of a right triangle with legs \(\ell\) and \(w\) is given by \(P = \ell + w + \sqrt{\ell^2 + w^2}\).
Square
A square is a type of rectangle where the sides \(\ell\) and \(w\) are identical.
Isoceles Right Triangle
A isoceles right triangle is a type of right triangle where the legs \(\ell\) and \(w\) are identical.
π― Problem Statement
The primary goal of this lab is to explore inheritance and polymorphism. The secondary goal of this lab is to become familiar with pull requests and the process of reviewing code on GitHub. Perform the actions outlined in the Acceptance Criteria below.
β Acceptance Criteria
Part One - Polymorphism
In the first part of this lab, we are going to create a polymorphic relationship between an abstract Shape
class and a few derived classes.
- Start off by writing an abstract class called
Shape
with virtual methodsget_area()
andget_perimeter()
. - Create three subclasses that implement the
Shape
interface:Circle
,Rectangle
, andRightTriangle
. In each of these cases, you will need to override theget_area()
andget_perimeter()
methods with their specific implementations. - Test each of the subclasses using
assert
statements. You should be able to create a new instance of each class with whatever dimensions are appropriate and then assert that the area and perimeter are correct. - Commit and push up your code to GitHub using a feature branch. Make sure that you donβt push these changes up to the
main
branch! - Set up a Pull Request to merge these changes into main.
- Have your partner review and approve your code. Be sure to address any changes that they might request.
- Merge your code into the
main
branch. - Make sure all of the partners in the group pull down the latest changes from the repository.
Part Two
We focused on polymorphism in the first part of this lab. Now, we will focus on inheritance.
- Update your program with a new concrete class called
Square
. TheSquare
class should inherit its behavior from theRectangle
class. You may or may not need to overwrite the constructor,get_area()
, andget_perimeter()
withinSquare
. - Update your program with a new concrete class called
IsocelesRightTriangle
. TheIsoscelesRightTriangle
class should inherit its behavior from theRightTriangle
class. You may or may not need to overwrite the constructor,get_area()
, andget_perimeter()
withinIsocelesRightTriangle
. - Write a function with the declaration
void print_area_to_console(Shape *s)
. This function should take aShape
object and print out a nice message which includes the name of the shape and its area. You should be able to use it with all of the shapes that you created! - Test each of the classes that you created via
assert
statements. - Commit and push up your code to GitHub using a feature branch. Make sure that you donβt push these changes up to the
main
branch! - Set up a Pull Request to merge these changes into main.
- Have your partner review and approve your code. Be sure to address any changes that they might request.
- Merge your code into the
main
branch. - Make sure all of the partners in the group pull down the latest changes from the repository.
π Dev Notes
- You need a partner to work on this lab.
- One person in the group should create a GitHub repository to house the project. They should make sure that they add all of the members of the group as collaborators via the Settings tab.
π₯οΈ Example Output
N/A
π Thought Provoking Questions
N/A
πΌ Add-Ons For the Portfolio
N/A
π Works Cited
N/A