College of Science, Engineering and Technology
⋄
ASSIGNMENT 2
Year Module — 2026
⋄
Module Code: COS3711
Module Name: Advanced Programming
Assignment No.: 2
Semester: Year Module 2026
Submitted in partial fulfilment of the requirements for Advanced Programming (COS3711)
at the University of South Africa.
,UNISA | COS3711 Assignment 2 — Advanced Programming
Question 1: Shape Inheritance Hierarchy and Qt GUI Application
This question requires building a Qt C++ application that draws shapes (squares, circles,
rectangles, and ellipses) using an abstract inheritance hierarchy. The design separates com-
mon pen and brush attributes into a base class, delegates one-property shapes (circles and
squares) to a mid-level abstract class, and handles two-property shapes (ellipses and rectan-
gles) through a further subclass.
1.1 Class Hierarchy Design
The hierarchy shown in the assignment UML follows a clean stratification. The Shape base
class holds three protected attributes: penWidth (int), penColour (QColor), and fillColour
(QColor). It declares draw() as a pure virtual function, making it abstract.
Shape1Property inherits from Shape and adds property1 (int), which represents the radius
for a circle or the side length for a square. It also declares draw() as pure virtual.
Shape2Property inherits from Shape1Property and adds property2 (int), representing the
second dimension needed by ellipses and rectangles. It too declares draw() as pure virtual.
Circle and Square each inherit directly from Shape1Property and provide concrete draw()
implementations.
Ellipse and Rectangle inherit from Shape2Property and provide their own concrete draw()
implementations.
Page 1 of 32
, UNISA | COS3711 Assignment 2 — Advanced Programming
Shape (abstract)
#penWidth: int
#penColour: QColor
#fillColour: QColor
+draw() = 0
Shape1Property (abstract)
#property1: int
+draw() = 0
Circle Square
+Circle() +Square()
+draw() +draw()
Shape2Property (abstract)
#property2: int
+draw() = 0
Ellipse Rectangle
+Ellipse() +Rectangle()
+draw() +draw()
Figure 1: Shape inheritance hierarchy for COS3711 Assignment 2
1.2 Header Files
shape.h
Listing 1: shape.h – Abstract base class
1 # ifndef SHAPE_H
2 # define SHAPE_H
3
4 # include < QColor >
5 # include < QPainter >
6
7 class Shape {
8 public :
9 Shape () ;
10 Shape ( int penWidth , QColor penColour , QColor fillColour ) ;
11 virtual ~ Shape () {}
12
13 // Pure virtual -- all concrete shapes must implement this
Page 2 of 32
, UNISA | COS3711 Assignment 2 — Advanced Programming
14 virtual void draw ( QPainter & painter ) = 0;
15
16 // Getters
17 int getPenWidth () const { return penWidth ; }
18 QColor getPenColour () const { return penColour ; }
19 QColor getFillColour () const { return fillColour ; }
20
21 // Setters
22 void setPenWidth ( int w ) { penWidth = w ; }
23 void setPenColour ( QColor c ) { penColour = c ; }
24 void setFillColour ( QColor c ) { fillColour = c ; }
25
26 protected :
27 int penWidth ;
28 QColor penColour ;
29 QColor fillColour ;
30 };
31
32 # endif // SHAPE_H
Page 3 of 32