COMPUTER ORGANIZATION AND DESIGN



CODE: CSE-215

CSE 215 OBJECT ORIENTED PROGRAMMING USING C++ LAB

[0 0 3 1]

III Sem B.E. ( CS&E)

2011

Prepared by Approved by

Dr. Harish S. V. H.O.D.

DEPT. OF COMPUTER SCIENCE & ENGG.

M. I. T., MANIPAL

INSTRUCTIONS TO STUDENTS

1. Students should be regular and come prepared for the lab practice,

maintaining silence and discipline in the lab.

2. In case a student misses a class, it is his/her responsibility to complete

those missed experiment(s).

3. Students should bring the observation book.

4. Prescribed textbook and class notes can be kept ready for reference, if

required.

5.They should implement the given problems individually.

6. While conducting the experiments students should see that their programs would meet

the following criteria:

• Programs should be interactive with appropriate prompt messages, error messages if any, and descriptive messages for outputs.

• Programs should perform input validation (Data type, range error, etc.) and give appropriate error messages and suggest corrective actions.

• Comments should wherever needed and function names/signatures should indicate the purpose of the function, inputs and outputs properly.

• Programs should be properly indented.

• Meaningful names to be used for all variables.

• Constants and type definitions have to be used wherever needed.

7. Proper executing programs to be demonstrated to the instructors copied to the

observation and has to be signed by the instructors.

8. Questions for lab tests and exam need not necessarily be limited to the questions in the

manual, but could involve some variations and / or combinations of the questions.

CONTENTS

SL NO. TITLE OF EXPERIMENT PAGE NO.

1. Review of functions, structures 5

2. Structures, character pointers 5

3. Character pointers contd. 6

4. Classes, data members, methods, friend functions 6

5. Constructors, static data members, static functions 7

6. Dynamic allocation, destructors, operator overloading 8

7. Same as above 8

8. Matrix, stack classes, operator overloading 9

9. Singly, doubly linked lists 9

10. Inheritance, virtual functions, virtual classes 10

11. Templates, overloaded functions 11

12. Files 11

13. Test 11

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)

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

Week 4 (Classes, data members, methods, friend functions)

1. Define a class to represent a Complex number. 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 the first is greater or smaller than the second one.

3. Create 2 classes namely Rectangle and Circle with suitable data members. Provide functions namely input, display and area to these classes. Input is for inputting the dimensions, display is for displaying the dimensions as well as area and area function is for calculating the area and displaying the same. Write a friend function of class rectangle which compares the areas of the rectangle object with a circle object and displays which one is greater.

4. Create 2 classes named Meter and Feet which store the value of distances. Provide input and display functions to these classes. Meter object stores distances in meters and centimeters; Feet object stores distance in feet and inches. Write a friend function which adds a Meter object to a Feet object. The resultant object can be a Meter object or a Feet object. The display should be in the form of feet & inches or in the form of meters & centimeters.

Week 5 (Constructors, static data members and static functions.)

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

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

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.

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

Week 6 and Week 7 (Dynamic memory allocation, destructors, operator overloading)

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 fn. called display to display the array contents.

5. A fn. called search to search for an element in the array.

6. A fn. called compare which compares 2 IntArr objects for equality.

7. Overload + operator to add 2 IntArr objects.

8. “= =” to compare for equality of 2 objects.

9. “[ ]” to retrieve a integer from the specified index.

10. Also overload input output operators for this class( Week 7)

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. Overload + operator to concatenate 2 string objects. {Note:- proper memory allocation to be done}

5. A display function to display the string object.

6. A destructor to deallocate the memory which was allocated dynamically.

7. ChangeCase, which converts all lower case to upper case and vice versa.

8. Reverse, which reverses the character array. (Week 7)

9. Also overload input and output operators. ( Week 7)

10. “[ ]” to retrieve a character from the specified index ( Week 7)

Week 8 (Matrix class, Stack class, operator overloading)

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. Overload input output operators to read and display Matrix object.

4. A fn. which displays only the principal diagonal elements.

5. A fn. which sorts the matrix in row major order.

6. Also overload + and – operators to add and subtract 2 matrices and to hold the result in a third matrix.

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 fn. to display the matrix.

5. IsEmpty, to test if the stack is empty or not.

6. IsFull , to test if the stack is full or not.

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

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

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 fn. which inserts a NODE at the beginning of the list.

iv) “Add_At_Last” member fn. which inserts a NODE at the end of the list.

v) “Delete_At_Head” member fn. which deletes a NODE from the first of the list.

vi) “Delete_At_Tail” member fn. 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 (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 char. array or string field to represent the final result. Also it has a member fn. 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 fn. called GetData to initialize base class data members and another member fn. displayArea to compute and display the area of figures. Make displayArea a virtual fn. and redefine this fn. 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 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 11 (Function Templates, class 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.

5. Write 3 overloaded functions called computeArea which are used to compute the area of a triangle, a rectangle and a circle, respectively. Show the invocation of these functions in the main.

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

Week 12 Files

1. Write a program to copy the contents of one file to another.

2. Write a program to change all characters in a file to uppercase.

3. Write a program to count the number of words, new lines and tabs in a file.

4. Write a program to extract all the words of a file and display them on the screen. Assume that every 2 words is separated by a blank, tab or new line.

5. Write a program to append one file to another.

Week 13

Test

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download