Polymorphism is
one of the central ideas of object-orientation (OO): that a single object may take on multiple different roles within a program.
The word originates from Greek,
meaning
– “poly”: many
– “morph”: forms
class Shape
{
public:
virtual double area() = 0;
virtual double perimeter() = 0;
}
//=0 does what?The “= 0” on each method indicates that our class Shape does not define the method – it is the responsibility of any class fulfilling this role to implement them instead.
class Shape
{
public:
virtual double area() = 0;
virtual double perimeter() = 0;
}
//virtual means what?The keyword “virtual” on the methods indicates that
Shape expects implementing classes to provide their own
definition, and will allow those to be accessed from the
Shape perspective.
class Circle: public Shape
{
public:
Circle(double r);
double area();
double perimeter();
void draw();
private:
double radius;
}
//public shape allows what?public shape-indicates that
Circle is inheriting the specifications and
preexisting blueprint of the type Shape.
public indicates that the original access
modifiers of Shape should remain
unchanged.