BIT153 · TU past paper
Object Oriented Programming 2080.1 question paper
The complete TU 2080.1 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.
- 110 marksBottom-up approach in OOPHideAnswer
Why object oriented programming is known as bottom up approach?Explain different features of object oriented programming.[3+7]
--- Object Oriented Programming (OOP) is known as a bottom-up approach because the program design starts from the lower level (individual objects/classes) and builds upward toward the complete system. The reasoning is as follows: - In OO...
- 210 marksMulti-level inheritanceHideAnswer
Differentiate between single level inheritance and multiple inheritance?Write a C++ program to calculate the percentage of a student using multi-level inheritance. Accept the marks of three subjects in base class "Capture". Derive class "Total" from "capture" which should include a function to find the total marks obtained and derive class "Final" from "Total" which should calculate and display the percentage of student.[3+7]
Feature Single Level Inheritance Multiple Inheritance --------- Definition A derived class inherits from only one base class A derived class inherits from two or more base classes Syntax class B : public A {} class C : public A, public B...
- 310 marksConversion between objects of different clHideAnswer
Why data conversion is needed?How Conversion between objects of different classes is carried out.Write a program containing classes Kilogram and Gram to convert Gram to Kilogram. (1 Kilogram = 1000 grams)[2+4+4]
Data conversion (type conversion) is needed in programming for the following reasons: - Compatibility between different types: When we perform operations between variables of different data types (e.g., int and float), conversion is nece...
- 45 marksConstructor overloadingHideAnswer
What is constructor? Write a program to demonstrate constructor overloading in C++. [1+4]
A constructor is a special member function of a class that is automatically called when an object of that class is created. It has the same name as the class and has no return type (not even void). Its main purpose is to initialize the d...
- 55 marksFunction overloadingHideAnswer
What is function overloading? Explain with an example. [5]
Function overloading is a feature in C++ (and other OOP languages) that allows multiple functions to have the same name but with different parameter lists (different number, type, or order of parameters). The compiler determines which fu...
- 65 marksPublic, private, and protected inheritanceHideAnswer
What is the role of protected access specifiers in inheritance? Explain with example. [5]
Role of Protected Access Specifier in Inheritance
Definition
The protected access specifier is an access control modifier that makes class members accessible:
- Within the same class
- Within derived (child) classes
- NOT accessible from outside the class hierarchy (unlike public)
It sits between
privateandpublicin terms of accessibility.Access Specifier Comparison Table
Access Specifier Same Class Derived Class Outside Class privateYes No No protectedYes Yes No publicYes Yes Yes Role in Inheritance
The key role of
protectedin inheritance is:- It allows base class members to be inherited and used by derived classes directly.
- If a member is
private, the derived class cannot access it directly, even though it inherits it. protectedsolves this by giving derived classes direct access while still hiding the member from external code.
Example in C++
#include <iostream> using namespace std; class Animal { private: int age; // NOT accessible in derived class protected: string name; // Accessible in derived class, NOT outside public: Animal(string n, int a) { name = n; age = a; } }; class Dog : public Animal { public: Dog(string n, int a) : Animal(n, a) {} void display() { // Accessing protected member 'name' from base class - ALLOWED cout << "Dog's name: " << name << endl; // Accessing private member 'age' - NOT ALLOWED (compile error) // cout << age << endl; // Error! } }; int main() { Dog d("Bruno", 3); d.display(); // Accessing protected member from outside - NOT ALLOWED // cout << d.name; // Error! return 0; }Output:
Dog's name: BrunoKey Points
protectedmembers act likeprivateto the outside world.protectedmembers act likepublicto the derived classes.- It enables data hiding while still supporting code reuse through inheritance.
- It is especially useful when a base class wants to share internal data with its subclasses without exposing it publicly.
- 75 marksVirtual functionsHideAnswer
Differentiate between virtual function and pure virtual function with an example. [5]
A virtual function is a member function declared in a base class using the virtual keyword that can be overridden in derived classes. It has a definition (body) in the base class, so objects of the base class can still be instantiated an...
- 85 marksFriend functionsHideAnswer
What is friend function? Write a program to add any two private numbers of two different classes using friend function. [1+4]
Friend Function
Definition
A friend function is a function that is not a member of a class but has access to the private and protected members of that class. It is declared inside the class using the keyword
friend. A friend function can be a friend of more than one class at the same time, which allows it to access private data of multiple classes.Key points:
- Declared with the
friendkeyword inside the class body - Defined outside the class without using the scope resolution operator (
::) - Not called using an object of the class
- Can access private and protected members of the class it is declared friend of
Program: Add Two Private Numbers from Two Different Classes
#include <iostream> using namespace std; // Forward declaration of class B class B; class A { private: int num1; public: A(int x) { num1 = x; } // Declaring friend function friend void addNumbers(A, B); }; class B { private: int num2; public: B(int y) { num2 = y; } // Declaring friend function friend void addNumbers(A, B); }; // Definition of friend function void addNumbers(A obj1, B obj2) { int sum; sum = obj1.num1 + obj2.num2; // Accessing private members of both classes cout << "Sum of two private numbers = " << sum << endl; } int main() { A a(15); B b(25); // Calling friend function directly (not using object) addNumbers(a, b); return 0; }
Output
Sum of two private numbers = 40
Explanation
Step Description class B;Forward declaration so class A can reference class B friend void addNumbers(A, B);Declared in both classes to grant access obj1.num1Accessing private member of class A inside friend function obj2.num2Accessing private member of class B inside friend function addNumbers(a, b)Called directly without any object, like a normal function Note: The friend function
addNumbers()is not a member of either class, yet it can freely access the private data membersnum1andnum2of both classes A and B respectively. - Declared with the
- 95 marksOperator overloading definition and purposHideAnswer
What is operator overloading? Explain with an example. [5]
Operator overloading is a compile-time polymorphism feature in C++ that allows existing operators (such as +, -, , ==, etc.) to be given additional meaning or redefined for user-defined data types (classes/structures), while keeping thei...
- 105 marksTry, catch, and throw statementsHideAnswer
Illustrate Exception handling in C++ using try, throw and catch statements. [5]
Exception handling is a mechanism in C++ that allows a program to detect and handle runtime errors (exceptions) gracefully, without crashing the program. C++ provides three keywords for exception handling: Keyword Purpose ---------------...
- 115 marksBinary file reading and writingHideAnswer
Write a program that writes objects to a binary file and reads objects from the same file. [5]
Binary file I/O in C++ allows storing objects (structured data) directly in binary format using fstream. The key functions used are: - write() - writes raw bytes of an object to a binary file - read() - reads raw bytes from a binary file...
- 125 marksFunction templatesHideAnswer
What is function template? Write a program to demonstrate the use of function template that returns the greatest among integers, and float passed to it. [1+4]
A function template is a blueprint or generic function that allows a single function definition to work with different data types. Instead of writing separate functions for each data type, a function template uses a type parameter (place...