Intake 2023
CS4ME: Introduction to Programming
23 June 2025
PART II: PROGRAMMING (30 min / 30 marks)
Instruction:
Directly write your answer to each question in the Answer Box
Object definition:
• A square object is defined by its area and size. A square object has the following
methods: computeArea, getArea, and print.
• A circle object is defined by its area and radius. A circle object has the following
methods: computeArea, getArea, and print.
• A rectangle object is defined by its area, side1, and side2. A rectangle object has the
following methods: computeArea, getArea, and print.
Method definition:
• computeArea: to compute the area of the object, and store the value in its area
attribute.
• getArea: to return the value of the area attribute.
• print: to output the area of the object depending on the type of the object,
accordingly.
Consider the following main() program and the result when it runs.
1 #include <iostream>
2 #include "polygon.h"
3 #include "square.h"
4 #include "circle.h"
5 #include "rectangle.h"
6 using namespace std;
7
8 int main()
9 {
10 Square s(5);
11 Circle c(3);
12 Rectangle r(3, 4);
13
14 Polygon* p[3];
15 p[0] = &s;
16 p[1] = &c;
17 p[2] = &r;
18
19 for (int i = 0; i < 3; ++i) {
20 p[i]->computeArea();
21 p[i]->print();
22 }
23 }
- Area of the SQUARE: 25
- Area of the CIRCLE: 28.2743
- Area of the RECTANGLE: 12