CSE 213 - OBJECT ORIENTED PROGRAMMING USING C++ LAB MANUAL

III Sem BE (CS&E)

2013

Prepared ByApproved By

1. Manjula Shenoy K

2. Mamatha Balachandra

3. Praseeda (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., overloaded functions

Week 4:Classes, data members, methods

Week 5:Friend functions, static members, objects & arrays

Week 6: Constructors

Week 7:Dynamic memory allocation, destructors

Week 8: Matrix class, stack class

Week 9: Singly, doubly linked lists

Week 10: Operator overloading

Week 11: Inheritance, virtual functions, virtual classes

Week 12: Function templates, class templates, exception handling

Week 13: Test (batch I)

Week 14: Test (batch II)

PROCEDURE FOR EVALUATION

Student will be evaluated based on following criteria

Implementation of experiments,

Observation and /or Journal and 60% ( 60 Marks )

Viva Voce

Test 40% (40 Marks )

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 id’s 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, overloaded functions)

(1) Write a user defined function which compares 2 strings and determines whether the first one is greater, smaller or equal to the second string. If they are equal return 0, if first string is greater return 1 and if the first string is lesser return -1. Pass the two strings to be compared as parameters to the function. . Note:- Comparison has to be done in alphabetical order. Eg:- abc is smaller than abd, greater than abb and is equal to abc. Show the usage of this function in the main.

(2) 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.

(3) 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.

(4) Write separate functions to swap 2 integers making use of (i) pointer parameters and (ii) reference parameters.

(5) Write an overloaded function called computeArea which is used to compute the area of a triangle, a rectangle and a circle, respectively. Show the invocation of these functions in the main.

Week 4 (Classes, data members, methods)

1a. Define a class to represent a complex numbercalled 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.

4. Create a class called Stack for storing integers. The data members are an integer array for storing the integers and an integer for storing the top of stack (tos). Include member functions for initializing tos to 0, pushing an element to the stack and for popping an element from the stack. The push() function should check for “stack overflow” and pop() should check for “stack underflow”.

Week 5 (Friend functions, static members, objects & arrays)

1. Create 2 classes namely Rectangle and Circle with suitable data members. Providemember functions namely input and display to these classes. Function input is for inputting the dimensions and calculating the area. The area should be stored along with the input data members. Function display is for displaying the dimensions as well as area. Write a friend function (called larger) of class Rectangle and class Circle which compares the areas of the rectangle object with a circle object and displays which one is greater.

2. Modify the above program by using friend class. Make function larger as a member function of class Circle and make class Circle as a friend of class Rectangle.

3. Create a class called Counterthat 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.

4. Define a class called Customer that holds private fields for a customer ID number, name and credit limit. Include public functions that set each of the three fields and also for displaying a customer’s data. Write a main() function that declares an array of 5 Customer objects. Prompt the user for values for each Customer, and display all 5 Customer objects.

Week 6 (Constructors)

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. Define a class to represent a Bank account. Include the following members.

Data members:-

  1. Name of the depositor
  2. Account number.
  3. Type of account.
  4. Balance amount in the account.
  5. Rate of interest (static data)

Provide a default constructor, a parameterized constructor and a copy constructor to this class.

Also provide Member Functions:-

1. To deposit amount.

2. To withdraw amount after checking for minimum balance.

3. To display all the details of an account holder.

4. Display rate of interest (a static function)

Illustrate all the constructors as well as all the methods by defining objects.

Week 7 (Dynamic memory allocation, destructors)

1. Define a class IntArr which hosts an array of integers. Provide the following

member functions:-

  1. A default constructor.
  2. A parameterized constructor which initializes the array of the object.
  3. A copy constructor.
  4. A function called display to display the array contents.
  5. A function called search to search for an element in the array.
  6. A function called compare which compares 2 IntArr objects for equality.

2. 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.

Week 8 (Matrix class, 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 intialises 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 9 (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 10 (Operator overloading)

1. Continue question 1 of Week 7

  1. Overload + operator to add 2 IntArr objects.
  2. Overload “= =” to compare for equality of 2 objects.
  3. Overload “[ ]” to retrieve an integer from the specified index.
  4. Overload input output operators for this class.

2. Continue question 2 of Week 7

  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. Continue question 1 of Week 8

  1. Overload input output operators to read and display a Matrix object.
  2. Overload + and – operators to add and subtract 2 matrices and to hold the result in a third matrix.

4. Continue question 2 of Week 8

1. Overload postfix decrement operator to pop an item from the stack.

2. Overload prefix increment operator to push an item to the stack.

Week 11 (Inheritance-method overriding, virtual functions, virtual classes)

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.

3. Define a class which represents a single Linked List. Provide operations like add, delete which operates at the end of the list. Also provide a method to display the list and to check if it is empty or not. Design a second class called doubleEndedList where additions and deletions can be done at both ends of the list. Make this class inherit the relevant properties of the first class. Show the usage of both the classes.

4. Solve the above problem by making the lists as Doubly linked lists.

Week 12 (Function Templates, class Templates, exception handling)

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.

5. Design a template Stack which can work with either a Student record or an Employee record. A Student record contains name, rollNo and cgpa. An Employee record contains name, empId and salary fields. Provide push, pop, display functions to the template stack class.

6. Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in main()

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.