Sie sind auf Seite 1von 16

CPP Lab Exercises:

A.OOPs and C++ Introduction


Lab Exercise No: 1
Exercise Objective(s):

Exercise:
There are N seats in a cricket stadium (get the N as input from the user).
As part of the promotions for a match, every 7th seat is gets a 7-Up TShirt. The first 200 seats are allocated for VIPs and last 400 seats are
unreserved. These seats are not eligible for the T-Shirts. Write a program
to find out which seats are eligible to get T-Shirt in the upcoming match.
Recommended duration: 15 mins
Solution Guidance (if applicable): NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:
Write a program to in C++ to read 10 numbers (positive and negative
numbers). Print positive numbers and negative numbers separately in
descending order. (30 mins)
Recommended duration: 15 mins
Solution Guidance (if applicable): NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
Implement Matrix addition, multiplication and transpose using functions.
Provide a separate function that prints the matrix. Pass the matrix by
reference. Use const references where ever needed.
Recommended duration: 30 mins
Solution Guidance (if applicable): NA
Lab Exercise No: 4

Exercise Objective(s):

Exercise:
Write an inline function that computes the length of the array.
Recommended duration: NA
Solution Guidance (if applicable): NA
Lab Exercise No: 5
Exercise Objective(s):

Exercise:
Change the sort exercise to
include this inline function to compute the size of the array.
Instead of 10 numbers, allow user to enter any number of numbers
Recommended duration: NA
Solution Guidance (if applicable): NA
Lab Exercise No: 6
Exercise Objective(s):

Exercise:
Write a program that asks the user to type an integer N and computes the
sum of the cubes from 5 to the power 3 to N the power 3. Use inline
function to compute the power.
Recommended duration: NA
Solution Guidance (if applicable): NA
Lab Exercise No: 7
Exercise Objective(s):

Exercise:
For Numerology application, read date, moth and year of birth and form a
number in the form DDMMYYYY. Use recursive function find out the birth
number. Birth number is obtained by adding the digits of the number

continuously until a single digit is reached. Pass the number using


reference. If the year is not entered, the default year should be 2000.
For example, if date, month and year entered are 28, 8, 1991,
then
the number is 2881991. Now add the entire numbers like this
2+8++0+8+1+9+9+1 = 38
Result is not a single digit number, now add these digits still it is getting
single digit number 3 + 8 = 11
1+1=2
Display the output as Birthnumber is 2
Recommended duration: 30 mins
Solution Guidance (if applicable): NA

B. Encapsulation and Classes


Lab Exercise No: 1
Exercise Objective(s):

Exercise:

Create a class that represents a time in the form of hours, minutes and
seconds. Accept the time parameters from the user in the form of
a) hours, minutes and seconds
b) hh:mm:ss
Only valid time must be allowed to be created.
Display the time in the form of hh:mm:ss.
Recommended duration: 30 mins
Solution Guidance (if applicable): use string library that you learnt in
C

Lab Exercise No: 2


Exercise Objective(s):

Exercise:
Write a class ACCOUNT that represents your bank account. The class
should allow you to deposit money, withdraw money and send an error
message if you go overdrawn. The name of the customer and the address
must also be encapsulated. The address struct should have house
number, street number, area, town, city, state, and pin. The account must
have methods to display name and address of the customer.
Recommended duration: 30 mins
Solution Guidance (if applicable):NA

Lab Exercise No: 3


Exercise Objective(s):

Exercise:
Create a Linked List class that encapsulates a Node class. Provide
methods to add and delete values from the linked list.
Recommended duration: 45 mins
Solution Guidance (if applicable):NA

C. Constructors and Destructors


Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Create a class called Date with day, month and year. The class should have
constructors. If month is not given it must default to 1, if year is not given it must
default to the current year. The default date must be set to 1/1/2000. Provide a
member function displayDate that displays the month, day and year separated by
forward slashes(/). Provide a method that helps in computing the age. If the
difference in month is greater than 6 months, then the age must be difference in
years +1. Write a test program that demonstrates all these capabilities.
Recommended duration: 30 mins
Solution Guidance (if applicable):NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:
For the Linked List class that you created, add constructors and a
destructors. (15 mins)
Recommended duration: 15 mins
Solution Guidance (if applicable):NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
For the ACCOUNT class that you created, add constructors destructors.
Provide multiple constructors and if the balance is not entered while
creating, it should default to 1000. All the strings allocated must be
freed in the destructor.
Recommended duration: NA
Solution Guidance (if applicable):NA
Lab Exercise No: 4
Exercise Objective(s):

Exercise:
Create a class Employee that stores data about employee consisting of
name, employee code , gender, location (East, West, North, South).
Employee code must be readonly. Whenever new employee object is
created, employee code is automatically generated. The employee
code is a combination of the first letter of the location and sequence
number. The sequence number start from 0001 and is incremented till
9999, after which the application must generate an error. (30 mins)
Recommended duration: 30mins
Solution Guidance (if applicable):NA
Lab Exercise No: 5
Exercise Objective(s):

Exercise:
Create a class that records patients general details at the front desk
before they are sent to the doctor. The details recorded are like name,
address, complaints/sickness, temperature and blood pressure. The
normal temperature for a human being is between 97.0 and 99.5
degrees and blood pressure is 120/80 to 139/89. When the patient
detail is printed, the pressure and temperature status as to high or
low must be displayed.
Recommended duration: 30mins
Solution Guidance (if applicable):NA
Lab Exercise No: 6
Exercise Objective(s):

Exercise:
For the linked list exercise, change the malloc and free to new and
delete.
Recommended duration: 15mins
Solution Guidance (if applicable):NA
Lab Exercise No: 7
Exercise Objective(s):

Exercise:
Use the date class that you created to create a collection of date objects.
Initially let the size be 5. if user enters more dates then increase the size to
double of its original size. Print the ages in sorted order. (30 mins)
Recommended duration: 15mins
Solution Guidance (if applicable):NA

D. Inheritance
Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Create a base class with Vehicle and create the derived class as Car,
Truck and Cycle. Write a program to read the no of wheels in each
vehicle, maximum limit of petrol consumption and mileage for each
derived object.
Recommended duration: 15mins
Solution Guidance (if applicable):NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:
Write a CPP program with Base class as Animal, Derived class as Domestic
and Wild animals. From these classes, create derived class as Tiger, Cow,
Elephant and Horse. Few animals will come under both domestic as well as

wild animal categories. Create the classes and define methods to get / set
living place, eating preference (carnivore,herbivore). (20 mins)

Animal
Domestic
Cow

Wild
Elepha
nt

Tiger

Recommended duration: 20mins


Solution Guidance (if applicable):NA

E.Overloading:
Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Create a class called Calculator which has 4 different methods add and mul
that accept two numbers as parameters. Overload the methods such that the
parameters can be of the following patterns:
Both are of int data type.
Both are of double data type.
First parameter is of int data type and second parameter is of
double data type.
First parameter is of double data type and second parameter is of
int data type.
Create an object to access these methods and invoke these methods with
different type of numbers and display the results for the corresponding
methods.
Recommended duration: 20mins
Solution Guidance (if applicable):NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:

1) Write a program with 2 overloaded functions


char Increment(char c, int i)
Accepts char c as alphabets either from A-Z or from a-z. Increments c by
the specified int i. If on incrementing, the end Z or z is reached, then it
must cycle back to starting A or a. For instance, if c is a, c+26 must
return a and c+27 must return b etc.
int Increment(int i, char c)
Returns i by incrementing it by the ascii value of char c.
Recommended duration: 20mins
Solution Guidance (if applicable):NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
*Create a matrix class and overload following operators:
+,- for adding and subtracting 2 matrix
++,-- for pre and post increment and decrement
Unary to transpose the matrix
Implement these as member function.
Recommended duration: NA
Solution Guidance (if applicable):NA
Lab Exercise No: 4
Exercise Objective(s):

Exercise:
*For the Date class implement the following
Overload + that will add x number of days to the date
Deep copy for dates
== , > and < to compare dates
Recommended duration: NA
Solution Guidance (if applicable):NA
Lab Exercise No: 5
Exercise Objective(s):

Exercise:

Class Duration encapsulates hours and minutes. Provide an overloaded


cast function to converted it into double. For instance, 2 hours and 30
minutes when converted into double would be 2.5 hours. Similarly 5 hours
45 minutes when converted into double would be 5.75.
Recommended duration: 15mins
Solution Guidance (if applicable):NA
Lab Exercise No: 6
Exercise Objective(s):

Exercise:
Also overload () such that when you call duration(1,2) it must add the 1 to
hour part and 2 to minutes part of the duration.
Recommended duration: 15mins
Solution Guidance (if applicable):NA

F.POLYMORPHISM:
Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Create a class called Worker. Write classes DailyWorker and
SalariedWorker that inherit from Worker. Every worker has a name and a
salary. Write method pay() to compute the week pay of every worker. A
Daily worker is paid on the basis of the number of days she/he works. The
salaried worker gets paid the wage for 40 hours a week no matter what
the actual worked hours are. Create a few different types of workers, and
pass these as an array to a function that print all the details of the
workers(name, salary and D/W (indicating the type of worker)).
Recommended duration: 30mins
Solution Guidance (if applicable):NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:
In the previous exercise which function will you make abstract. Change the
code accordingly
Recommended duration: 10mins
Solution Guidance (if applicable):NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
The Charity Collection Box contains money in different currencies - dollarscents or pounds-pence or rupees-paise. All of these currencies have notes
and coins. The note and coin numbers are counted when they are added
based on their value (that is number of 5 rupee notes, or $1 dollar note).
*A super class representing Currency is created with different
denomination for notes and coins. Subclass Dollar, Pound and Rupee has
conversion methods to rupees, print() and compute().
*Create class called CollectionBox that allows entry of these currencies in
terms of number of notes and coins of different denomination. Create a
display method that allows any of these currency types and displays the
total amount collected in terms of Rupees. (Assume1 dollar= Rs. 50 and 1
pound = 1.6232 U.S. dollars)
Recommended duration: NA
Solution Guidance (if applicable):NA

G.Templates
Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Write a template function to implement binary search. Instantiate
the function to make it work for double, strings and the Worker
class that you created in the previous session.
Recommended duration: 30mins
Solution Guidance (if applicable):NA

Lab Exercise No: 2


Exercise Objective(s):

Exercise:
1) Create a Stack class that can take any type of data.
Recommended duration: 10mins
Solution Guidance (if applicable):NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
Create another class that uses the queue for Worker objects.
Recommended duration: 10mins
Solution Guidance (if applicable): NA
Lab Exercise No: 4
Exercise Objective(s):

Exercise:
Create a hash table that can take key of any type and value of any
type. The size of the hash table will be specified at the time of
instantiation. Provide 2 overloaded functions called get
1. gets the value based on the key
2. gets the key and value based on index position.
Also provide a method to add key and value.
Recommended duration: 30mins
Solution Guidance (if applicable): NA
Lab Exercise No: 5
Exercise Objective(s):

Exercise:
Provide a generic sort function. Provide specialization for strings.
Recommended duration: 20mins
Solution Guidance (if applicable): NA

Lab Exercise No: 6


Exercise Objective(s):

Exercise:
From hashtable class create a specialized class that represents a
dictionary with words and meaning.
Recommended duration: 20mins
Solution Guidance (if applicable): NA
Lab Exercise No: 7
Exercise Objective(s):

Exercise:
Make the add method as private in hash table class Create a friend
function that adds the words and meaning for the dictionary
implemented in the previous exercise.
Recommended duration: 20mins
Solution Guidance (if applicable): NA

H. STL
Lab Exercise No: 1
Exercise Objective(s):

Exercise:
Write a STL program that declares a vector of integer values, stores
five arbitrary
values in the vector and then prints the single vector elements
to cout. (15 mins)
Recommended duration: 15mins
Solution Guidance (if applicable): NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:

Write a program that inserts 10 employee details in a list. Sort and


print the elements.
Recommended duration: 15mins
Solution Guidance (if applicable): NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
3) Create a container that will maintain history of 10 items. When
history is listed the
newer item must be displayed first. The older history items must
be discarded when
new history items are added and the size grows beyond 10.
Recommended duration: 15mins
Solution Guidance (if applicable): NA
Lab Exercise No: 4
Exercise Objective(s):

Exercise:
Use stack to convert infix notation to postfix notation.
Infix: 3+4*5/6
Postfix: 3 4 5 * 6 /
Recommended duration: 15mins
Solution Guidance (if applicable): Go through priority_queue
Lab Exercise No: 5
Exercise Objective(s):

Exercise:
Given an array of employee objects. Remove the duplicates.
Recommended duration: 15mins
Solution Guidance (if applicable):
Lab Exercise No: 6

Exercise Objective(s):

Exercise:
Implement Phone book
Recommended duration: 15mins
Solution Guidance (if applicable):

I.

Exception Handling

Lab Exercise No: 1


Exercise Objective(s):

Exercise:
1) Match the right options
Scenarios
Option

Application encounters a problem while deleting an object


Catch exception
If user tries to withdraw more than what is in the bank,
prompt the user and display the amount available.
Code as normal if
System must store the data in the database and this must
statement
be 100% reliable. So, if connection fails, application stores
the data in a file temporarily.
If user enters any alphabet other than d, w, t or v take user
to the main menu.
If customer does not return a book in time, an reminder
email has to be sent.
Recommended duration: NA
Solution Guidance (if applicable):NA
J.IO

Lab Exercise No: 1


Exercise Objective(s):

Exercise:

Write a program to read data for n students. Write it to the file in


sorted order. Use
STD and string
Recommended duration: 15mins
Solution Guidance (if applicable):NA
Lab Exercise No: 2
Exercise Objective(s):

Exercise:
Write a program that helps in searching for an student by name or roll
number.
Recommended duration: 15mins
Solution Guidance (if applicable):NA
Lab Exercise No: 3
Exercise Objective(s):

Exercise:
Create a database kind menu-driven application that allows, insertion,
deletion, updation, search facilities for student records.
Recommended duration: 30mins
Solution Guidance (if applicable):NA
Lab Exercise No: 4
Exercise Objective(s):

Exercise:
Reverse a file by opening it in input/output mode. Assume fixed
number of characters in the file.
Recommended duration: 30mins
Solution Guidance (if applicable):NA

Das könnte Ihnen auch gefallen