Blog
Object Oriented Programming BSCS 2nd Term 2017 Past Paper UOS
University of Sargodha
BS 2nd Term Examination 2017
Subject: Computer Science
Paper: Object Oriented Programming (CMP: 2124)
Time Allowed: 2:30 Hours
Maximum Marks: 60
Objective Part (Compulsory)
Q.1. Write short answers of the following in 2-3 lines each on your answer sheet. (2*12)
i. Why constructors are used? Give an example of parameterized constructor?
ii. Which access specifier is taken as default if no access specifier is explicitly mentioned? Give an example in favour of your answer.
iii. How a virtual function is created? When is it used?
iv. Write the statement (only header) in which class B inherits class A privately.
v. Why exception handling is used?
vi. How abstract class is created in C++?
vii. What do you mean by function overriding?
viii. What is operator overloading?
ix. Which operator is used in order to access a member function of a class when dynamic object is created? Also give an example to access a function by using dynamic object.
x. Write names of two operators that cannot be overloaded?
xi. How static member function is differentiated from non-static function in calling a function?
xii. What is default constructor? Can an object be created by using default constructor when only parameterized constructor is defined in a class?
Subjective Part (3*12)
Q.2. Create a Distance class having data members as feet (of type int) and inches (of type float). Write at least two constructors of the class and a suitable method to compare two objects of the class to determine whether two objects are equal or not? The result is displayed by main().
(Note: the number of inches in each distance is less than 12).
Q.3. Write a class named Array that contains an array of integers as data member. The class contains the following member functions:
- A constructor that initializes the array element to 0.
- Input function to input the values in the array.
- Display function to display the values of array.
- A member function to make the odd values of the array three times (multiply by 3).
Q.4. Write a program that declares a Data class. It consists of only one data member count (of type int). Write appropriate constructor and other member function in order to validate the following statement in main().
obj++;
++obj;
where obj is an object of Data class.
Q.5. Create a Person class having name (i.e. array of char) as a data member. Create another class Teacher that inherits from Person class and declares salary (of type double). Write suitable constructors (multiple) of Teacher class to initialize data members. Create two objects of Teacher class in main() with two different constructors and display them.
Q.6. Create a class Geometry without any data member. Another class Circle is inherited from Geometry class which declares radius (of type float) as its data member. Derive another class Square from Geometry that declares side as its data member (of type int). By using the concept of polymorphism, display the area and perimeter of Circle and Square in main().
Area of Circle: πr²
Perimeter of Circle: 2πr
Area of Square: r²
Perimeter of Square: 4r
Solved answers – OOP BSCS 2017
Short questions
- Constructors initialize an object automatically when it is created, so data members never start with garbage values.
class Point { int x, y; public: Point(int a, int b) : x(a), y(b) {} // parameterized constructor }; Point p(3, 4); - private is the default for members of a
class:class A { int x; // private by default }; A obj; // obj.x = 5; -> error: x is private - Prefix the base-class member function with
virtual:virtual void show();. It is used when a base-class pointer or reference should call the derived class’s overridden version at run time (run-time polymorphism). class B : private A { … };- Exception handling (try/throw/catch) separates error-handling code from normal code and lets a program detect and recover from run-time errors (division by zero, file not found, bad allocation) instead of crashing.
- By declaring at least one pure virtual function in the class:
virtual void draw() = 0; - Function overriding: redefining a base-class function in a derived class with the same name, parameters and return type, so the derived version replaces it (dynamically if the function is virtual).
- Operator overloading: giving an existing operator a special meaning for user-defined types by defining an
operatorfunction, e.g.c3 = c1 + c2for Complex objects. - The arrow operator (
->).Student* s = new Student(); s->display(); // arrow operator on a pointer to a dynamic object delete s; - Any two of:
::,.,.*,?:,sizeof. - A static member function is called with the class name and scope resolution (
Student::getCount()) without any object; a non-static function must be called through an object (s1.display()). - A default constructor takes no arguments. No – if a class defines only a parameterized constructor, the compiler does not generate a default one, so
Point p;gives a compile error unless a default constructor is also written.
Subjective questions (programs compiled and tested with g++)
Q.2 Distance class – check equality
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
float inches; // always less than 12
public:
Distance() : feet(0), inches(0.0f) {}
Distance(int f, float i) : feet(f), inches(i) {}
bool isEqual(const Distance& d) const {
return feet == d.feet && inches == d.inches;
}
void show() const { cout << feet << "' " << inches << "\""; }
};
int main() {
Distance d1(5, 7.5f), d2(5, 7.5f), d3;
d1.show(); cout << " and "; d2.show();
cout << (d1.isEqual(d2) ? " are equal" : " are not equal") << endl;
d1.show(); cout << " and "; d3.show();
cout << (d1.isEqual(d3) ? " are equal" : " are not equal") << endl;
return 0;
}Output: 5′ 7.5″ and 5′ 7.5″ are equal; 5′ 7.5″ and 0′ 0″ are not equal.
Q.3 Array class – triple the odd values
#include <iostream>
using namespace std;
class Array {
private:
static const int SIZE = 5;
int arr[SIZE];
public:
Array() { // all elements = 0
for (int i = 0; i < SIZE; i++) arr[i] = 0;
}
void input() {
cout << "Enter " << SIZE << " integers: ";
for (int i = 0; i < SIZE; i++) cin >> arr[i];
}
void display() const {
for (int i = 0; i < SIZE; i++) cout << arr[i] << " ";
cout << endl;
}
void tripleOdd() { // odd values x 3
for (int i = 0; i < SIZE; i++)
if (arr[i] % 2 != 0) arr[i] *= 3;
}
};
int main() {
Array a;
a.display(); // 0 0 0 0 0
a.input();
a.tripleOdd();
a.display();
return 0;
}Sample: input 1 2 3 4 5 → 3 2 9 4 15.
Q.4 Data class with obj++ and ++obj
#include <iostream>
using namespace std;
class Data {
private:
int count;
public:
Data(int c = 0) : count(c) {}
Data& operator++() { // prefix ++obj
++count;
return *this;
}
Data operator++(int) { // postfix obj++
Data old = *this;
count++;
return old;
}
void show() const { cout << "count = " << count << endl; }
};
int main() {
Data obj;
obj++;
++obj;
obj.show(); // count = 2
return 0;
}Output: count = 2.
Q.5 Person and Teacher classes
#include <iostream>
#include <cstring>
using namespace std;
class Person {
protected:
char name[40];
public:
Person() { strcpy(name, "Unknown"); }
Person(const char* n) { strncpy(name, n, 39); name[39] = '\0'; }
};
class Teacher : public Person {
private:
double salary;
public:
Teacher() : Person(), salary(0.0) {}
Teacher(const char* n, double s) : Person(n), salary(s) {}
void display() const {
cout << "Name: " << name << ", Salary: " << salary << endl;
}
};
int main() {
Teacher t1; // default constructor
Teacher t2("Dr. Ahmed", 150000); // parameterized constructor
t1.display();
t2.display();
return 0;
}Output: Name: Unknown, Salary: 0 / Name: Dr. Ahmed, Salary: 150000.
Q.6 Geometry, Circle and Square – polymorphism
#include <iostream>
using namespace std;
class Geometry { // no data members
public:
virtual double area() const = 0;
virtual ~Geometry() {}
};
class Circle : public Geometry {
float radius;
public:
Circle(float r) : radius(r) {}
double area() const { return 3.14159 * radius * radius; }
};
class Square : public Geometry {
int side;
public:
Square(int s) : side(s) {}
double area() const { return side * side; }
};
int main() {
Geometry* g[2] = { new Circle(3.5f), new Square(4) };
for (int i = 0; i < 2; i++) {
cout << "Area = " << g[i]->area() << endl; // run-time polymorphism
delete g[i];
}
return 0;
}Output: Circle (r = 3.5) area 38.48; Square (side 4) area 16.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
