BIT 123 Object Oriented Programming MS
BIT 123 Object Oriented Programming MS
PAUL’S UNIVERSITY
DATE: TIME:
INSTRUCTIONS:
QUESTION 1
[2 Marks]
a program can consist of two functions where one can perform integer
addition and other can perform addition of floating point numbers but
the name of the functions can be same such as add.
The function add() is said to be overloaded.
Operator overloading:the ability to use one function more than one
time
Page 2 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
By overloading functions.
Inheritance
Programming.
i. Constructor [2 Marks]
This is a special function in a class which has the same name as its
class, has no return type and returns no value
Class A
A::A(){ }
Page 3 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
A word with special meaning when used in a program and can not
be used to declare variables by programerseg integer,class,
QUESTION 2:
i. Destructor [2 Marks]
Used to destroy the objects that have been created by a constructor
ii. Reference variable [2 Marks]
Provides an alias (alternative name) for previously defined varriable
Page 4 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
d) Develop main function such that the sides and area of your square are shown
when the program is executed. [8 Marks]
Int main()
Square sqrt(length);
Cout<<”width is “<<rect.Getwidth<<endl;
Cout<<”Area is “<<rect.Area<<endl;
Return 0;
Page 5 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
QUESTION 3:
a) Implement a class named rect from the base class Sqr given below.
[7 Marks]
#include<iostream.h>
int lenght;
public:
sqr(int x) {
lenght=x;
int get_lenght() {
Page 6 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
return lenght;
double Area() {
return lenght*lenght;
~sqr() {
cout<<"\nDestructor1 called";
};
Private:
Int width,
Public:
Page 7 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
};
b) Write a program which calculates the average of the elements of the row of
the two dimensional array. Say row 1 elements{2,3,4,5,5}, row 2 elements
{ 2,6,7,3,2} [10 Marks]
#include<iostream>
using namespace std;
int main ()
{
int age[2][5]= { {2,3,4,5,5}, { 2,6,7,3,2}};
int i,j;
int sum=0,avg=0;
for(i=0;i<2;i++)
{
for(j=0;j<5;j++)
{
sum=sum+age[i][j];
}
avg=sum/5;
cout << "Average of the elements of the row " << i+1 << " is
" <<avg << endl;
sum=0;
}
return(0);
}
Page 8 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
Set of code used to perform some specified task with return types of
void,int orfloat to the calling program
QUESTION 4:
a) Using classes, write a program that captures student data. The program
should prompt a user to enter a student Name, Age, course and
telephone number. The program should then display the details.
[15 Marks]
#include<iostream>
using namespace std;
class student
{
Public:
char name[50];
int age;
char course70];
int teleno;
Page 9 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
}stud1;
int main ()
{
student stud2;
cout << "Enter the name of the student" << endl;
cin >> stud1.name;
cout << endl << "Enter the age of the student" << endl;
cin >> stud1.age;
cout << endl << "Enter the course he undertakes " << endl;
cin >> stud1.course;
cout << endl << "Enter the telephone no of the employee" << endl;
cin >> stud1.teleno;
stud2=stud1;
cout << endl << "The name of the student : " <<stud2.name << endl;
cout << "The age of the student : " << stud2.age << endl;
cout << "The course he/she studies: " << stud2.course << endl;
cout << "His phone number is: " << stud2. teleno << endl;
return(0);
}
Page 10 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
static: a variable that is known only in the function that contains its definition
but is never destroyed and retains its value between calls to that function. It
exists from the time the program begins execution
QUESTION 5
Page 11 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
class M
{
public:
void display(void)
{
cout<<"class M\n";
}
};
class N
{
public:
void display(void)
{
cout<<"class N\n";
}
}; // what display ()function will be used?
This can be resolved by overriding.
i.e
class P: public M; public N;
{
public:
void display(void) //overrides display() of M and N
{M:: display();
}
}
};
Now we use the derived class as follows;
int main()
{
Pp
p.display();
return 0;
}
[6 Marks]
Page 12 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
Switch()
case 1:
cout<<””:
case 2:
cout<<””:
case 3:
cout<<””:
default:
cout<<””:
QUESTION 6
Any four
Page 13 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
b) Write a full C++ program that keeps printing (i.e. non-stop) the multiples of
the integer 2 (i.e. 2, 4, 8, 16, 32, 64…). [8 marks]
# include <iostream.h>
void main (void)
{
cout << “\n Printing multiples of 2…;
for (int i = 2; i > 0 ; i *= 2)
cout << i << “ “;
c)
d) Below is program that uses do-while loop.
#include<iostream>
Using namespace std;
int main()
{
int digit=0;
Do {
cout<<digit<<endl;
digit++;
while(digit<=9)
}}
1. Write the expected output after running the program.
(2marks)
0
1
2
3
4
5
6
Page 14 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
7
8
9
e) Identify SIX errors in the following code. [6 marks]
#include <iostream>
int main( );
Int, n;
cin>>n
total = n * rate;
cout<<total<<endl;
return 0;
Page 15 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
QUESTION 7.
b). Based on the following table, identify the applicable data type for
variables Staff ID, Name, Salary and Taxable respectively.
[4 marks]
StaffID – int
Name
Salary – float
Page 16 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
Int salary;
Return(salary); }
Int salary;
Salary=hours*rate;
Return(salary); }
d). Using a syntax code and simple diagram show an example of an inheritance hierarchy
for different types of bank account s. [6 mark]
ACCOUNT
Page 17 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.
CLASS ACCOUNT
};
};
};
};
End of Exam
End of Exam
Page 18 of 18