You are on page 1of 4

Problem Set: OOP

1. Suppose, you are working on a software project that involves implementing a


geometry library in C++. The library should have a “Shape” class hierarchy to
represent different types of shapes, such as “Rectangle” and “Circle”. The “Shape”
class has the following specifications:
Member variables named “name” and “color”, Member functions named
setname(),setcolor(), displayinfo().
The derived class “Rectangle” inherited from “Shape” class has member variables
named “length” and “width” and member functions named “setDimensions()”,
“calculateArea()” (an override member function) Formula: length * width;
“displayInfo()”(an override member function). Similarly the derived class “Circle”
that has member variable “radius” and member functions
“setRadius()”,“calculateArea()” Formula: 3.14 * radius * radius; (an override member
function) “displayInfo()”(an override member function).
You need to demonstrate method overloading by implementing multiple versions of
the “calculateArea()” function in the “Shape” class based on the number and types of
parameters. Also, you need to demonstrate method overriding by overriding the
“calculateArea()” function in the “Rectangle” and “Circle” classes to provide their
specific implementation of calculating the area.
Once you have implemented the classes, you should create objects of each class, set
their attributes using the member functions, and call the “displayInfo()” function on
them to display the information of the shapes, including their calculated areas, on the
screen. Print appropriate messages to indicate which class's member function is being
called during the execution of the program. Apply polymorphism to the given criteria
by writing cpp code.

2. Problem Statement: Managing Bank Accounts

Develop a C++ program to manage bank accounts, utilizing friend functions and
constructors. The program should allow users to create and manipulate bank accounts,
including functionalities such as depositing and withdrawing funds, checking account
balances, and transferring funds between accounts. Implement a BankAccount class
with private attributes such as account number, account holder name, and balance.
Utilize a friend function to facilitate fund transfers between accounts while ensuring
appropriate access to private member variables. Additionally, design constructors to
initialize bank accounts with default or user-specified values. Develop a user-friendly
interface in the main program to interact with the bank account functionalities,
enabling users to perform transactions seamlessly. Ensure robust error handling to
manage invalid inputs and account operations effectively.
3. A class called Employee with private attributes for salary (double) and number of
hours worked per day (int). Define a constructor that initializes these attributes with
values passed as arguments.

i) Define a public method called 'getInfo()' that takes the salary and number of hours
of work per day of an employee as parameters and sets the corresponding attributes of
the Employee object.

ii) Define another public method called 'AddSal()' that adds $10 to the salary of the
employee if it is less than $500. This method should be called automatically as soon
as the Employee object is created.

iii) Define a third public method called 'AddWork()' that adds $5 to the salary of the
employee if the number of hours of work per day is more than 6.

In the main method, create an instance of the Employee class by passing the initial
salary and number of hours worked per day as arguments to the constructor. Then,
call the 'AddSal()' and 'AddWork()' methods on the Employee object to adjust the
salary based on the criteria specified. Finally, print out the final salary of the
employee to the console. Add error handling to the program by validating user input
for the salary and number of hours worked per day. Ensure that the program does not
crash or behave unexpectedly in case of invalid input. Demonstrate a C++ program
for the above scenario.

4. Problem Set: Managing Student Records

You are tasked with creating a program to manage student records using C++. Your
program should utilize an array of objects, constructors, and a friend function. Design a
solution that meets the following requirements:

Define a Student class to represent individual student records. Each student should have
the following attributes:
a. Student ID
b. Name
c. Grade
Implement a constructor for the Student class to initialize these attributes.

Create a friend function named displayStudentInfo that displays the information of a


student object.

Develop a main program that:


d. Creates an array of Student objects to store student records.
e. Initializes the array with data provided by the user or hardcoded values.
f. Calls the displayStudentInfo function to display the information of each
student.
Ensure proper validation of user inputs and error handling in the program.

5. Problem Statement: Area Calculation with Single Inheritance


You are required to develop a C++ program to calculate the area of different shapes using a
base class and single inheritance. Design a solution with the following specifications:
 Define a base class named Shape (Circle) with a function calculateArea()- for
circle. This class should have public member variables such as length, width, and
radius.
 Implement a derived class named Rectangle, inheriting from the Shape class. The
Rectangle class should have methods to set its dimensions and override the
calculateArea() function to calculate the area of a rectangle using the formula: length
* width.
 Develop a main program that:
 Creates an object of the Rectangle class.
 Sets its dimensions using user input or hardcoded values.
 Calls the calculateArea() function of the Rectangle class to calculate its area.
 Displays the calculated area on the screen.
 Test the program with various scenarios, including calculating the area of rectangles
with different dimensions.

6. Problem Statement: Generic Calculator


You are tasked with developing a generic calculator program using templates in C++.
Design a solution with the following specifications:
Define a template class named Calculator that supports arithmetic operations on
generic data types (integers, floating-point numbers, etc.). The Calculator class
should have methods to perform addition, subtraction, multiplication, and division
operations.
 Implement methods for each arithmetic operation within the Calculator class.
 Develop a main program that:
 Creates objects of the Calculator class for different data types (integers,
floating-point numbers, etc.).
 Calls the arithmetic operation methods on these objects to perform
calculations.
 Displays the results of each operation.
 Ensure proper validation of user inputs and error handling in the program.
 Test the program with various data types and arithmetic operations.

7. Design a C++ program that handles invalid input using try-catch blocks. Implement a
solution with the following specifications:

Define a function named calculateAverage() that calculates the average of two numbers. If
the user inputs a non-numeric value, the function should throw an exception.

Use a try-catch block within the calculateAverage() function to handle the following
exception:

If the user inputs a non-numeric value, throw an exception of type std::invalid_argument with
the message "Invalid input. Please enter numeric values".
Develop a main program that:

Prompts the user to input two numbers.


Calls the calculateAverage() function to calculate the average of the two numbers.
Handles any exceptions thrown by the calculateAverage() function and displays appropriate
error messages.
Displays the calculated average if no exceptions occur.

make a simple solution using cpp, try catch

You might also like