You are on page 1of 10

OBJECT ORIENTED PROGRAMMING CAT

DICT 2B

GROUP MEMBERS
1.ANNLITA

2.CYNTHIA

3.HELLEN

4.BRENDA WANJIRU N - 39314


A) Write a program in C++ to demonstrate how one class can
grant its friendship to another class to access the private
data of the initial class through the public member function
of the former class

B) Explain the difference between a constructor and a destructor citing the advantage of
Each
Constructor - Allocates memory to an object
Advantage:
Object initialization
Allows initialization of object data member when an instance of the class is created

Destructor - De - allocates memory created by constructor after it's deletion


Advantage:
Resources management
They free up memory allocated by the objects or release resources held by the object
C) Write an object-oriented program to find the greatest number among the four
variables a, b, c, and d whose values are entered at run time.

D) Write a program to display numbers from 10 to


1 using a
while loop construct.
E) Explain virtual functions and their characteristics
These are key features of object-oriented
programming that enable polymorphism which
allows a function to behave differently based on the
type of object that invokes it.
Characteristics
Virtual functions enable dynamic polymorphism – the appropriate function is determined
at runtime based on the type of the object being referenced or pointed to.
Function overriding – virtual functions allows derived classes to provide their own
implementation of a function
Virtual functions are declared in the base class using the ‘virtual’ keyword.

F) With the aid of a c++ segment, explain the role of a friend function in OOP
include <iostream>
using namespace std;
class Box
{
private:
int length;
public:
box(): length ( 0 ) { }
friend int printLength(Box); // friend function
};
int printLength (Box b)
{
b.length += 10;
return b.length;
}
int main()
{
Box b;
Cout << “Length of box:” <<printLength(b)<<endl;
Return 0;
}

A friendship function allows a function or another class to access the private and
protected members of a class.

G) Evaluate each of the following C++ statements (show your working)


i) b=12+4-7*9%6+12/3
1.Multiplication and division
7 * 9 = 63
12 / 3 = 4
2.Modulo
63 % 6 = 3
3.Addition and subtraction
12 + 4 = 16
16 – 3 = 13
Value of b is 13.

ii). 13-(8%5)>=9%7-3+6*2
1.Modulo
8%5=3
9 % 7 =2
2.Multiplication
6 * 2 = 12
3.Subtraction and addition
13 – 3=10
2 – 3 = -1
-1 + 12 = 11
4.Comparison
10 >= 11
The statement is false because 10 is not greater than or equal to 11

H) Write a C++ program that accepts the radius of a cylinder and


its height and determines the surface area of the cylinder. The
output of the program should output the surface area of the
cylinder.

I) Write a program to calculate the salary of a medical


representative based on the sales.
J) Write an object-oriented program in C++
that would accept
the dimensions of a right-angled triangle, and
determine and
output its perimeter. Use the appropriate data
members
and one function member.
K) Write a C++ program that calculates the
area and the volume
of a cube using Objects and classes.
(8mks)

You might also like