UNIVERSITY EXAMINATIONS: 2016/2017
ORDINARY EXAMINATION FOR THE DEGREES OF BACHELOR OF
SCIENCE IN INFORMATION TECHNOLOGY /BACHELOR OF
BUSINESS IN INFORMATION TECHNOLOGY
BIT2108 BBIT203 OBJECT ORIENTED PROGRAMMING
FULLTIME/PART TIME/DISTANCE LEARNING
DATE: AUGUST, 2017 TIME: 2 HOURS
INSTRUCTIONS: Answer Question One & ANY OTHER TWO questions.
QUESTION ONE: 30 MARKS (COMPULSORY)
a) TRUE or FALSE: Namespaces allow you to have multiple memory locations with the same
variable name. (2 Marks)
b) Explain the error in the following groups of function declarations or write “No Error”
(6 Marks)
i. void f(int x, string y);
int f(int x, string y);
ii. float g(int x, float y = 2.31, string z);
iii. void h(int x);
void h();
c) Explain the difference between the keywords struct and class. (2 Marks)
d) Consider the following class definition: (6 Marks)
class TwoPoint{
private:
int x, y;
public:
void set(int x, int y);
};
Provide the definition of the method set. Write the method outside the class and do not change
the prototype of the method.
e) Give two reasons to pass an object by reference. (4 Marks)
f) Refer back to the TwoPoint class in (d) above. Write a default constructor for the class that does
not have an empty parameter list. (3 Marks)
g) Give one reason why a class would need a destructor. (2 Marks)
h) Why can’t the constant pointer this be used inside a static method? (1 Marks)
i) Assume the necessary operators are overloaded: (4 Marks)
i. Write the following in operator form: b.operator=(a.operator+(c));
ii. Write the following in functional form: z = x + w*y;
QUESTION TWO (20 MARKS)
a) Consider the following inheritance hierarchy: (6 Marks)
class A{
protected:
int x, y;
public:
int z;
}
class B: public A{
private:
int a, b, c;
}
i. How many data members does B have?
ii. How many of B’s data members are visible in B?
b) Why is protected accessibility needed? (4 Marks)
c) Give an example of a case where virtual inheritance is necessary (ie, a case where you
need virtual base classes in a multiple inheritance hierarchy.)
(4 Marks)
d) Consider the following inheritance hierarchy: (6 Marks)
class A{
public:
int x;
}
class B: public A{
private:
int x;
public:
void set(int a, int b);
}
Write the definition for the method set that assigns the inputs a and b to B’s data members.
QUESTION THREE (20 MARKS)
a) TRUE or FALSE: If a base class has a default constructor, then none of the constructors in the
derived class need to explicitly call a base class constructor. (2 Marks)
b) Differentiate struct, class and union data types as use in Programming. (6 Marks)
c) TRUE or FALSE: Virtual inheritance is necessary for polymorphism. (2 Marks)
d) What must you do if you want a class derived from an abstract base class to be nonabstract?
(2 Marks)
e) Define what is a pointer, Dynamic Memory Allocation and use a pointer example to show how
this is done in Object Oriented Programming. (8 Marks)
QUESTION FOUR (20 MARKS)
Write a program that reads in test scores and applies two different curves to them. The program should
Contain a base class ScoreBank with two private data members: an integer pointer for the scores and a
float for the average. The class should contain a method EnterScores which asks the user how many test
scores are needed, allocates enough memory, and reads in the scores. The class should also contain
a method CalcAverage which stores the average of the entered scores in the private float data member.
Also have a Output function that prints a sorted list of test scores to the screen as well as the average. Be
sure the class has an appropriate destructor.
Derive from ScoreBank a class Curve1 which contains a method Curve. This curve sets the average score
to 75. Find out how far away from 75 the actual average is and then add this value to each test score.
Overload the Output method to print, sorted, the original scores and the curved scores as well as the
original and new average.
Derive from ScoreBank a class Curve2 which contains a method Curve. This curve sets the high score to
100 and scales the rest of the scores accordingly. Find the high score (you can use sort to do this) and
divide all the scores by this value. Then overload the Output function to print the original scores, the new
scores, and the averages for both sets.
Feel free to add any other methods or data members to the classes other than the ones I’ve mentioned
if it will help you implement any of the required features.
Write a main function driver for these classes that creates objects of type Curve1 and Curve2 and asks the
user to input a list of test scores into each object and then runs the Output functions from each object.
(20 Marks)
QUESTION FIVE (20 MARKS)
a) Show the output of the following program: (5 Marks)
#include<iostream>
class Base {
public:
Base(){cout<<”Base”<<endl;}
Base(int i){cout<<”Base”<<i<<endl;}
~Base(){cout<<”Destruct Base”<<endl;}
};
class Der: public Base{
public:
Der(){cout<<”Der”<<endl;}
Der(int i): Base(i) {cout<<”Der”<<i<<endl;}
~Der(){cout<<”Destruct Der”<<endl;}
};
int main(){
Base a;
Der d(2);
return 0;
}
b) Write a program that has an abstract base class named Quad. This class should have four member
data variables (floats) representing side lengths and a pure virtual function Area. It should also
have a method for setting the data variables. Derive a class Rectangle from Quad and override the
Area method so that it returns the area of the Rectangle. Write a main function that creates a
Rectangle and sets the side lengths. Also write a top-level function that will take a parameter of
type Quad and return the value of the appropriate Area function.
(15 Marks)