Inclusion polymorphism (part 2)
This lesson contains approximately 20 minutes of video content.
Polymorphic behavior
Abstract base classes and pure virtual functions
Virtual destructors
Activity: Polymorphism and shapes
Graded Playground Autograder
Activity Prompt:
In today’s exercise, you’ll implement inheritance and polymorphism with shapes. You’ll need to implement a Shape base class, debug a derived class, and create derived classes, which are listed below.
Shape
Member functions:
Function | Description |
---|---|
Shape(); |
Default Constructor for Shape, = Default. Relies on in-class initialization. |
Shape(std::string color); |
Parameterized constructor for Shape. Set the data members with the values stored in their corresponding parameters. |
virtual unsigned int GetArea() const = 0; |
Pure virtual function. Derived class must provide an implementation that computes area for the shape it represents. |
virtual unsigned int GetPerimeter() const = 0; |
Pure virtual function. Derived class must provide an implementation that computes the perimeter for the shape it represents. |
std::string GetColor() const; |
Returns the color_ of the shape. |
Data members:
Data member | Description |
---|---|
std::string color_ = "Illini Blue"; |
Represents the color of the shape. |
Circle
There's already some (broken) code in Circle.cc - Your job is to find/fix the bugs and ensure that the program compiles correctly. (Hint: circle.hpp isn't broken, and we're engineers, so pi = 3!)
Rectangle
Member functions:
Function | Description |
---|---|
Rectangle(); |
Default Constructor for Rectangle, = Default. Relies on in-class initialization. |
Rectangle(std::string color); |
Passes the relevant parameters to the parameterized base class constructor via the initializer list. All other data members are initialized with their default values using in-class initialization. |
Rectangle(std::string color, unsigned height, unsigned width); |
Passes the relevant parameters to the parameterized base class constructor via the initializer list. It uses initializer list to set the derived class's portion with values stored in the corresponding parameters. All other data members are initialized with their default values using in-class initialization. |
unsigned int GetArea() const; |
Computes and subsequently returns the area of the rectangle. |
unsigned int GetPerimeter() const; |
Computes and subsequently returns the perimeter of the rectangle. |
unsigned int GetHeight() const; |
Returns the height_ of the rectangle. |
unsigned int GetWidth() const; |
Returns the width_ of the rectangle. |
Data members:
Data member | Description |
---|---|
unsigned int width_ = 1; |
Represents the width of the rectangle. |
unsigned int height_ = 1; |
Represents the height of the rectangle. |
#include "Shape.hpp"
#include <iostream>
int main() {
//Your code here!
return 0;
}
#ifndef CIRCLE_HPP
#define CIRCLE_HPP
#include "Shape.hpp"
class Circle: public Shape {
public:
Circle() = default;
Circle(std::string color);
Circle(std::string color, unsigned int radius);
unsigned int GetArea() const;
unsigned int GetPerimeter() const;
unsigned int GetRadius() const;
private:
unsigned int radius_ = 1;
};
#endif
#include "Circle.hpp"
//default constructor for circle, parametrized constructor for circle
Circle::Circle(std::string color): Shape(color_) {
}
//parametrized constructor for both circle and shape
Circle::Circle(std::string color, unsigned int radius): Shape(), radius_(radius) {
}
//returns the area
unsigned int Circle:GetArea() const {
return 3 * radius_ * radius_;
}
//returns the perimeter
unsigned int GetPerimeter() const {
return 2 * 3 * radius_;
}
//returns the radius
unsigned int Circle::GetRadius() const {
return radius_;
}
#ifndef SHAPE_HPP
#define SHAPE_HPP
#include <string>
//Your code here!
#endif
#include "Shape.hpp"
//Your code here!
#ifndef RECTANGLE_HPP
#define RECTANGLE_HPP
#include "Shape.hpp"
//Your code here!
#endif
#include "Rectangle.hpp"
//Your code here!
Submit is not supported in this site.