Questions For Practice - PSOOP - 2021
Questions For Practice - PSOOP - 2021
1. Create a class Cow, representing a cow in a dairy herd. The class should include a
name, an ID number, a date of birth, and a date of most recent calving. Also write a
method that adds the ability to keep a total of milk output, to enter a value for a
milking, and to compute average milk production. The method should also support
the ability to reset the total milk output value to zero. Create appropriate objects and
use setter methods to initialise the objects and getter methods to extract the values of
these objects.
2. Define a class to represent a bank account.Include the following members:
Data members
a. Name of the depositor
b. Account number
c. Type of account
d. Balance amount in the account
Member function
a. To assign initial value
b. To deposit an amount
c. To withdraw an amount after checking the balance
d. To display name and balance
Write a main program to test the program.
3. Create a program using class to process Shopping List for a departmental store.
The list should include details like Code No , Name and Price for each item.The
actions performed on the list should be able to display the name and price for entered
code number (Code No ) of the item. There should also be a provision to add new
item as initially the list is empty and then to delete an existing item. Also all the
items need to be shown with CodeNo, Name and Price and then total of all the
prices.
..................................................................................................................................................
1. Create a class 'Employee' with three data members which are name, age and Salary.
The constructor of the class assigns default values name as "unknown", age as '18'
and salary as 20000. Write the setter and getter methods for class Employee. Write
another method which prints the name, age and Salary of 10 employees sorted
according to their age.
2. Design the constructor for the class designed in Exp1.2.
3. Write a Program to generate Fibonacci Series by using Constructor to initialize the
Data Members.
..................................................................................................................................................
2.Write a C++ program to implement method overloading by implementing the Power class which
contains a function raisePower() to raise a number a b in the following different forms .
a. The function takes a DOUBLE value for a and INT value for b.
Use a default value of 2 for b to make the function to calculate squares when this argument
is omitted.
b. Implement another function that performs the same operations but takes INT for both a and
b.
3.Write a program that overloads absolute 3 functions namely absolute that return absolute
values of int, floating point number and double.
...........................................................................................................................................................
Create a class Posting and write 3 constructors to initialize the object and set the parameters
and display the employee post according to selection criteria.
Data members:
● int courseWork;
● int AptTest;
● int TechTest;
● int interview;
Methods:
● Posting (int courseWork, int AptTest, int TechTest,int interview)
● Posting (int TechTest,int interview)
● Posting (int interview)
Make use of ‘this’ keyword.
3.Write a program for finding area of different geometric shapes (circle, rectangle, cuboid). Use
constructor overloading with type, order, sequence of arguments to find the area of shapes.
Consider a class Product with data members barcode and name of the product. Create the
appropriate constructor and write getter methods for the individual data members. and write two
virtual methods, scanner() and printer().
Derive 2 classes from Product, 1st class is PrepackedFood and 2nd class is FreshFood. the
PrepackedFood class should contain the unit price and the FreshFood class should contain a weight
and a price per kilo as data members. Override the methods scanner and printer in the derived
classes. (These methods will simply output product data on screen or read the data of a product
from the keyboard depending upon whether it is Prepacked or FreshFood) In main, create a base
class pointer and point it to the appropriate derived class objects to demonstrate runtime
polymorphism.
Create class person with attributes phone_number, name and a method read()for getting and setting
the name and phone_number. Include a method show() to display the phone_number and name.
Derive class student from person with attributes roll_number ,course and method read to override
that in base class person. In this read() method give a call to the base class read() and ask for setting
roll_number and course. Here also include a method show() which initially calls the base class
method show() and then displays the roll_number and course.
3.Create a base class called Shape.Use this class to store two double type values that could be used
to compute the area of figures. Derive two specific classes called Triangle and Rectangle from the
base Shape. Add to the base class, a member function get_data() to initialize base class data
members and another member function display_area() both as virtual functions and redefine these
functions in the derived classes to suit the requirements.
Using these three classes, design a program that will accept dimensions of a triangle or a rectangle
interactively, and display the area.
Remember the two values are given as input will be treated as lengths of two sides in the case of
rectangles and as base and height in the case of triangles and used as follows:
Area of rectangle = x*y
Area of triangle = ½ *x*y
In main, create a base class pointer and point it to the appropriate derived class objects to
demonstrate runtime polymorphism.
6) Program on Inheritance: Implement a Program to demonstrate single, multilevel
Inheritance
1. Create a class called Employee with data members emp_id and name. Write a
parameterized constructor and getter methods for the data members of the Employee class.
Next, create two classes FullTimeEmployee and PartTimeEmployee derived from the Employee
class using public mode of inheritance. FullTime Employee receives salary monthly and has
data members basic salary, dearness allowance and HRA. Total monthly salary is equal to sum
of all the above data members. Write a calculateSalary() method for computing a full time
employee’s monthly salary. PartTime Employees are paid on an hourly basis with the rate of
payment per hour and the total number of hours worked stored as data members. Write a
calculateWages() method for computing the daily wages of a given Part Time Employee
assuming that the employee has worked for a period of 30 days. Write appropriate constructors
for the derived classes. Also write a menu-driven main() function which simulates the above
scenario.
2. Mr John is given the contract to colour the house . Design a c++ program that implements
multi- level inheritance in such a way that
○ There must be 3 classes i.e. Exterior ,Paint ,Bill.
○ The Class Bill inherits the Paint class which in turn inherits the Class Exterior
○ Class Exterior contains a function to find the area of the house
○ Class Paint contains a function to select the paint type and brand
○ Class Bill calculates the final cost of the painting by getting the house details and
paint details.
● Write a main() function to create appropriate objects and simulate the above scenario.
3. a. Design three classes: STUDENT, EXAM and RESULT. The STUDENT class has data
members such as rollno, name. EXAM is created by inheriting STUDENT. EXAM class
adds data members representing the marks scored in six subjects. Derive RESULT from
EXAM and has its own data members such as totalmarks. Write a program to model this
relationship.
b.Taking person as base class: Attributes: Name,gender,age Function : get_data()
Worker derived from person: Attributes: base_salary,shift(in hours) Function: Set_data()
Manger derived from worker: Attributes: dept,no of people(subordinates or people working
under him/her),bonus Function:display_payment()
Display the total payment. Total payment calculated as : TP=(base_salary*shift)+(no o
people* bonus)
Define a class called Home with two data members used to store the number of rooms and the size
in square meters. Supply default values for your constructor definition to create a default
constructor. In addition to accessor methods, also define the method display(), which outputs the
data members of an apartment.
Define another class called Car with data members used to store car number, seating capacity and
model name. Add appropriate constructors and member functions.
Define a class derived from the Car and Home classes called MotorHome, which is used to
represent motorhomes. Inheritance of public base classes is used.The MotorHome class contains a
new data member used to store one value of the CATEGORY type. CATEGORY represents the
categories “Luxury,” “First Class,” “Middle Class,” and “Economy”. In addition to defining a
constructor with default values, also define appropriate access methods and a display() method for
output.
Write a menu driven main function to test the above scenario.
3.Imagine a publishing company that markets both book and audiocassette versions of its works.
Create a class publication that stores the title (a string) and price (type float) of a publication. For
this class consider two base classes: book, which adds a page count (type int), and tape, which
adds a playing time in minutes (type float). Each of these three classes should have a getdata()
function to get its data from the user at the keyboard, and a putdata() function to display its data.
Write a main() program to test the book , tape and publication classes by creating instances of them,
asking the user to fill in data with getdata(), and then displaying the data with putdata().
The Coworker class represents human resources data for a company. The class is used as a base
class for various employees, blue-collar, white-collar, and freelancers. The Coworker class has only
a name, address of an employee, and the division where the employee works as data members.
Create a base class called CoWorker with the given data members. Write virtual functions for
computing income and displaying the worker’s details and appropriate constructor and
destructor(virtual). Create a derived class called Laborer which adds data members wages and
hours. Class Laborer must override the income and display methods and have constructor,
destructor and other appropriate methods. Create another derived class called Employee with data
member salary. Class Employee must override the income and display methods of CoWorker class
and have constructor, destructor and other appropriate methods. In main, create an array of pointers
of CoWorker type and initialize them to different objects of derived classes (Employee/Laborer)
stored on heap memory. Call the display and income methods for these objects using the base class
pointer. Delete the objects created to show the appropriate destructor calls.
3.Create a base class as a Vehicle. The Vehicle class has wheels and engine capacity as data
members.and two pure virtual functions spec() to set the values for data members and
display_stats() to display the values assigned. Create classes LMV(Light Motor
Vehicle),HMV(Heavy Motor Vehicle) and TW(Two Wheeler) publicly derived from the Vehicle
class. Include variables like speed,mileage and rpm in the derived classes and override the virtual
methods in these classes.Also have constructor initializing the values to 0 as default and a virtual
destructor for the classes.In main create an array of pointers of the base class and set them to the
objects of the derived classes.
Now make a call to the various methods for these objects using the base class pointer. Delete the
objects created to show the appropriate destructor calls.
Create a class Book with private data members title, author and year of publication. Write
appropriate constructors for the same. Write a display() method to display a book’s details and a
getter method for getting a book’s title. In main, create a vector to store book objects. Create a
menu with the options: Add a book, Display all books, Search a book by title, Exit.
Add a book should be able to add a given book to the vector you created. Display all books should
be able to traverse through the vector of books created(Use an iterator for vector for the same).
Search a book should be able to search for a given book through the iterator for the vector of books.
Sample dialog is shown:
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:1
Enter title:More than Human
Enter author:Sturgeon, Theodore
Enter year:1953
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:1
Enter title:At Home in the Universe
Enter author:Kauffman
Enter year:1996
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:1
Enter title:Object Oriented Programming with C++
Enter author:Balagurusamy
Enter year:2008
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:2
Title:More than Human
Author:Sturgeon, Theodore
Year:1953
Title:At Home in the Universe
Author:Kauffman
Year:1996
Title:Object Oriented Programming with C++
Author:Balagurusamy
Year:2008
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:3
Enter the book title:At Home in the Universe
Title:At Home in the Universe
Author:Kauffman
Year:1996
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:3
Enter the book title:Problem Solving with C++
Sorry, this book is not available!
1. Add a book
2. Display books
3. Search a book
4. Exit
Enter your choice:4
2. Write a program which mimics a voting system for awarding the best movie. Your program must
first read the total number of votes received. Next it must read the movie names one by one. A
movie name entered means a vote received for the movie. Calculate the total votes received for
each movie, and output it along with the movie name. Find and print the best movie. Use a map to
calculate the output. Your map should index from a string representing each movie’s name to
integers that store the sum of the votes for the movie.
Sample input:
Enter the no. of votes:7
Enter movie name to resemble each vote one to a line:
Harry Potter and the Order of the Phoenix
Harry Potter and the Order of the Phoenix
The Bourne Ultimatum
Harry Potter and the Order of the Phoenix
The Bourne Ultimatum
Wall-E
Glitter
3.1 Create a class student that includes a student’s first name and his roll_number.
Create five objects of this class and store them in a list thus creating a
phone_lit.Write a program using this list to display the student name if the
roll_number is given and vice-versa.
3.2Create an array with even numbers and a list with odd numbers.Merge two
sequences of numbers into a vector using merge() algorithm.Display vector.
1.Write a program that converts dates from numerical month/day format to alphabetic
month/day (for example 1/31 or 01/31 corresponds to January 31).
You will define two exception classes, one called MonthError and another called
DayError. If the user enters anything other than a legal month number (integers from
1 to 12), then your program will throw and catch a MonthError. Similarly, if the user
enters anything other than a valid day number (integers from 1 to either 29, 30, or 31,
depending on the month), then your program will throw and catch a DayError. To
keep things simple, always allow 29 days for February.( If the user enters an illegal
month or day other than the valid number, for ex, some gibberish like 8&*68, the
program must still print a MonthError/DayError as applicable)
Sample output:
Enter Date in month/day numeric notation:
1/30
That is the same as
January 30
Again?(y/n)
y
Enter Date in month/day numeric notation:
02/29
That is the same as
February 29
Again?(y/n)
y
Enter Date in month/day numeric notation:
02/30
Invalid day for the corresponding month
Try Again!
Enter Date in month/day numeric notation:
1@12/23
Invalid month
Try Again!
Again?(y/n)
y
Enter Date in month/day numeric notation:
1@12&23
Invalid Date
Try Again!
Again?(y/n)
n
End of program
Hint: Input each number as a string, and then scan through the
string to see if it contains all digits. If not, throw an exception. To convert
a string str to an integer, use the following code:
stoi(str); follow: https://www.softwaretestinghelp.com/cpp-string-to-integer/
2. Consider the expression in the form a+b where ‘a’ and ‘b’ are numeric values and
‘+’ is any operator. Operators can be +,-,*,/,log and ^.
Write a program to handle these operations and the exceptions generated.
Exceptions that needs to be considered are :
a.Check if ‘a’ and ‘b’ are numbers. If yes then ok for further execution else throw an
exception and handle it by asking the user to give correct inputs.
b.Check if ‘+’ is an operator as specified in the operators list.
c.Check for the order of the expression as ‘ a+b’ only and not +ab or ab+.
d.Check for divide by zero and log1 exceptions.
Write appropriate catch blocks for handling these exceptions.Make the program a
menu driven one to ask the user for the operations to be performed.