Sei sulla pagina 1di 10

OBJECT ORIENTED PROGRAMMING

USING C++ LAB MANUAL


II Sem BS (CS)

2014

Prepared By

Approved By

Mamatha Balachandra

(H.O.D)

DEPT OF COMPTER SCIENCE & ENGG.


M. I. T., MANIPAL

Week-wise Schedule
Week 1

Review of functions, structures

Week 2

Structures, character pointers

Week 3

Character pointers contd.

Week 4

Classes, friend functions

Week 5

Constructors, static members

Week 6

Inheritance, Dynamic method dispatch

Week 7

Files

Week 8

Dynamic allocation, destructors, operator overloading

Week 9

Operator overloading continued

Week 10

Matrix class, stack class

Week 11

Singly, doubly linked lists

Week 12

Templates, overloaded functions

Week 13

Test (batch I)

Week 14

Test (batch II)

PROCEDURE FOR EVALUATION


Student will be evaluated based on following criteria

Observation and /or Journal and

5M

Test

20M

The test will be conducted after completion of twelve experiments / entire lab
syllabus.

Week 1 (Review of functions, structures)


{Aim:- Defining functions, invoking functions, passing parameters to
functions and returning values from functions. (all work on integers)}
Write functions for the following and show their invocation in main.
(1) A function to search for an integer in a list of integers. If found the return value of the
function is 1 else return value is -1. Pass the list of integers, length of the array and the key to be
searched as parameters to the function.
(2) A function which determines the biggest element in an array of integers. The function returns
this number. Pass the list of integers and length of the array as parameters to the function.
(3) Write a function which sorts an array of integers in ascending order using bubble sort. The
length of the array and the array are passed as parameters to the function.
(4) Define a structure called Rectangle which keeps track of the length and breadth of a
rectangle. Write functions namely input, displayDimensions, displayArea and edit to input the
dimensions of a rectangle, to display the dimensions, to calculate and display the area of a
triangle respectively. Write a main function which defines a variable of type Rectangle. Invoke
these functions in main and observe the result.

Week 2 (structures, character pointers)


(1) Define a structure to represent a Student. Keep track of name, id and cgpa. Write functions
namely input, display and edit to input details to a Student variable, to display it and to edit it
respectively. Write a main function which defines a student variable. Invoke these functions in
main and observe the result.
(2) Define the same Student structure. Also keep an array to hold 4 Student records. Write
functions namely input, display, arrange and search which inputs records to the array, displays
the array in the order of input, arranges the records in order of ids and searches for a record
taking id as the key. Provide a suitable menu for this program.
(3) Write a user defined function which determines the length of a given string. The only
parameter to the function is the string. The return value of the function should be the length.
Show the usage of this function in the main.
(4) Write a user defined function which concatenates one string to another. Pass the two strings
as parameters to the function. The return value of the function should be the concatenated string.
Show the usage of this function in the main.

Week 3 (character pointers continued)


(1) Write a program which makes use of strlen(), strcmp() and strcat() library functions to
determine the length of a string, compare 2 input strings, concatenate one string to the other
respectively.
(2) Write a function which sorts an array of strings in ascending order using bubble sort. The
number of strings in the array and the array are passed as parameters to the function.
(3) Write separate functions to swap 2 integers making use of (i) pointer parameters and (ii)
reference parameters.
(4) Write a function IsPalin() to check whether the given string is a palindrome or not, Write a
main function to test this function.

Week 4 (Classes, friend functions)


1. Define a class to represent a complex number called Complex. Provide the following member
functions:1. To assign initial values to the Complex object.
2. To display a complex number in a+ib format.
3. To add 2 complex numbers. (the return value should be complex)
4. To subtract 2 complex numbers
Write a main function to test the class.
2. Create a class called Time that has data members to represent hours, minutes and seconds.
Provide the following member functions:1. To assign initial values to the Time object.
2. To display a Time object in the form of hh:mm:ss {0 to 24 hours}
3. To add 2 Time objects (the return value should be a Time object)
4. To subtract 2 Time objects (the return value should be a Time object)
5. To compare 2 time objects and to determine if they are equal or if the first is greater or
smaller than the second one.

3a. Define a class named Movie. Include private fields for the title, year, and name of the
director. Include three public functions with the prototypes
void Movie::setTitle(char [ ]);
void Movie::setYear(int);
void Movie::setDirector(char [ ]);
Include another function that displays all the information about a Movie.
3b. Include a function which accepts 2 objects of type Movie and displays whether or not they
were released in the same year and also whether the Directors are same. String functions may be
used.

Week 5 (Constructors, static members)


1. Consider the already defined Complex class. Provide a default constructor, a parameterized
constructor and a copy constructor to this class. Also provide a display function. Illustrate all the
constructors as well as the display method by defining Complex objects.
2. Consider the already defined Time class. Provide a default constructor, a parameterized
constructor and a copy constructor to this class. Also provide a display function. Illustrate all the
constructors as well as the display method by defining Time objects.
3. Create a class called String which consists of only one data member which is a character
pointer. Provide the following member functions to this class:1. A default constructor which initializes the pointer to null value.
2. A parameterized constructor which receives a string as its parameter. {Note:memory allocation to be done}
3. A copy constructor which receives a string as its parameter. {Note:- memory
allocation to be done}
4. A display function to display the string object.
5. A destructor to deallocate the memory which was allocated dynamically.
6. ChangeCase, which converts all lower case to upper case and vice versa.
7. Reverse, which reverses the character array.

4. Create a class called Counter that contains a static data member to count the number of
Counter objects being created. Also define a static member function called showCount() which
displays the number of objects created at any given point of time. Illustrate this.

Week 6 (Inheritance, Dynamic method dispatch)


1. Design a base class called Student with the foll. 2 fields:- (i) Name (ii) Id. Derive 2 classes
called Sports and Exam from the Student base class. Class Sports has a field called s_grade and
class Exam has a field called e_grade which are integer fields. Derive a class called Results
which inherit from Sports and Exam. This class has a character array or string field to represent
the final result. Also it has a member function called display which can be used to display the
final result. Illustrate the usage of these classes in main.

2. Create a base class called Shape. Use this class to store 2 double type values which could be
used to compute the area of figures. Derive 2 specific classes called Triangle and Rectangle from
the base class Shape. Add to the base class, a member function called GetData to initialize base
class data members and another member function displayArea to compute and display the area of
figures. Make displayArea a virtual function and redefine this function in the derived classes to
suit their requirements. Using these three classes design a program which will accept dimensions
of a triangle or rectangle interactively and display the area.

Week 7 (Files)
1. Write a C++ program to create 2 text files: file1.txt and file2.txt. Store the contents in both the
files. Crate a third file main.txt. Copy the contents of file1.txt and file2.txt to the main.txt file and
display the resultant file.
2. Write a C++ program to create a file Integer.dat and store integers in it. Read the file
Integer.dat and copy the odd numbers into odd.dat file and even numbers to even.dat files.
Display the contents of both the files odd.dat and even.dat.
3.Write a C++ program to create a text file infile.txt and store some sentences in it. Create
second file called outfile.txt. Read the infile.txt file, convert and write lowercase letters to
uppercase letters to the file outfile.txt. Display outfile.txt.
4. Write a C++ program which demonstrates the writing of objects to a file.

Week 8 (Dynamic allocation, operator overloading)


1. Write a C++ program to dynamically allocate and de-allocate memory for a 2D array of order
M X N.

2. Write a function to overload + operator to add any two complex numbers and demonstrate this
by writing a suitable main function.
4. Create a class to represent a string, called String consisting of 2 private data members one
pointer to character and second for the length. Include appropriate constructors and destructors to
set the values of String object by allocating memory dynamically. Overload the operators ++
and -- to add and delete a character from the String object respectively.

Week 9 (Operator overloading continued)


1. Write a C++ program to demonstrate the usage of relational operator overloading.
2. Continue question 3 of Week 5
1. Overload + operator to concatenate 2 string objects. {Note:- proper memory allocation
to be done}
2. Overload input and output operators.
3. Overload [ ] to retrieve a character from the specified index
3. a) Create a class to represent a distance in kilometres and meters, called Distance1 consisting
of 2 private integer data members km and metre. Include a member function to set the values
of Distance1 object. These values have to be read from the keyboard. Create another class to
represent a distance in kilometres and miles, called Distance2 consisting of 2 private integer data
members km and mile. Include a member function to set the values of Distance2 object. These
values have to be read from the keyboard.
b) Using operator overloading convert both Distance1 and Distance2 class to float type, which
indicates the distance in kilometres only.
c) Overload + operator which takes 2 arguments, one of the type Distance1 and other of the type
Distance2, adds 2 distances and returns the result in float.
d) Write a main function which creates array on objects of size N of both classes Distance1 and
Distance 2. Using above functions input the value to these objects, add corresponding elements
of the array and store sum in an array of float type and display this array.

Week 10 (Matrix , Stack class)


1. Define a class which represents a matrix. Include the following members.
Data members:1. row, which represents the no. of rows.
2. col, which represents the no. of cols.
3. A 2-d array to hold the elements of the object.
Member Functions:1. A parameterized constructor of the form (int row, int col, int a[][]), which
initializes the row, col and the 2-d array.
2. A copy constructor.
3. A function which displays only the principal diagonal elements.
4. A function which sorts the matrix in row major order.
2. Define a class which represents a stack. Include the following members.
Data members:1. An integer array to hold the stack elements.
2. A member which represents the stack top.
3. A member which represents the maximum size of the stack.
Member Functions:1. A default constructor which intializes the member which represents stack top.
2. Push, to push an element.
3. Pop, to pop an element.
4. A function to display the stack.
5. IsEmpty, to test if the stack is empty or not.
6. IsFull , to test if the stack is full or not.

Week 11 (Single and doubly linked list)


1. Design a class which hosts a list. Include the following members.
Data members:(i) A structure of the form,
struct NODE
{
int item;
NODE *next;
};
(ii) 2 Node pointers called Head and Tail, which point to first node and last node of the
list respectively.
Member Functions:(i) A default constructor which initializes Head and Tail NODE pointers.
(ii) Also provide an adequate destructor to deallocate the dynamically allocated memory.
(iii) Add_At_First member function which inserts a NODE at the beginning of the list.

(iv) Add_At_Last member function which inserts a NODE at the end of the list.
(v) Delete_At_Head member function which deletes a NODE from the first of the list.
(vi) Delete_At_Tail member function which deletes a NODE from the last of the list.
(vii) Is_Empty which determines whether the list is empty or not.
(viii) display to display the list.
2. Solve the above same problem by making the list a doubly linked list.

Week 12 (Templates, Overloaded functions)


1. Write a template function to swap the values of 2 variables. Illustrate how you swap 2
integers, 2 characters as well as 2 doubles.
2. Write a template function to sort an array. Illustrate how you sort integer, character as well as
double arrays using the same template function.
3. Write a template function to search for a given key element from an array. Illustrate how you
perform search in integer, character as well as double arrays using the same template function.
4. Design a generic stack class which can be used to create integer, character or floating point
stack objects. Provide all necessary data members and member functions (push, pop, display &
default constructor) to operate on the stack.

Week 13
Test batch I

Week 14
Test batch II
Text Books:
1. Sourav Sahay, Object oriented programming with C++, Oxford Higher Education, 2008.
References:
1. Stanley B Lippman, Josee Lajoe, Barbara E Moo, C++ Primer, , 4th edition 2005.
2. Robert Lafore, Object oriented programming in C++, Galgotia publications, 3rd edition,
2006.

Potrebbero piacerti anche