UNIVERSITY EXAMINATIONS: 2012/2013
EXAMINATION FOR THE BACHELOR OF SCIENCE IN
INFORMATION TECHNOLOGY
BIT 2108 OBJECT ORIENTED PROGRAMMING
DATE: AUGUST, 2013 TIME: 2 HOURS
INSTRUCTIONS: Answer Question ONE and Any Other TWO Questions
QUESTION ONE
a) With an example demonstrate how the new and delete keywords are used with
pointers in dynamic memory allocation [4 marks]
b) Differentiate between the following
i). Object oriented and object based programming languages
ii). Class and Object
iii). Overriding and overloading [6 marks]
c) Give FOUR characteristics of object oriented programming languages [4 marks]
d) What is the difference between Procedural and OOPs? [3 marks]
e) What is the difference between constructor and method? [3 marks]
a) Predict the output of the following program
#include <iostream>
using namespace std;
class vector
{
public:
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << “The surface of a: ” << a.surface() << endl;
return 0;
}
[2 marks]
b) Describe the following terms as used in OOP
i). Static memory allocation
ii). Dynamic memory allocation
iii). Constructors
iv). Destructors [8 marks]
QUESTION TWO
a) Define what a friend function is and predict the output of the following program
#include <iostream>
using namespace std;
class MyRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend MyRectangle duplicate (CRectangle);
};
void MyRectangle::set_values (int a, int b) {
width = a;
height = b;
}
MyRectangle duplicate (CRectangle rectparam)
{
MyRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
MyRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
[6 marks]
b) Give the following class Polygon, give a definition of the class MyRectangle that
inherits Polygon with a method that compute the area and perimeter of a
Rectangle
Design also the class Triangle that inherits from Polygon with additional method
that compute the area of a triangle.
Create the main program that demonstrate how the two derived classes are
invoked
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
[9 marks]
c) When implementing inheritance, some elements from the base class are not
passed to the derived class. List those methods that are not inherited. [3 marks]
d) Define1 what you understand by the following
i). Virtual function
ii). Multiple inheritance [2 marks]
QUESTION THREE
a) Debug the following program.
#include<ionstream.r>
main ( )
{
cout<<”enter two nubmers:”;//prompt
cin>> number1; //Read number
float number 1, number 2,sum, average;
cin>>number 2; //from keyboard
average=sum/2;
sum=number 1+number 2;
void;
cout<<”sum=”<<sum<<”\n”;
cout<<”average=”<<average<<”\n”;
}
}
[5 marks]
b) Find the errors in the following function prototype.
i). Float average(a,b);
ii). Int mul(int a,b);
iii). Int display();
iv). Void vect(int&v,int&size);
v). Void print(float data[],size=20);
5 marks]
c) State with a reason whether the following statements are true or false.
(i) Function argument is a value returned by the function to the calling
program.
(ii) When a function returns a value, the entire function can be assigned to
variable.
(iii) When argument is passed by value the function works with the original
arguments in the calling program.
(iv) A function can return a value by reference.
(v) When argument is passed by reference, a temporary variable is created
in the calling program to hold the argument value. [5 marks]
d) Briefly discuss how each of the following visibility modifiers affects inheritance
of member elements in derived classes.
(i) Private [2 marks]
(ii) Public [1 marks]
(iii) Protected [2 marks]
QUESTION FOUR
a) Write methods for bank operations such as recording customer’s details, recording
deposits and withdrawals. (Methods are declared within a class, main() is outside the
class)
Program should have the following:
Methods
Get details
Make deposit
Make withdrawals
Enquiry(optional)
Main() [10 marks]
b) Explain how you could modify the above program to achieve data hiding.
[5 marks]
c) Differentiate between intrinsic and user defined data type as used in C++. Give two
examples in each case. [5 marks]
QUESTION FIVE
KCA University is pleased with your outstanding performance in information technology
courses especially in programming. As such they would like to hire your services and
attach you to library.
(a) Write a program to capture details of available and new books. [10 marks]
(b) Rewrite the program in 5(a) and include the following
(i) Book borrowing
(ii) Book return
(iii) Display a list of available books [10 marks]
NOTE Marks will be awarded for use of class(es) and object in part 5(b)