Object Oriented Programming Cat 1 - Edited
Object Oriented Programming Cat 1 - Edited
• #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
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;
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;
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;
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