Practice

Shape Class Hierarchy

2023/Oct/Nov·Variant 2
HARDObject-Oriented Programming

Class Shape stores a private ShapeName and exposes GetName(). It also defines GetArea(), returning 0 by default.

Classes Circle (INHERITS Shape, adds Radius) and Square (INHERITS Shape, adds Side) each call SUPER.NEW(N) in their constructor and override GetArea() with the correct formula (use 3.14159 for π; no square root needed).

Write procedure DescribeShape(S : Shape) that outputs <name> area = <area rounded to 2 dp>, calling S.GetArea() polymorphically — it must work for either subclass through the same Shape-typed parameter.

The test harness reads "circle" or "square" and a measurement (radius or side), creates the matching object named "MyCircle" / "MySquare", and calls DescribeShape.

Input: circle or square, then a number. Output: One formatted line.

Example:

Input:  circle
        2
Output: MyCircle area = 12.57
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Circle of radius 2
Inputs: circle, 2
Expected: MyCircle area = 12.57
Test 2: Square of side 5
Inputs: square, 5
Expected: MySquare area = 25