You are on page 1of 6

1

SASTRA DEEMED UNIVERSITY


SCHOOL OF COMPUTING
2019 – 2020 EVEN SEMESTER

CSE201 – Object Oriented Programming in C++


Sample Problems for Lab

LIST OF LAB EXERCISES


1. Basic C++ Programs using Input / Output, branching and Loops
2. Programs using functions with default arguments, inline functions and function overloading
3. Programs using classes with constructors and destructors
4. Programs using array as data members of a class and array of objects
5. Programs using String class
6. Programs using operator overloading
7. Programs for data conversion using overloading
8. Programs using inheritance
9. Programs using virtual functions and friend functions
10. Programs using file Input / Output
NOTE: Questions given in BOLD & ITALIC under each exercise have to be recorded
in the observation note book and to be evaluated by the faculty.

1. Programs using Input / Output, branching and loops


A. Program to input the student details like Name, Regno, YearOfStudy, Branch, Marks in
5 subjects and print the details in a formatted manner.
B. Program to create a structure called Employee with members as EmpId, Name, Age,
BasicSalary, DA(in %), PF(in % of BasicSalary), GrossSalary and NetSalary.
GrossSalary = BasicSalary + BasicSalary*DA. NetSalary = GrossSalary – PF. Input the
details for 3 employees and print the details of each employee in a formatted manner.
C. In Q.No-1A, compute the total marks of the student and the percentage and print along
with the details of student.
D. In Q.No-1B, input the details for ‘n’ employees, compute the salary details and print the
details of all the employees in tabular form.

2. Programs using functions with default arguments, inline functions and


function overloading
A. Write a program to calculate cube of a given variable using inline function and return
the result by reference.
B. Write a program to find the largest of the three numbers using inline functions.
C. Write a program to find the absolute value of an integer, double and float value using
function overloading.
2

D. Raising a number ‘N’ to the power ‘P’ is the same as multiplying N by itself P times. Write
a function called power() that takes N (double) and P (int) as input, and returns the result
N*P as a double value. Use a default argument of 2 for P, so that if this argument is
omitted, the number N will be squared. Overload power() function, to work with int, long,
and float. Overload the power() function for char datatype also, which should print P times
the given character N. Write the main() program to exercise these overloaded functions
with all argument types.
E. Program to compute the volume of a cylinder using a function compute(), with default
values - 1 for radius of the base of the cylinder and 2 for height of the cylinder. Calculate
and return the volume of the cylinder, by calling the function from the main(), with and
without arguments.

3. Programs using classes with constructors and destructors


A. Create a class Date whose object can store a day, month and year. Include the necessary
constructors to initialize the objects and function to display the date in ‘dd/mm/yyyy’
format.
B. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay Rs. 50 as
toll fee. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps
track of the number of cars that have gone by, and of the total amount of money
collected. Model this tollbooth with a class called TollBooth. The two data items are a
type unsigned int to hold the total number of cars, and a type double to hold the total
amount of money collected. A constructor initializes both of these to 0. A member
function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nonPayingCar(), increments the car total but adds nothing to
the cash total. Finally, a member function called dispDetails() displays the two totals.
Make appropriate member functions const. Write a program to test this class. This
program should be menu-driven to allow the user to select paying car, or nonpaying car
or to print the total cars and total cash collected and finally exit the program.
C. Write a C++ program to create a class called Complex to implement the following
functions on Complex type objects. READ/INPUT, ADD, SUBTRACT, MULTIPLY and
PRINT with return the resultant object. Write the program with required constructors,
member functions with necessary arguments and return types.
D. Create a class called CarPark that has the members CarRegnno(int), ChargePerHour(int)
and ParkingDuration(float). Set the data and show the charges and parked hours of a car
based on CarRegnNo. Make two member functions for setting and showing the data.
Member function should be called from other functions.
E. Create a class called TIME that has separate data members for Hours(int), Minutes(int) and
Seconds(int). One constructor should initialize this data to zero, and another should
initialize it to fixed values. Overload the constructors to do this. Add a member function to
display time in 11:59:59 format. Add another member function addTIME to add two
objects of type TIME passed as arguments. The main() program should create two
initialized Time objects and one that is not initialized. Then it should add two initialized
value together, leaving the result in the third Time variable. Finally, it should display the
value of this third variable.
3

4. Programs using array as data members of a class and array of objects

A. Create a class called Employee that contains the Name and Employee number (type
long). Include a member function called getData() to get data from the user for insertion
into the object, another function called putData() to display the data. The name should
have no embedded blanks. Write a main() program to exercise this class. It should create
array of type Employee and then invite the user to input data for up to 10 employees.
Finally, it should print out the data for all the employees. (Use setw, setiosflags to format
the output).
B. Write a program that calculates the average of up to 10 English distances input by the
user. Create an array of objects of the Distance class with the data members as feets and
inches to calculate the average create a member function called avgDistance(). (Hint: 12
inches = 1 Feet)
C. Write a program to create a class HEIGHT with feet and inches as members. Write
member functions to READ, find the maximum height (MAX), find the mimimum height
(MIN) and average height (AVG) for arrays of object.
D. Write a program to create a class Date with day, month and year as members. Write a
member function to validate the dates and display the invalid dates. Test the class with an
array of Date objcts.

5. Programs using String class


A. Write a program to create a class String_manip with data member as char array. Write a
member function to extract and remove the given substring from the original string.
B. Wrtie a progeam to create a class Name with data member as char array. Write a
member function to erase a particular character from the given input C++ string.
C. Wrtie a program to create a class STUD with data members first_name, last_name,
DOB. Create an array of ‘n’ students by reading last_name, first_name and DOB into a
single string separated by commas and then extract each component into separate data
members. Display the student data in the form of a table with headers.
D. Write a program to compare two C++ strings (String class) and find whether they are
exactly equal or not.
E. Write a program to sort the array of string objects of standard c++ sring claas.

6. Programs using operator overloading


A. Overloading + operator to add to two distance objects
B. Overloading == operators to compare two strings
C. Overload ++, +=, *= for matrix objects.
D. Overload == to check equality og heights
E. For employee class check the seniority of employess based on joining date by overloading
>, < and == operators.

7. Programs for data conversion using overloading


A. Program to show how a compiler explicitly converts basic data types.
B. Program to demonstrate the conversion from user defined data type to basic data type.
C. Program to show the conversion from basic to user defined data type – for example from
centimeters(int) to Feet and Inches(user defined struct type).
4

D. Progarm to convert between two user defined classes – class of type feet,inches to class of
type metres,centimetres class and vice versa.

8. Programs using inheritance


A. Create a class called InputData. It has two private data members data_a and data_b. Set
the values of these two data members by using two public functions get_a() and get_b().
Derive a class called Arith_Unit from InputData. It contains the functions add(),sub(),
mul(),div() to perform arithmetic operations on data_a and data_b. Derive a class
Logic_Unit from InputData. It contains the functions and(), or() and xor() to perform
logical operations on data_a and data_b. Finally derive a class called ALUnit from
Arith_Unit and Logic_Unit classes. It has to perform arithmetic and logical operations
according to the given codes. Choose code 0 to code 6 to perform the said seven
operations. Write sample program to test the ALU class.
B. Create a class called Transmitter. It has three unsigned int private data members:
pay_load, parity and data_packet; It has one function called parity_bit(). It calculates
parity bit by XORing all pay_load bits and assign the value to parity. By using function
get pay_load(), pay_load is got. data_packet is calculated by multiplying parity_bit with
32768 and add the product with (pay_load – 32768). Create another class called
Receiver. It has unsigned int private data members Rx_Pkt , Rx_Data and E_Flag.
Rx_Data is got from Rx_Data as given below. First E_Flag is calculated from Rx_Pkt by
XORing all bits. If E_Flag is zero, then Rx_Data = Rx_Pkt – 32768, Else a message
should warn about corrupted packet. Derive a class called Tranceiver from the above two
classes and check the functionalities. Use any other functions if needed.
C. Define a class Employee with data members as empno, name and designation. Derive a
class Qualification from Employee that has data members UG, PG and experience. Create
another class Salary which is derived from both these classes to display the details of the
employee and compute their increments based on their experience and qualifications.
D. Define a class Student with data members as rollno and name. Derive a class Fees from
student that has a data member fees and functions to submit fees and generate receipt.
Derive another class Result from Student that displays the marks and grade obtained by the
student. Write a program that extends the class Result so that the final result of the Student
is evaluated based on the marks obtained in tests, activities and sports.
E. Write a program that has a class Train with data members seats_first_class,
seats_second_class and seats_ac_2tier and member functions to set and display data.
Derive a class Reservation that has data members booked_first_class, booked_second_class
and booked_ac_2tier and functions to book and cancel tickets and display status.
F. Write a program for a publishing company that markets both printed books and audio-
visual lectures stored on CDs. Write a class Publication that stores title and price. Derive a
class book which has an additional member as no_pages and a class Lecture with member
play_time.
G. Create a class called Employee with protected data members emp_id, name and
designation. It contains the member functions to get details of employee and display them.
Derive two classes Permanent and Contract from Employee class. Contract has data
members num_hrs and wages_per_hr. Permanent has basic, DA, TA and HRA. Get the
5

necessary details using member functions and display the employee details with their salary
according to the given data.
H. Create a class named EB_amount. It has the data members units_used and bill. Use
member function to set unit_used. Upto 200 units 3 rupees per unit, 201 to 500, 4 rupees
per and above 500 5.5 rupees per unit are allotted by EB. Calculate the bill amount and
display the amount. Create another class Salary with basic, DA and HRA. Set basic by a
member function. 104 percent of basic is assigned as DA and 10 percent is allotted as
HRA. Display the total salary. Derive a class Budget contains income, tuition_fee,
house_rent, saving, grocery, eb_bill as data members. Set the values and get the values of
income and eb_bill from parent classes. Display the budget details.
I. Create a base class called Shape which has two double type values. Use member functions
get() and set() to get and set the values. Use a virtual function display_area() to display the
area. Derive two specific classes called Triangle and Rectangle from the base class Shape.
Redefine display_area() in the derived classes. Write a program to display the rectangle or
triangle area.
J. Create a base class called Radix10 which has a member function decimal. It can be set and
display by member functions. It also contains a virtual function called convert. Derive two
classed from Radix10. One is called Radix2 and the next is called Radix16. They redefine
the convert function such that the decimal value is displayed into binary or hexa_decimal.
They have a data member length which can be set by member functions. To convert the
decimal data is divided by appropriate radix length times and the remainder which is
always from 0 to (radix-1) should be displayed in reverse order. In hexa decimal, for
remainder 10, 11, 12 ,13, 14 and 15 the codes A, B, C, D, E or F should be used
respectively.

9. Programs using virtual functions and friend functions

A. Create a class Date whose object can store a day, month and year. Include the
necessary constructors to initialize the objects and function to display the date in
‘dd/mm/yyyy’ format. Define a non-member function findAge(Date dob, Date today)
which should return the calculated age from the input dates ‘dob’ and ‘today’. Set this
function as friend to Date class. Write a main function to read the today’s date and
date of birth of a person from the user and display the age of that person by calling
the proper function.
B. Create a class Matrix to store a m x n matrix. Include necessary constructors and
functions to initialize and display the matrix. Using friend function, find the dot
product of two input matrices.
C. Create two classes IntArray to store the set of integer numbers and FloatArray to store
decimal numbers. Add a member function read() for both classes for reading inputs.
Create two objects ‘x’ for IntArray and ‘y’ for FloatArray. Read the inputs for x and y.
Using a friend function maxmin(x,y), display the maximum and minimum among the set
of integers and decimal numbers.
D. Create a class Rational whose object can store a rational number. (For ex: ¾). Include
the necessary constructors to initialize the objects and function to display the rational
number in x/y format. Define the following non-member functions.
6

E. addRational(Rational r1, Rational r2) : which should return a summation of the two
input rational numbers r1 and r2.
F. mulRational(Rational r1, Rational r2) : which should return the product of the r1 and r2.
G. Set these two functions as friend to the Rational class
H. Main function to read two rational numbers and display the sum and product of the input
by calling the proper functions.

10. Programs using file Input / Output

A. Write a program to writing in to a text file, read from a text file and display
B. Write a program to copy the content of one file into another
C. Write a program to convert the text file contents to Upper-case & write the contents into
another file.
D. Write a program to create a numeric file and split the file into two files, one
containing all odd numbers and another with even numbers.
E. Write a program to compare two text/data files for equality of contents.
F. Write a program to create a file that contains employee and display the contents in a
neatly formatted manner.
G. Write a program to store Student objects in a file, search for a student and display his
details. The program also should display the number of the object from the beginning
and the end of the file.

You might also like