Lab Program C++ First Year SASTRA University
Lab Program C++ First Year SASTRA University
Lab Program C++ First Year SASTRA University
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.
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.
D. Progarm to convert between two user defined classes – class of type feet,inches to class of
type metres,centimetres class and vice versa.
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.
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.
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.