2079

BIT153 · TU past paper

Object Oriented Programming 2079 question paper

The complete TU 2079 exam paper for Object Oriented Programming (BIT153), all 12 questions with solved model answers written to the mark scheme.

Tap a question to open its answer.

  1. 110 marksTemplate concept and importanceAnswer

    What do we need template? Why the program terminate after the occurrence of exception?Describe the types of constructor.[4+6]

    A template is a feature in C++ that allows writing generic programs -- functions or classes that work with any data type without rewriting the code for each type. 1. Code Reusability Without templates, we must write separate functions fo...

  2. 210 marksAbstract classes and concrete classesAnswer

    Differentiate between abstract class and concrete class.Write a program to create a class named 'Quadratic' that represents a function of the form f(x) = ax + bx +C, where x is a real variable and a, b, c are real constants. The class must satisfy the following requirements: a. A constructor should be provided that takes the values of a, b and c as arguments. All three of these arguments should default to zero. b. A function that takes a single argument x return the value of f(x). c. All data members should be private.[2+8]

    (a) Difference Between Abstract Class and Concrete Class Feature Abstract Class Concrete Class --------- Definition A class that cannot be instantiated directly and may contain pure virtual functions A class that can be instantiated dire...

  3. 310 marksAnswer

    Explain the chain of constructor and destructor between sub classes and super classes during inheritance with example.Create a class named 'Point' with data members x and y. Overload the + operator to add the value of two objects of this class.[5+5]

    Chain of Constructor and Destructor in Inheritance + Operator Overloading


    (a) Chain of Constructor and Destructor in Inheritance

    Concept

    In C++ inheritance, when an object of a derived (sub) class is created or destroyed, constructors and destructors are called in a specific order involving both the base (super) class and the derived class.

    Constructor Chaining (Order of Execution)

    When a derived class object is created:

    1. Base class constructor is called FIRST
    2. Derived class constructor is called SECOND

    This ensures that the base part of the object is fully initialized before the derived part uses it.

    Destructor Chaining (Order of Execution)

    When a derived class object is destroyed:

    1. Derived class destructor is called FIRST
    2. Base class destructor is called SECOND

    The destructor order is the exact reverse of constructor order.

    Constructor Order:    Base --> Derived
    Destructor Order:     Derived --> Base
    

    Example

    #include <iostream>
    using namespace std;
    
    // Super class (Base class)
    class Animal {
    public:
        Animal() {
            cout << "Animal Constructor called" << endl;
        }
        ~Animal() {
            cout << "Animal Destructor called" << endl;
        }
    };
    
    // Sub class (Derived class)
    class Dog : public Animal {
    public:
        Dog() {
            cout << "Dog Constructor called" << endl;
        }
        ~Dog() {
            cout << "Dog Destructor called" << endl;
        }
    };
    
    // Further derived class
    class Puppy : public Dog {
    public:
        Puppy() {
            cout << "Puppy Constructor called" << endl;
        }
        ~Puppy() {
            cout << "Puppy Destructor called" << endl;
        }
    };
    
    int main() {
        cout << "Creating Puppy object:" << endl;
        Puppy p;
        cout << "\nObject going out of scope:" << endl;
        return 0;
    }
    

    Output

    Creating Puppy object:
    Animal Constructor called
    Dog Constructor called
    Puppy Constructor called
    
    Object going out of scope:
    Puppy Destructor called
    Dog Destructor called
    Animal Constructor called
    

    Summary Table

    EventOrder
    Object CreationBase --> Intermediate --> Derived
    Object DestructionDerived --> Intermediate --> Base

    Key Rule: Constructors run from top (most base) to bottom (most derived). Destructors run from bottom (most derived) to top (most base).


    (b) Class Point with Overloaded + Operator

    Concept of Operator Overloading

    Operator overloading allows C++ operators (like +, -, *) to work with user-defined objects. The keyword operator is used to define the overloaded function.

    Program

    #include <iostream>
    using namespace std;
    
    class Point {
    private:
        int x;
        int y;
    
    public:
        // Constructor
        Point(int x = 0, int y = 0) {
            this->x = x;
            this->y = y;
        }
    
        // Overload + operator
        Point operator+(Point p) {
            Point temp;
            temp.x = this->x + p.x;
            temp.y = this->y + p.y;
            return temp;
        }
    
        // Display function
        void display() {
            cout << "Point(" << x << ", " << y << ")" << endl;
        }
    };
    
    int main() {
        Point p1(3, 4);
        Point p2(5, 6);
        Point p3;
    
        cout << "Point 1: ";
        p1.display();
    
        cout << "Point 2: ";
        p2.display();
    
        // Using overloaded + operator
        p3 = p1 + p2;
    
        cout << "Sum of Points: ";
        p3.display();
    
        return 0;
    }
    

    Output

    Point 1: Point(3, 4)
    Point 2: Point(5, 6)
    Sum of Points: Point(8, 10)
    

    Explanation

    StepDescription
    Point p1(3, 4)Creates object p1 with x=3, y=4
    Point p2(5, 6)Creates object p2 with x=5, y=6
    p1 + p2Calls p1.operator+(p2)
    Inside operator+temp.x = 3+5 = 8, temp.y = 4+6 = 10
    Returns tempResult stored in p3

    Note: The operator+ function takes one argument (the right-hand operand) because the left-hand operand is the calling object (this). The function returns a new Point object containing the sum.

  4. 45 marksStructures versus classesAnswer

    Can we use object oriented concept using structure instead of classes? Justify your opinion. [5]

    Yes, , to a significant extent, we can use object-oriented concepts using structures (struct) instead of classes. However, there are important limitations. Below is a justified explanation. --- In C++, a struct and a class are almost ide...

  5. 55 marksDefault argumentsAnswer

    In which case default argument is used? Describe with an example. [5]

    A default argument is used when a function is called without providing a value for one or more parameters. If the caller omits an argument, the compiler automatically substitutes the default value specified in the function declaration/de...

  6. 65 marksStatic data members and static functionsAnswer

    What is the task of static data members and function? Illustrate with an example. [5]

    Static Data Members and Static Member Functions in C++

    Static Data Members

    A static data member is a class variable that is shared among all objects of that class. Unlike regular data members (which have separate copies for each object), only one copy of a static data member exists, regardless of how many objects are created.

    Key characteristics:

    • Declared with the keyword static inside the class
    • Defined and initialized outside the class (in global scope)
    • Shared by all objects of the class
    • Exists even if no object is created

    Static Member Functions

    A static member function can be called without creating any object of the class. It can only access static data members and other static member functions.

    Key characteristics:

    • Declared with the keyword static
    • Called using the class name and scope resolution operator (::)
    • Does not have access to this pointer
    • Cannot access non-static members directly

    Example

    #include <iostream>
    using namespace std;
    
    class Student {
    private:
        int rollNo;
        static int count;       // static data member: shared by all objects
    
    public:
        Student(int r) {
            rollNo = r;
            count++;            // incremented each time an object is created
        }
    
        // static member function
        static void showCount() {
            cout << "Total Students: " << count << endl;
        }
    
        void showRoll() {
            cout << "Roll No: " << rollNo << endl;
        }
    };
    
    // Definition and initialization of static data member (outside class)
    int Student::count = 0;
    
    int main() {
        Student::showCount();   // called without any object -> Total Students: 0
    
        Student s1(101);
        Student s2(102);
        Student s3(103);
    
        s1.showRoll();          // Roll No: 101
        s2.showRoll();          // Roll No: 102
    
        Student::showCount();   // called using class name -> Total Students: 3
    
        return 0;
    }
    

    Output:

    Total Students: 0
    Roll No: 101
    Roll No: 102
    Total Students: 3
    

    Summary Table

    FeatureStatic Data MemberStatic Member Function
    Shared among objectsYesN/A
    Accessed via class nameYesYes
    Needs object to callNoNo
    Can access non-static membersN/ANo
    this pointer availableN/ANo

    Task in short: Static data members maintain common/shared state across all objects (e.g., counting objects), while static member functions provide utility operations that belong to the class as a whole rather than to any individual object.

  7. 75 marksAggregation versus inheritanceAnswer

    Differentiate between aggregation and inheritance. Why do we need virtual base class? [5]

    --- Feature Aggregation Inheritance --------- Relationship "Has-a" relationship "Is-a" relationship Nature One class contains an object of another class as a member One class derives properties and behaviors from another class Code Reuse...

  8. 85 marksText file reading and writingAnswer

    Define stream. How do you read the text from file? Describe. [5]

    A stream is an abstraction for a flow of characters between a program and some source or destination of data, such as a file, the keyboard, the screen, or a block of memory. The program deals only with the stream, and the library deals w...

  9. 95 marksFeatures of object-oriented programmingAnswer

    Explain the features of OOP. [5]

    Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects rather than functions and logic. The key features of OOP are: --- - The process of wrapping data (attributes) and methods (function...

  10. 105 marksNamespace definitionAnswer

    Define namespace. List any two manipulators with their uses. [5]

    A namespace is a declarative region that provides a scope to the identifiers (names of types, functions, variables, etc.) inside it. It is used to organize code into logical groups and to prevent name collisions that can occur especially...

  11. 115 marksFriend classesAnswer

    Why do we need friend class? How can we define member function outside the class? [5]

    In C++, data members declared as private or protected are not accessible from outside the class. This enforces data encapsulation. However, sometimes it is necessary for an external class or function to access the private and protected m...

  12. 125 marksPure virtual functionsAnswer

    Explain the use of pure virtual function with example. [5]

    A pure virtual function is a virtual function that has no implementation in the base class and is declared by assigning = 0 in its declaration. A class containing at least one pure virtual function is called an Abstract Class. --- Featur...