0% found this document useful (0 votes)
68 views

Object Oriented Programming Cat 1 - Edited

The document discusses object oriented programming concepts like constructors, polymorphism, operator overloading, friend functions and inheritance. It also provides C++ code examples to calculate rectangle perimeter, triangle hypotenuse, grade of coffee and average of three numbers entered by user.

Uploaded by

Andrea Shiks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Object Oriented Programming Cat 1 - Edited

The document discusses object oriented programming concepts like constructors, polymorphism, operator overloading, friend functions and inheritance. It also provides C++ code examples to calculate rectangle perimeter, triangle hypotenuse, grade of coffee and average of three numbers entered by user.

Uploaded by

Andrea Shiks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MOUNT KENYA UNIVERSITY

REG NO: BSCIS/2022/50553


NAME :MBUYA DELIGHT FAVOUR
BIT 3106 : OBJECT ORIENTED PROGRAMMING
CAT 1.
1. Write a functional C++ program code that prompts a user for four sides of a
rectangle and prints out the perimeter in centimeters. You should include
comments to indicate the meaning of key statements in your program

• #include <<iostream>;
It includes the necessary stream header for handling the user input
and output
• Int main () ;
This is the entry point for the program
• Double side1, side2, side3, side4 ;
Declaring variables and these variables are used to store the four
sides of the rectangle
• Std:: cout <<”enter the length of side1 in centimeters”>> ;
Std:: cin >> side1;
These are user input prompts and the rest are used for the remaining
three sides
• Double perimeter=side1+side2=side3+side4 ;
The perimeter is calculated by adding the four sides of the rectangle
• Std::cout <<”the perimeter of the rectangle is: “<<perimeter<<
“centimeters”<<
The calculated perimeter is printed to the console
• Return 0 ;
Indicates successful program execution and termination
2. Differentiate between data abstraction and data encapsulation

Data abstraction is concerned with modeling classes based on essential


features, while data encapsulation is about bundling data and methods into
a single unit and controlling access to the internal data. Both concepts
contribute to creating more modular, maintainable, and secure software by
organizing and managing data effectively in an object-oriented paradigm.

3. Discuss the benefits of object oriented programming with reference to


software reusability, code sharing, rapid prototyping and information
hiding.

Software Reusability:
• Benefits: Reusing well-designed classes can significantly reduce
development time and effort. It also improves code consistency and
reliability by utilizing proven and tested components.
Code Sharing:
• Benefits: Teams can work on specific modules or classes
concurrently, fostering collaboration and parallel development. This
modular approach leads to cleaner code and allows for easier
maintenance and updates.
Rapid Prototyping:
• Benefits: Prototyping becomes more efficient as developers can
leverage existing class libraries to create prototypes rapidly. This
speeds up the development cycle and refinement of ideas.
Information Hiding:
• Benefits: Information hiding enhances security, reduces complexity,
and makes it easier to maintain and modify the code. It also allows
for changes to the internal implementation without affecting the
external code that uses the class.
4. Write a C++ program code to compute the hypotenuse of triangle when
user enters the height and the base

#include <<iostream>> ;
#include <cmath> : {
double height, base;
std::cout << "Enter the height of the triangle: ";
std::cin >> height;

std::cout << "Enter the base of the triangle: ";


std::cin >> base;
double hypotenuse = sqrt(pow(height, 2) + pow(base, 2));
std::cout << "The hypotenuse of the triangle is: " << hypotenuse <<
std::endl;}
return 0;

5. Using valid C++ examples, discuss the following terms


I. Constructors
II. Polymorphism
III. Operator overloading
IV. Friend functions
V. inheritance
Constructors:
Constructors are special member functions in a class that are automatically called
when an object of the class is created. They are used to initialize the object's data
members or perform any setup operations.
Polymorphism:
Polymorphism allows objects of different types to be treated as objects of a
common type. In C++, this is achieved through function overloading and virtual
functions.
Operator Overloading:
Operator overloading allows you to define how operators behave for user-defined
types.

Friend Functions:
Friend functions are functions that are not members of a class but have access to
its private members.
Inheritance:
Inheritance is a fundamental concept in OOP, allowing a class to inherit
properties and behaviors from another class .
6. Use the switch statement to write a program that accepts the grade of a
certain type of coffee and the outputs an appropriate message based on
the following table. Declare the appropriate variables .
Grade Message
A Export quality
B Local market
C Good for blending
Any other Rejected

#include <iostream>

int main() {
// Declare variable to store the grade of coffee
char coffee Grade;

// Prompt the user to enter the grade of coffee


std:: cout << "Enter the grade of coffee (A, B, C): ";
std: :cin >> coffee Grade;

// Switch statement to determine the message based on the coffee grade


switch (coffee Grade) {
case 'A':
std::cout << "Export quality" << std::endl;
break;
case 'B':
std::cout << "Local market" << std::endl;
break;
case 'C':
std::cout << "Good for blending" << std::endl;
break;
default:
std::cout << "Rejected" << std::endl;
break;
}

return 0;
}
7. Declare suitable variables for the following cases:
i. To hold employee id number
int employeeId;
ii. To hold an employee gross salary
double grossSalary;
iii. To hold the gender of an employee
char gender;
8. What are the limitations of operator overloading
i. Complexity and Maintenance:
Overloaded operators can increase the complexity of the code, and
understanding the behavior of an overloaded operator requires careful
reading of the class implementation. This can make maintenance and
debugging more challenging.
ii. Syntax Limitations:
The syntax for overloading operators is fixed, and it might
not be as expressive or flexible as desired in some cases.
iii. Efficiency Concerns:
Overloaded operators might introduce inefficiencies
compared to regular member functions. For example,
creating temporary objects during the operation can impact
performance.
iv. Efficiency Concerns:
Overloaded operators might introduce inefficiencies
compared to regular member functions. For example,
creating temporary objects during the operation can impact
performance.
9. Write a C++ program that calculate the average of three numbers entered
through the keyboard by the user
#include <iostream>

int main() {
// Declare variables to store three numbers
double num1, num2, num3;

// Prompt the user to enter three numbers


std::cout << "Enter the first number: ";
std::cin >> num1;

std::cout << "Enter the second number: ";


std::cin >> num2;

std::cout << "Enter the third number: ";


std::cin >> num3;

// Calculate the average


double average = (num1 + num2 + num3) / 3.0;

// Display the result


std::cout << "The average of the three numbers is: " << average <<
std::endl;

return 0;
}
10.Write down the input of the following statement;
Int x=1
While(x++<5)
This program will output:
x: 2
x: 3
x: 4
x: 5

You might also like