Browse papers
A

Section A: Long Answer Questions

Attempt all / any as specified.

4 questions
1long12 marks

(a) Distinguish between a structure and a class in C++ with respect to default access specifiers and member functions. (b) Explain the concept of a constructor and a destructor, stating the rules they must follow. (c) Write a complete C++ program that defines a class BankAccount having private data members accountNo, balance and an owner name. Provide a parameterized constructor to initialize an account, member functions deposit(amount) and withdraw(amount) (the latter must reject withdrawals that exceed the balance), and a destructor that prints a message when an object is destroyed. Demonstrate the class in main().

classes-and-objectsconstructors-and-destructors
2long14 marks

(a) Define inheritance and explain the different forms of inheritance (single, multiple, multilevel, hierarchical and hybrid) with a labelled diagram for each. (b) What is the diamond problem in multiple inheritance and how does C++ resolve it using virtual base classes? (c) Using a base class Shape with a virtual function area(), derive classes Circle and Rectangle, and write a program that uses a base-class pointer to demonstrate run-time polymorphism.

inheritancepolymorphismvirtual-functions
3long14 marks

(a) What is operator overloading? List the operators that cannot be overloaded in C++ and explain why an overloaded operator cannot change the precedence or arity of the operator. (b) Differentiate between overloading an operator as a member function and as a friend function, giving one situation where a friend function is necessary. (c) Design a class Complex and overload the + operator (as a member function) and the << operator (as a friend function) so that two complex numbers can be added and printed.

operator-overloadingconstructors-and-destructors
4long10 marks

(a) Explain the difference between a function template and a class template with a suitable example of each. (b) Write a generic function template maximum() that returns the larger of two values of any comparable type. (c) Briefly describe what the Standard Template Library (STL) provides under the categories containers, iterators and algorithms, naming two examples of each.

templates-stlexception-handling
B

Section B: Short Answer Questions

Attempt all / any as specified.

8 questions
5short6 marks

Explain the four pillars of object-oriented programming: encapsulation, abstraction, inheritance and polymorphism. Briefly state how each is supported in C++.

oop-conceptsclasses-and-objects
6short6 marks

What is a copy constructor? When is it invoked? Write the prototype of a copy constructor for a class Student and explain the difference between a shallow copy and a deep copy.

constructors-and-destructors
7short6 marks

Differentiate between compile-time polymorphism and run-time polymorphism. What is a pure virtual function and how does it make a class abstract? Give a short code fragment.

polymorphismvirtual-functions
8short6 marks

Explain the visibility of base-class members in a derived class for public, protected and private modes of inheritance using a table. Which members of the base class are never inherited?

inheritanceclasses-and-objects
9short6 marks

Describe the C++ exception-handling mechanism using try, throw and catch. Write a program that throws and catches a DivideByZero exception when a division by zero is attempted.

exception-handling
10short6 marks

Explain the C++ stream class hierarchy for file handling (ifstream, ofstream, fstream). Write a program that writes three lines of text to a file data.txt and then reads the file back and displays its contents on the screen.

file-io-streams
11short6 marks

Differentiate between text-mode and binary-mode file access in C++. Explain the use of the seekg(), seekp() and tellg() functions for random access of a file with a short example.

file-io-streams
12short6 marks

(a) What is function overloading and how does it differ from a function template? (b) Explain this pointer with an example, and state why an overloaded assignment operator typically returns *this.

templates-stloperator-overloading