Assignment 2 – Full 2025 Detailed
Solutions and Distinction-Level
Answer Guide
University of South Africa
Detailed Solutions and Answer Guide for Advanced Programming
July 17, 2025
, 1 Introduction
This document provides comprehensive solutions and a distinction-level answer
guide for COS3711 (Advanced Programming) Assignment 2 for the 2025 academic
year. It includes refactored and expanded content with 100 detailed questions
and answers, focusing on key C++ concepts such as object-oriented program-
ming, templates, exception handling, and memory management. Questions are
highlighted in dark green for clarity, and answers are concise yet thorough, de-
signed to meet distinction-level standards.
2 Assignment Questions and Solutions
Below are 100 refactored questions and detailed answers, covering core topics
from the COS3711 curriculum. Each question is presented in a dark green head-
ing for emphasis.
2.1 What is polymorphism in C++, and how is it implemented
using virtual functions?
Answer: Polymorphism allows objects of different classes to be treated as in-
stances of a common base class, enabling dynamic behavior. In C++, it is im-
plemented using virtual functions, which ensure the correct function is called
based on the object’s actual type at runtime.
1 #include <iostream>
2 using namespace std;
3
4 class Shape {
5 public:
6 virtual void draw() const {
7 cout << ”Drawing a generic shape” << endl;
8 }
9 virtual ~Shape() {}
10 };
11
12 class Circle : public Shape {
13 public:
14 void draw() const override {
15 cout << ”Drawing a circle” << endl;
16 }
17 };
18
19 class Square : public Shape {
20 public:
21 void draw() const override {
22 cout << ”Drawing a square” << endl;
23 }
24 };
25
1