Blog
Object Oriented Programming BS IT 2nd Term 2017 Past Paper UOS
University of Sargodha
BS 2nd Term Examination 2017
Subject: I.T
Paper: Object Oriented Programming (CMP: 2123)
Time Allowed: 2:30 Hours
Maximum Marks: 60
Objective Part (Compulsory)
Q No 1: Write short answers of the following in 2-3 lines each on your answer sheet. (2*12)
- What are object oriented languages?
- What is difference between structured and object oriented programming?
- What is difference between source code and object code?
- Define the terms class and object?
- What is abstraction?
- What is Encapsulation?
- How many destructors can be defined in a class?
- When would a class destructor be useful?
- When are friend functions necessary?
- What is operator overloading?
- Define inheritance?
- Define polymorphism?
Subjective Part (3 x 16)
Note: Attempt any three out of five questions (All questions carry equal marks)
Q No 2:
Create a class rectangle with attributes length and width, each of which defaults to 1. Provide member function that calculate the area of the rectangle also provide set and get functions for the length and width attributes. The set function should verify that Length and width are each floating point numbers larger than 0.0 and less than 20.0.
Q No 3:
Write a class named operator that have only one data member count the class has the following member functions.
- A constructor to initialize the count.
- Show function to show the count
- Overload ++ operator to increase the count by 1
Q No 4:
Define a class for a bank account that includes the following data members:
- Name of the depositor
- Account number
- Type of Account
- Balance amount in the account
The class also contains the following member functions:
- A constructor to assign initial value
- Deposit function to deposit some amount. It should accept the amount as parameter.
- Withdraw function to withdraw an amount after checking the balance. It should accept the amount as parameter.
- Display function to display name and balance
Q No 5:
Explain constructor overloading with the help of one example.
Q No 6:
Write a class local phone that contains an attribute phone to store a local phone number. The class contains member functions to input and display phone number.
Write a child class NatPhone for national phone numbers that inherits localPhone class. It additionally contains an attribute to store city code. It also contains member function to input and show the city code.
Write another class IntPhone for international phone numbers that inherits NatPhone class. It additionally contains an attribute to store country code. It also contains member functions to input and show the country code.
Solved answers – OOP BS IT 2017
Short questions
- Object-oriented languages organize programs around objects that combine data and functions and support encapsulation, inheritance and polymorphism – e.g. C++, Java, C#, Python.
- Structured programming divides a program into functions that act on separate data (top-down). OOP groups data and the functions that use it into objects (bottom-up), giving better data security, reuse and maintenance.
- Source code is the human-readable program written by the programmer (.cpp); object code is the machine-language output of the compiler (.obj/.o), which the linker turns into an executable.
- A class is a user-defined blueprint describing data members and member functions; an object is an instance of a class that occupies memory, e.g.
Car c1; - Abstraction: showing only essential features and hiding implementation details, e.g. a car’s driver uses the steering and pedals without knowing how the engine works.
- Encapsulation: wrapping data and functions into a single unit (class) and restricting direct access to data with private members accessed through public functions (data hiding).
- Only one destructor per class – it takes no arguments and cannot be overloaded.
- A destructor is useful when an object holds resources that must be released when it is destroyed – freeing dynamically allocated memory (delete), closing files or network connections.
- Friend functions are needed when a non-member function must access private data of a class, e.g. overloading
<</>>for I/O, or a function that operates on private data of two different classes. - Operator overloading: giving an existing C++ operator a special meaning for user-defined types, e.g.
Complex operator+(const Complex&)so thatc3 = c1 + c2;works. - Inheritance: creating a new (derived) class from an existing (base) class so it reuses and extends the base’s members, e.g.
class Car : public Vehicle. - Polymorphism: “many forms” – the same interface behaves differently for different types: compile-time (function/operator overloading) and run-time (virtual functions overridden in derived classes).
Subjective questions (all programs compiled and tested with g++)
Q.2 Rectangle class
#include <iostream>
using namespace std;
class Rectangle {
private:
float length, width;
public:
Rectangle() : length(1.0f), width(1.0f) {} // both default to 1
void setLength(float l) {
if (l > 0.0f && l < 20.0f) length = l;
else cout << "Invalid length, keeping " << length << endl;
}
void setWidth(float w) {
if (w > 0.0f && w < 20.0f) width = w;
else cout << "Invalid width, keeping " << width << endl;
}
float getLength() const { return length; }
float getWidth() const { return width; }
float area() const { return length * width; }
};
int main() {
Rectangle r;
cout << "Default area: " << r.area() << endl; // 1
r.setLength(5.5f);
r.setWidth(4.0f);
cout << "Length " << r.getLength() << ", Width " << r.getWidth()
<< ", Area " << r.area() << endl; // 22
r.setWidth(25.0f); // rejected
return 0;
}Output: Default area 1; Length 5.5, Width 4, Area 22; “Invalid width, keeping 4”.
Q.3 Counter class with overloaded ++ – note: operator is a reserved keyword in C++, so a class cannot be named “operator”; it is named Counter here.
#include <iostream>
using namespace std;
// "operator" is a reserved word in C++, so the class is named Counter.
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {} // constructor initializes count
void show() const { cout << "Count = " << count << endl; }
void operator++() { ++count; } // prefix ++ increases count by 1
void operator++(int) { count++; } // postfix ++ as well
};
int main() {
Counter c(5);
c.show(); // 5
++c;
c.show(); // 6
c++;
c.show(); // 7
return 0;
}Output: Count = 5, Count = 6, Count = 7.
Q.4 Bank account class
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string name;
long accountNo;
string type;
double balance;
public:
BankAccount(string n, long no, string t, double b)
: name(n), accountNo(no), type(t), balance(b) {}
void deposit(double amount) {
if (amount > 0) balance += amount;
else cout << "Invalid amount" << endl;
}
void withdraw(double amount) {
if (amount <= 0) cout << "Invalid amount" << endl;
else if (amount > balance) cout << "Insufficient balance" << endl;
else balance -= amount;
}
void display() const {
cout << "Name: " << name << ", Balance: " << balance << endl;
}
};
int main() {
BankAccount acc("Ali Raza", 1001, "Saving", 5000);
acc.deposit(2000);
acc.withdraw(10000); // Insufficient balance
acc.withdraw(1500);
acc.display(); // Balance: 5500
return 0;
}Output: “Insufficient balance”, then Name: Ali Raza, Balance: 5500.
Q.5 Constructor overloading: a class can have several constructors with the same name but different parameter lists; the compiler chooses one according to the arguments given when the object is created. This lets objects be initialized in different ways.
#include <iostream>
using namespace std;
class Box {
private:
double length, width, height;
public:
Box() : length(1), width(1), height(1) {} // default
Box(double side) : length(side), width(side), height(side) {} // cube
Box(double l, double w, double h) : length(l), width(w), height(h) {}
double volume() const { return length * width * height; }
};
int main() {
Box b1; // calls Box()
Box b2(3); // calls Box(double)
Box b3(2, 3, 4); // calls Box(double, double, double)
cout << b1.volume() << " " << b2.volume() << " " << b3.volume() << endl; // 1 27 24
return 0;
}Output: 1 27 24 – each object called a different constructor.
Q.6 Multilevel inheritance: LocalPhone → NatPhone → IntPhone
#include <iostream>
#include <string>
using namespace std;
class LocalPhone {
protected:
string phone;
public:
void inputPhone() { cout << "Enter phone number: "; cin >> phone; }
void showPhone() const { cout << "Phone: " << phone << endl; }
};
class NatPhone : public LocalPhone {
protected:
string cityCode;
public:
void inputCity() { cout << "Enter city code: "; cin >> cityCode; }
void showCity() const { cout << "City code: " << cityCode << endl; }
};
class IntPhone : public NatPhone {
private:
string countryCode;
public:
void inputCountry() { cout << "Enter country code: "; cin >> countryCode; }
void showCountry() const { cout << "Country code: " << countryCode << endl; }
};
int main() {
IntPhone p;
p.inputCountry();
p.inputCity();
p.inputPhone();
p.showCountry();
p.showCity();
p.showPhone();
return 0;
}Sample run: country code +92, city code 048, phone 3721234. IntPhone inherits the phone and city-code members and functions from its parent classes (multilevel inheritance); protected lets derived classes access the inherited data.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
