CSC166 · TU past paper
Object Oriented Programming 2075 question paper
The complete TU 2075 exam paper for Object Oriented Programming (CSC166), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- Q1.10 marksData ConversionHideAnswer
Explain the concept of user-defined to user-defined data concession with the conversion routine located in the destination class.[10]
- Q2.10 marksPublic and Private InheritanceHideAnswer
Depict the difference between private and public derivation. Explain derived class constructor with suitable program.[10]
- Q3.10 marksStream Class Hierarchy for Console Input/OHideAnswer
Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.[10]
Hierarchy of Stream Classes and Operator Overloading
Part 1: Hierarchy of Stream Classes
C++ provides a rich set of stream classes for performing input/output operations. These classes are organized in a hierarchy, all defined in the header files
<iostream>,<fstream>, and<sstream>.Stream Class Hierarchy Diagram
ios (base class) | istream ostream (input) (output) \ / \ / iostream (input+output) / | \ / | \ ifstream fstream ofstream (file in) (file in/out) (file out)csc166-stream-class-hierarchyDescription of Each Class
Class Header Description ios<ios>Base class for all stream classes. Provides common properties like format flags, error state bits, etc. istream<iostream>Derived from ios. Handles input operations. Containscinobject and defines>>(extraction operator).ostream<iostream>Derived from ios. Handles output operations. Containscout,cerr,clogobjects and defines<<(insertion operator).iostream<iostream>Derived from both istreamandostream. Supports both input and output.ifstream<fstream>Derived from istream. Used for reading from files.ofstream<fstream>Derived from ostream. Used for writing to files.fstream<fstream>Derived from iostream. Used for both reading and writing files.Key Points
coutis an object ofostreamclasscinis an object ofistreamclass- Writing to a file uses an
ofstreamorfstreamobject instead ofcout, but uses the same<<operator - Reading from a file uses an
ifstreamorfstreamobject instead ofcin, but uses the same>>operator
Part 2: Overloading Extraction (
>>) and Insertion (<<) OperatorsImportant Rules (from notes)
coutis an object ofostreamclass andcinis an object ofistreamclass.- These operators must be overloaded as global functions (not member functions).
- To allow access to private data members, they must be declared as friend functions inside the class.
Syntax
friend ostream& operator<<(ostream& out, const ClassName& obj); friend istream& operator>>(istream& in, ClassName& obj);Complete Program
#include <iostream> using namespace std; class Complex { private: float real; float imag; public: // Constructor Complex() { real = 0; imag = 0; } Complex(float r, float i) { real = r; imag = i; } // Friend function to overload insertion operator (<<) // Used for output: cout << object friend ostream& operator<<(ostream& out, const Complex& c) { out << "Real Part : " << c.real << endl; out << "Imaginary Part: " << c.imag << endl; return out; } // Friend function to overload extraction operator (>>) // Used for input: cin >> object friend istream& operator>>(istream& in, Complex& c) { cout << "Enter real part: "; in >> c.real; cout << "Enter imaginary part: "; in >> c.imag; return in; } }; int main() { Complex c1, c2; // Using overloaded >> operator cout << "--- Input for Complex Number 1 ---" << endl; cin >> c1; cout << "--- Input for Complex Number 2 ---" << endl; cin >> c2; // Using overloaded << operator cout << "\n--- Complex Number 1 ---" << endl; cout << c1; cout << "--- Complex Number 2 ---" << endl; cout << c2; return 0; }Sample Output
--- Input for Complex Number 1 --- Enter real part: 3 Enter imaginary part: 4 --- Input for Complex Number 2 --- Enter real part: 1 Enter imaginary part: 2 --- Complex Number 1 --- Real Part : 3 Imaginary Part: 4 --- Complex Number 2 --- Real Part : 1 Imaginary Part: 2Explanation of Key Points
Point Explanation friendkeywordAllows the global function to access private members realandimagostream& outReference to the output stream object ( cout)istream& inReference to the input stream object ( cin)return out/return inReturning the stream reference allows chaining like cout << c1 << c2Global function These are NOT member functions; they are defined outside the class Note: The operators must return a reference to the stream (
ostream&oristream&) so that chained operations likecout << c1 << c2work correctly. - Q4.5 marksPointersHideAnswer
Write a member function called reverse() that reverses a string (an array of character). Use a for loop that swaps the first and last characters, then the second and next-to-last character and so on. The string should be passed to reverse() as an argument. [5]
- Q5.5 marksThe Default Copy ConstructorHideAnswer
Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor. [5]
- Q6.5 marksTypes of inheritanceHideAnswer
Briefly explain types of inheritance used in object oriented programming. [5]
- Q7.5 marksStatic membersHideAnswer
Create a real scenario where static data members are useful. Explain with suitable program. [5]
- Q8.5 marksFunctionsHideAnswer
What is the principle reason for using default arguments in the function? Explain how missing arguments and default arguments are handled by the function simultaneously? [5]
- Q9.5 marksFunctionsHideAnswer
"An overloaded function appears to perform different activities depending the kind of data send to it." Justify the statement with appropriate example. [5]
- Q10.5 marksFunction templatesHideAnswer
Create a function called swaps() that interchanges the values of the two arguments sent to it (pass these arguments by reference), make the function into a template, so it can be used with all numerical data types (char, int, float, and so on), write a main() program to exercise the function with several types. [5]
- Q11.5 marksExceptional HandlingHideAnswer
Explain how exceptions are used for handling C++ error in a systematic and OOP-oriented way with the design that includes multiple exceptions. [5]
- Q12.5 marksUnformatted Input/OutputHideAnswer
How is character I/O different from Binary I/O? Explain with examples. [5]