OOCP Practical Assignment
1. Write a C++ program that will ask for a temprature in Fahrenheit and display it in Celsius using a class called temp and member funcions.
2. Create a class Distance, which accepts data in feet and inches adds two distances and displays the members of the distance object in the appropriate form. Test the class in the main program by creating object d1 and d2 of type distance, accept data for each object and add them then display them.
3. An electricity board charges the following rates to demostic users
For first 100 units 40p per unit
For next 200 units 50p per unit
Beyond 300 units 60p per unit
All users are charged a minimum of Rs. 500. If the total cost is more than Rs. 250.00 then an additional surcharge of 15% is added. Write a program to read the name of users and units consumed and print out the charges along with names.
4. An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot papers. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, that ballot should be considered as a 'spoilt ballot', and the program should also count the number of spoilt ballots.
5. A Cricket team has the table of batting figures for a series of test matches. Write a program to read the data name , runs , innings , times not out into a class object and calculate the batting average , also display the result in the format as given below :
----------------------------------------------------------------------------------------------
Player's name Runs Innings Times Not Batting
Out Average
------------------------------------------------------------------------------------------------
Tendulkar 632 15 0
Azharuddin 524 16 2
* * * *
------------------------------------------------------------------------------------------------
6.Write a program to evaluate the following equation v = p(1+r)^n and print the tables which would give the value of v for various combination of the following values of p, r and n.
p : 1000, 2000, .....10,000
r : 0.10, 0.11, 0.12.....0.20
n : 1,2,3,....10
7.Write a program to implement the push and pop functions of a stack.
8. Write a program to implement the push and pop functions of a stack using a class Stack. Also make use of a private member function display() to display contents of the stack after every push and pop operations. Create a member function init() to initialize top of the stack.
9.Define a class to represent Bank account. Include the following members. Data members
(1) Name of depositor
(2) Account number
(3) Type of account
(4) Balance member functions
(a) To assign initial values
(b) To deposit an amount in a particular account
(c) To withdraw an amount after checking the balance
(d) To display name and balance
Write a main program to test the class for handling 10 customers
10. Write a program that calculates the value of m raised to the power n for both int and double data types. (Use the concept of function overloading)
11.Write a function, which will take two objects of Distance Class as arguments and returns the largest one. Include a main() program to implement this function of the distance class.
12. Write a class to represent a vector ( a series of float values). Include member functions to perform the following tasks:
(a) To create the vector
(b) To modify the value of a given element
(c) To multiply by a scalar value
(d) To add a vector to another
(e) To display the vector in the form (10, 20, 30, ...)
13. Demonstrate the use of static variables and static function in a class by
using it to count the number of objects created in the program,
having a static function to display the count.
14. Imagine a check-post at a bridge. Car passing by the check-post are expected
to pay Rs. 50 as tax. Most of the cars pay but sometimes a car goes without
paying the tax. The check-post has to keep track of number of cars and amount collected.
Create a class check to implement this problem. The data members of the class are no,
to count number of class and amount to keep track of the amount collected.
Write member function paying for cars which are paying the tax and another function
nopay for cars not paying the tax, also write a function to display number of cars
passed and amount collected.
Write a menu driven main program with option for paying car, another for not paying
car, a option to display the result and a exit option. Create a single object of check
type to test the class.
15 . Create a class date which stores date in dd-mm-yyyy format.
Include appropriate constructors to initialize the objects.
Write a member function which gives the differences of given two dates as number
of days. Another function to which days can be added so as to given the date
after addition of days. Check the class by creating objects of the date class.
Checking program should be menu driven.
16. Create a class that contains variables for storing feet and its equivalent value
of inches. Pass to the class's constructor no. of feet and have the constructor
display the no. of inches.
17. Create a function sleep() that pauses the computer for the number of seconds
specified by its single argument. Overload sleep() so it can be called with either
an integer or a string representation of an integer. (e.g. sleep(10) & sleep("10")
both should be valid)
18. Write a class to represent a Matrix. Include member functions to perform the
following tasks:
Matrix
Data Members
Integer array of 10X10 elements.
Integer row, column //dimensions.
Member Functions
To create the Matrix.
To add a Matrix to another.
To subtract a Matrix by another.
To multiply a Matrix to another.
To multiply a Matrix by a scalar.
To divide a Matrix by a scalar.
To transpose a Matrix.
To modify the value of a given element.
To display the Matrix
19. Create a class Matrix with the following data members :
int **p and int d1,d2; include a paramerized constructor that takes two arguments
and allocates the memory for a two dimension matrix with d1 and d2 dimensions.
Also include a destructor. Overload +, - , * on objects of Matrix.
Also overload << and >> on objects of Matrix.
20. Given the following class specifications and using friend as a bridge, write a
function to calculate the volume, assign it to member vol in class volume and
display the value of vol.
class cylinder { int r,h;};
class volume { long vol;};
21. Following are the class specifications:
class A { int a;};
class B { int b;};
Using a friend function, calculate the max of two objects and display it.
22. Write a class to represent a vector (a linear array). Include member fuctions.
- default constructor to create vector dynamically of the size 1 and initialize its element to zero.
- parameterized constructor
- Overload the + operator to add two vectors
- Overload the * operator to multiply by a scaler value (scalar * vector or vector * scalar)
- Overload the >> operator to input a vector and the << operator to display the vector in the form (10,20....).
23. Write a menu driven program that can perform the following functions on strings. (Use overloaded operators where possible).
1. Compare two strings for equality ( == operator)
2. Check whether first string is smaller than the second ( <= operator )
3. Copy the string to another.
4. Extract a character from the string ( overload [] )
5. Reverse the string.
6. Concatenate two strings ( + operator )
24. Define two class Polar and Rectangle to represent points in the Polar and Rectangle systems. Use conversion routines to convert from one system to another.
25. Construct a class Distance having member variables float feets and float inches. Write a program to convert this class
into the basic data type float, which will
represent the total no. of inches of the class. Also include the code to accept the value of
inches in a float variable and convert this basic data type into class
Distance type having feets and inches as member variables.
26. Assume that the bank maintains two kinds of accounts for customers,
one called savings account and another called current account.
he saving account provides interest and withdrawal facilities but
no cheque book facility while current account provides no interest
facilities
but provides with cheque book, also the current a/c holder should
maintain a
minimum amount in a a/c else he has to pay service charges. Using
inheritance
concept create a base class account that stores account holder name,
account no,
and type of account, from this base class derive two classes sav_acc
and cur_acc.
These classes should include members
1) to accept deposit and update the balance
2) display balance
3) withdraw amount and update the balance
4) compute interest
5) check minimum balance and impose penalty.
27.Create two classes Grade and Student. The class Grade has data members
Grade
while student has data members such as roll no, name, and total marks of the
student. Making use of data of both the Classes print the roll no, name, and
grade of each student whose grade is set by the grade class.
28. Write a program that creates a base class called num. Have this
class hold an integer value and contain a virtual function called shownum().
Create two derived classes called outhex and outoct that inherit num.
Have the derived classes override shownum() so that it displays the value in
hexadecimal and octal respectively.
29. Make the use of the write function to display your name in the following
fashion.
n
na
nam
name
nam
na
n
30. Write a program to read a list containing item name, item code, and cost
interactively and produce a three column output as shown below:
NAME CODE COST
C++ 101 233.81
JAVA 2 32 456.34
HTML 31 99.00
31. Create a class phonebook having two data members to hold the name and phone number of that person.
Define appropriate constructors and member functions to maintain
a phonebook.
Write a program to create this phonebook in a binary file and read it back from
the same file. Also include the facility to update a phone number, given a name.
Search that name into the file and update the phone number.
32. Write a program that reads a file and creates another file which is identical to
the first one except that the cpncecutive spaces are repalced by one space.
Use command line arguments to suppply the input and output filenames at runtime.
33. Create a generic class Stack. Create push and pop member functions to perform push
and pop operations.
34. Write the BubbleSort function as a function template. Provide a specialization of
the function for strings.
35. Write a program which copies its standard input, line by line, to its standard
output.
36 . Write a program which copies a user-specified file to another user-specified file.
Your program should be able to copy text as well as binary files.
37. Write a program with the following:
(a) A function to add two double type numbers from keyboard.
(b) A function to calculate the division of these two numbers.
(c) A try block to throw an exception when a wrong type of data is keyed in
(d) A try block to detect and throw an exception if the condition
"divide-by-zero" occurs
(e) Appropriate catch block to handle the exceptions thrown.
38.Write a progrma that reads the name "Martin", "Luther", "King" from the
keyboard into three separate string objects and then concatenates them
into a new string object using
(a) + operator and
(b) append() function.
39.Define a namespace named Constants that contains declarations of some constants.
Write a program that uses the constants defined in the namespace Constants.
40. Write a program that uses typeid and dynamic_cast operator on a polymorphic
class hierarchy.