2075

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.

  1. 110 marksData ConversionAnswer

    Explain the concept of user-defined to user-defined data concession with the conversion routine located in the destination class.[10]

    In C++, type conversion refers to converting a value from one data type to another. When both the source and destination are user-defined data types (i.e., classes), a special conversion routine must be written., "classes are user-define...

  2. 210 marksPublic and Private InheritanceAnswer

    Depict the difference between private and public derivation. Explain derived class constructor with suitable program.[10]

    --- When a derived class inherits from a base class, the visibility of inherited members in the derived class depends on the type of derivation used. Feature Public Derivation Private Derivation --------- Base class public members Remain...

  3. 310 marksStream Class Hierarchy for Console Input/OAnswer

    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-hierarchy
    

    Description of Each Class

    ClassHeaderDescription
    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. Contains cin object and defines >> (extraction operator).
    ostream<iostream>Derived from ios. Handles output operations. Contains cout, cerr, clog objects and defines << (insertion operator).
    iostream<iostream>Derived from both istream and ostream. 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

    • cout is an object of ostream class
    • cin is an object of istream class
    • Writing to a file uses an ofstream or fstream object instead of cout, but uses the same << operator
    • Reading from a file uses an ifstream or fstream object instead of cin, but uses the same >> operator

    Part 2: Overloading Extraction (>>) and Insertion (<<) Operators

    Important Rules (from notes)

    1. cout is an object of ostream class and cin is an object of istream class.
    2. These operators must be overloaded as global functions (not member functions).
    3. 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: 2
    

    Explanation of Key Points

    PointExplanation
    friend keywordAllows the global function to access private members real and imag
    ostream& 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 << c2
    Global functionThese are NOT member functions; they are defined outside the class

    Note: The operators must return a reference to the stream (ostream& or istream&) so that chained operations like cout << c1 << c2 work correctly.

  4. 45 marksPointersAnswer

    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]

    A string in C++ is a character array. To reverse it, we swap the character at the first position with the character at the last position, then the second with the second-to-last, and so on, continuing until we reach the middle of the str...

  5. 55 marksThe Default Copy ConstructorAnswer

    Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor. [5]

    A copy constructor is a special constructor that creates a new object as a copy of an existing object of the same class. It is called automatically when: - An object is initialized with another object of the same class - An object is pas...

  6. 65 marksTypes of inheritanceAnswer

    Briefly explain types of inheritance used in object oriented programming. [5]

    Inheritance is one of the key features of Object Oriented Programming that allows a new class (derived/child class) to acquire the properties and behaviors of an existing class (base/parent class)., "through inheritance, we can eliminate...

  7. 75 marksStatic membersAnswer

    Create a real scenario where static data members are useful. Explain with suitable program. [5]

    A static data member is a class member that is shared by all objects of that class. It is declared using the static keyword inside the class. Unlike regular data members (where each object has its own copy), a static data member has only...

  8. 85 marksFunctionsAnswer

    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]

    The principal reason for using default arguments in a function is to make function calls more flexible and convenient. A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the...

  9. 95 marksFunctionsAnswer

    "An overloaded function appears to perform different activities depending the kind of data send to it." Justify the statement with appropriate example. [5]

    Function overloading is a feature of C++ that allows two or more functions to have the same name but different parameter lists (different number, type, or order of arguments). The compiler distinguishes between overloaded functions based...

  10. 105 marksFunction templatesAnswer

    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]

    A function template allows writing a single generic function that works with multiple data types. The compiler automatically generates the appropriate version of the function for each data type used. Syntax: Passing arguments by referenc...

  11. 115 marksExceptional HandlingAnswer

    Explain how exceptions are used for handling C++ error in a systematic and OOP-oriented way with the design that includes multiple exceptions. [5]

    Exception handling is a mechanism in C++ that allows a program to detect and respond to runtime errors in a structured, OOP-oriented way. As stated in the notes, it is used "to develop a reliable and robust system" and "prevents the sepa...

  12. 125 marksUnformatted Input/OutputAnswer

    How is character I/O different from Binary I/O? Explain with examples. [5]

    --- Character I/O (also called Text Mode I/O) deals with data as a sequence of human-readable characters. Data is stored and read in ASCII/text format. - Uses functions like get(), put(), getline(), , << - Data is converted to/from text ...