Vous êtes sur la page 1sur 25

College of Engineering .

LAB MANUAL CS2209 OBJECT ORIENTED PROGRAMMING LAB YEAR : II Yr B.E CSE SEMESTER : 3

LAB MANUAL
Sub Code & Name: CS2209 OBJECT ORIENTED PROGRAMMING LAB Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date:13-07-09 Page 2

LIST OF EXPERIMENTS :-

CYCLE I

IMPLEMENTATION IN C++

EX NO : 1

A) Income Tax Calculation Using Default Arguments B) Categorizing Employees Based On Designation Using Static Members C) Addition Of Two Private Data Members Using Friend Functions Matrix Vector Multiplication Manipulation Of Complex Numbers Using Operator Overloading And Type Conversions Matrix Manipulation Using Dynamic Memory Allocation Overloading New And Delete Operators Implementation Of Linked List Using Templates CYCLE II

EX NO : 2 EX NO : 3 EX NO : 4 EX NO : 5 EX NO : 6

EX NO : 7

A) Implementation Of Bubble Sort And Insertion Sort Using Templates B) Implementation Of Quik Sort And Merge Sort Using Templates Implementation Of Stack And Queue Using Exception Handling

EX NO : 8

EX NO : 9 Implementation Of Minimum Spanning Tree Using Inheritance EX NO : 10 Dynamic Polymorphism And RTTI EX NO : 11 Manipulation Of Complex Numbers Using File Functions

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 1a Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 3

Income Tax Calculation Using Default Arguments


Aim: To write a C++ program to calculate income tax using default arguments. Algorithm: 1. Start. 2. Create a class with two data members, name and salary. 3. Create two functions , one for calculating income tax using default percentage and other for calculating income tax using tax percentage read from the user. 4. First get the salary amount from the user. 5. Calculate income tax to be paid by both the methods. 6. Finally display the tax amount to be paid. 7. Stop. Sample input and output: MENU --------1. Default tax percentage calculation 2. Getting tax percentage from the user 3. Exit Enter the option = 1 Enter the name Enter the salary amount : Ramu : Rs. 15000

NAME : Ramu SALARY : Rs.15000 INCOME TAX : Rs. 7500 MENU --------1. Default tax percentage calculation 2. Getting tax percentage from the user 3. Exit Enter the option = 2 Enter the name Enter the salary amount : Samuel : Rs. 15000

Enter the tax percent NAME : Samuel SALARY : Rs.15000 INCOME TAX : Rs. 750

: 0.05

MENU --------1. Default tax percentage calculation 2. Getting tax percentage from the user 3. Exit Enter the option = Press any key to continue!!!

Result: Thus a C++ program for calculating income tax using default arguments has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 7 Semester : 3

Exercise : 1b

Branch:

CS

CATEGORIZING EMPLOYEES BASED ON DESIGNATION USING STATIC DATA MEMBERS


Aim: To write a C++ program to categorize employees based on designation using static data members. Algorithm: 1. Start 2. Create a class for employee with two count variables declaring as a static member and employee id, name and designation as other data members. 3. Get the employee id name and designation from the user. 4. If the designation is MANAGER increment first count by 1 and if it is ENGINEER increment second count by 1. 5. Finally 6. Stop. Sample input and output: Enter the number of employees = 3 Enter the employee ID Enter the name Enter the designation Enter the employee ID Enter the name Enter the designation Enter the employee ID Enter the name Enter the designation MENU --------1. Managers List 2. Engineers List : 5001 : Ramu : Manager 5002 : Ramnaresh : Engineer 5003 : Sankar : Manager display the total number of managers, engineers and employees.

3. Total Employees 4. Exit Enter the option = 1 EMPLOYEE ID : 5001 NAME EMPLOYEE ID : 5003 NAME : Ramu

: Sankar = 2

The total number of managers MENU --------1. Managers List 2. Engineers List 3. Total Employees 4. Exit Enter the option = 2 EMPLOYEE ID : 5002 NAME

: Ramnaresh

The total number of engineers = 1 MENU --------1. Managers List 2. Engineers List 3. Total Employees 4. Exit Enter the option = 3 The total number of employees = 3

Result: Thus

C++

program

to

categorize

employees

based

on

designation using static data members has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 1c Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 11

Addition Of Two Private Data Members Using Friend Functions


Aim: To write a C++ program to add two private data members using friend functions. Algorithm: 1. Start. 2. Create two separate classes for reading two numbers. 3. Declare sum function as a friend function. 4. In sum function add the two numbers and print it. 5. Stop. Sample input and output: ADDITION OF TWO NUMBERS -----------------------------------------Enter the first number = 12 Enter the second number = 25 The sum of 12 and 25 is 37

Result: Thus a C++ program for adding two private data members using friend functions has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 2 Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 14

Matrix Vector Multiplication using Friend Functions


Aim: To write a C++ program to implement matrix vector multiplication using friend functions. Algorithm: 1. Start. 2. Create two classes one for reading the input matrices and other for resultant matrix. 3. Declare calculate function as a friend function. 4. Create a menu for Row and Column matrix. 5. For the Row matrix call the corresponding calculate function and find the resultant matrix and print it. 6. For the Column matrix call the corresponding calculate function and find the resultant matrix and print it. 7. Stop. Sample input and output: MATRIX MULTIPLICATION -------------------------------------1. Row Matrix 2. Column Matrix 3. Exit Enter the option = 1 Enter the rows and columns for the 1st matrix = 2 Enter the rows and columns for the 2nd matrix = 2 Enter A matrix -----------------Enter the value of a[0][0] Enter the value of a[0][1] Enter B matrix ----------------Enter the value of Enter the value of Enter the value of Enter the value of

= =

1 2

b[0][0] b[0][1] b[1][0] b[1][1]

= = = =

3 4 5 6

Resultant Matrix -------------------13 16 MATRIX MULTIPLICATION -------------------------------------1. Row Matrix 2. Column Matrix 3. Exit Enter the option = 2 Enter the rows and columns for the 1st matrix = 2 Enter the rows and columns for the 2nd matrix = 2 Enter A matrix -----------------Enter the value of a[0][0] Enter the value of a[0][1] Enter B matrix ----------------Enter the value of Enter the value of Enter the value of Enter the value of

= =

1 2

b[0][0] b[0][1] b[1][0] b[1][1]

= = = =

3 4 5 6

Resultant Matrix -------------------3 4 6 8 MATRIX MULTIPLICATION -------------------------------------1. Row Matrix 2. Column Matrix 3. Exit Enter the option = 3 Press any key to continue!!!

Result: Thus a C++ program to implement matrix vector multiplication using friend functions has been executed successfully.

C/MN/01/28.02.02

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise : 3 Branch: CS Semester : 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 18

Manipulation Of Complex Numbers Using Operator Overloading And Type Conversions


Aim: To write a C++ program to manipulate complex numbers using operator overloading and type conversions. Algorithm: 1. Start. 2. Create a menu for addition and subtraction of complex numbers and type conversion of integer to complex, double to complex and vice versa. 3. By overloading operators + and add and subtract the two complex numbers. 4. For integer to complex, get the integer from the user and represent it in complex form by making the imaginary part zero. 5. For complex to integer, get the complex number from the user and just print the real part. 6. For double to complex and complex to double follow the same procedure but represent the numbers in decimal form. 7. Stop. Sample input and output: MENU -------1. Addition 2. Subtraction 3. Integer to Complex 4. Complex to Integer 5. Double to Complex 6. Complex to double 7. Exit Enter Enter Enter Sum the the the = option = 1 real and imaginary part : real and imaginary part : 22 + i22 7 + i12 15 + i10

MENU -------1. Addition 2. Subtraction 3. Integer to Complex 4. Complex to Integer 5. Double to Complex 6. Complex to double 7. Exit Enter the option = Enter the real and Enter the real and Difference = -8 + 2 imaginary part : imaginary part : i2 7 + i12 15 + i10

MENU -------1. Addition 2. Subtraction 3. Integer to Complex 4. Complex to Integer 5. Double to Complex 6. Complex to double 7. Exit Enter the option = 3 Enter the number to be converted = 10 The number in complex form is = 10 + i0 MENU -------1. Addition 2. Subtraction 3. Integer to Complex 4. Complex to Integer 5. Double to Complex 6. Complex to double 7. Exit Enter the option = 4 Enter the real and imaginary part = 30 The number in integer form is = 30 15

Result: Thus a C++ program to manipulate complex numbers using operator overloading and type conversions has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 4 Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 23

MATRIX MANIPULATION USING DYNAMIC MEMORY ALLOCATION


Aim: To write a C++ program to perform matrix addition and subtraction using dynamic memory allocation. Algorithm: 1. Start 2. Create a class matrix. 3. Allocate the memory dynamically for the two dimensional array. 4. Number of rows and columns are to be initialized to zero using default constructors. 5. Get the rows and the columns from the user and it must be the same for both the matrices. 6. Depending upon the menu add or subtract the two matrices. 7. Print the resultant matrix. 8. Stop. Sample input and output: MATRIX MANIPULATION ----------------------------------Enter the number of rows and columns = 2 Enter A matrix -----------------Enter the value of Enter the value of Enter the value of Enter the value of Enter B matrix -----------------Enter the value of Enter the value of Enter the value of Enter the value of MENU -------1. Addition 2. Subtraction

a[0][0] a[0][1] a[1][0] a[1][1]

= = = =

5 6 8 10

b[0][0] b[0][1] b[1][0] b[1][1]

= = = =

2 7 12 15

3. Exit Enter the option = 1 RESULTANT MATRIX ----------------------------7 13 20 25 MENU -------1. Addition 2. Subtraction 3. Exit Enter the option = 2 RESULTANT MATRIX ----------------------------3 -1 -4 -5 MENU -------1. Addition 2. Subtraction 3. Exit Enter the option = 3 Press any key to continue!!!

Result: Thus a program for

matrix manipulation using dynamic memory

allocation has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 5 Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 27

OVERLOADING NEW AND DELETE OPERATORS


Aim: To write a program to perform calculate student marks by

overloading new and delete operators. Algorithm: 1. Start 2. Get the rollno, name and the marks in each subject from the user. 3. Overload new operator to allocate space for name and marks. 4. In the sum function calculate the marks. 5. Display the student details finally. 6. Finally delete the space allocated for name and marks by overloading delete operator. 7. Stop. Sample input and output: OVERLOADING NEW AND DELETE OPERATORS ------------------------------------------------------------------Enter the Roll no = 36 Enter the Name = Guruprasad Enter the marks one by one 98 96 95 99 93 STUDENT MARK LIST -----------------------------ROLL NO : 36 NAME : Guruprasad TOTAL : 481

Result: Thus a program to calculate student marks by overloading new and delete operators has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 6 Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 29

IMPLEMENTATION OF LINKED LIST USING TEMPLATES


Aim: To write a program to develop a template for linked list class and its methods. Algorithm: 1. Start. 2. Declare a class for data and pointer to next node. 3. Develop a template for the class so that it can hold different datatypes. 4. Create a header node using new function and create other nodes in the same manner. 5. Create a menu for insertion, deletion and display functions. 6. For inserting a node get the position and the number to be inserted from the user. 7. Now insert the node at the correct position. 8. For deleting a node get the number to be deleted from the user. 9. After performing each and every operation display the list to view the change. 10. Stop. Sample input and output: LINKED LIST ---------------Datatypes -----------1. Integer 2. Floating Point 3. Exit Enter the option = 1 CREATION Enter the Enter the Enter the Enter the Enter the number number number number number (Press (Press (Press (Press (Press 999 999 999 999 999 for for for for for the the the the the end) end) end) end) end) = = = = = 10 20 30 40 50

Enter the number (Press 999 for the end) = 60 Enter the number (Press 999 for the end) = 999 LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 1 Enter the position in which the number is to be inserted = 3 Enter the number to be inserted = 25 LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 3 10202530405060END LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 2 Enter the position of the node to be deleted = 7 LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 3 102025304050END LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 4 Datatypes -----------1. Integer 2. Floating Point 3. Exit Enter the option = 2

CREATION Enter the Enter the Enter the Enter the Enter the Enter the Enter the

number number number number number number number

(Press (Press (Press (Press (Press (Press (Press

999 999 999 999 999 999 999

for for for for for for for

the the the the the the the

end) end) end) end) end) end) end)

= = = = = = =

10.56 20.23 30.14 40.18 50.89 60.54 999

LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 1 Enter the position in which the number is to be inserted = 3 Enter the number to be inserted = 25.26 LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 3 10.5620.2325.2630.1440.1850.8960.54END LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 2 Enter the position of the node to be deleted = 7 LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 3 10.5620.2325.2630.1440.1850.89END LINKED LIST MENU -------------------------1. INSERTION 2. DELETION 3. DISPLAY 4. EXIT Enter the option = 4 Datatypes

-----------1. Integer 2. Floating Point 3. Exit Enter the option =3 Press any key to continue!!!!!!!!

Result: Thus a template to implement linked list class and its method has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 7a Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 39

IMPLEMENTATION OF BUBBLE SORT AND INSERTION SORT USING TEMPLATES


Aim: To write a program to implement bubble sort and insertion sort using templates. Algorithm: 1. Start 2. Create a menu or integer and float data types. 3. Create a menu for bubble sort and insertion sort. 4. Create a class with count and array as data members and member functions for both insertion and bubble sort. 5. For bubble sort compare each number with others and do the sorting operation. 6. Similar procedure is followed for insertion sort but the previous list of numbers was checked after each sort. 7. Finally display the list of numbers. 8. Stop Sample input and output: DATATYPES ----------------1. Integer 2. Floating point 3. Exit Enter the option = 1 SORTING OPTIONS -------------------------1. Bubble Sort 2. Insertion Sort 3. Exit Enter the option = 1 Enter no of terms = 5 Enter the numbers 5 8 2 4

The sorted array is 1 2 4 5

SORTING OPTIONS -------------------------1. Bubble Sort 2. Insertion Sort 3. Exit Enter the option = 3 DATATYPES ----------------1. Integer 2. Floating point 3. Exit Enter the option = 2 SORTING OPTIONS -------------------------1. Bubble Sort 2. Insertion Sort 3. Exit Enter the option = 2 Enter no of terms = 6 Enter the numbers 5.62 6.14 5.54 2.17 The sorted array is 2.17 3.55 4.89 5.54

3.55 5.62

4.89 6.14

Result: Thus the program to implement bubble sort and insertion sort using templates has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 7b Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 45

IMPLEMENTATION OF QUIK SORT AND MERGE SORT USING TEMPLATES


Aim: To write a program to implement quick sort and merge sort using templates. Algorithm: 1. Start 2. Get the elements to be sorted form the user. 3. From the given set of elements, a pivot element is selected and based on pivot element two arrays are created, one for elements lesser than pivot and other for elements greater than pivot. 4. These two arrays are sorted and this process of sorting is know as quick sort. 5. The fundamental operation of merge sort is merging two sorted lists. 6. Each element in the first list is compared with first element in the second list followed by next and it continues. 7. The process of sorting the elements in this manner is called merge sort. 8. Stop Sample input and output: DATATYPES ----------------1. Integer 2. Floating point 3. Exit Enter the option = 1 SORTING OPTIONS -------------------------1. Quick Sort 2. Merge Sort 3. Exit Enter the option = 1

Enter no of terms = 5 Enter the numbers 5 8 2 4 The sorted array is 1 2 4 5

SORTING OPTIONS -------------------------1. Quick Sort 2. Merge Sort 3. Exit Enter the option = 3 DATATYPES ----------------1. Integer 2. Floating point 3. Exit Enter the option = 2 SORTING OPTIONS -------------------------1. Quick Sort 2. Merge Sort 3. Exit Enter the option = 2 Enter no of terms = 6 Enter the numbers 5.62 6.14 5.54 2.17 The sorted array is 2.17 3.55 4.89 5.54

3.55 5.62

4.89 6.14

Result: Thus the program to implement quick sort and merge sort using templates has been executed successfully.

LAB MANUAL
Sub Code & Name : CS2209 OBJECT ORIENTED PROGRAMMING LAB Exercise: 8 Branch: CS Semester: 3

LM-CS2209 LM Rev. No:00 Date: 13-07-09 Page 49

IMPLEMENTATION OF STACK AND QUEUE USING EXCEPTION HANDLING


Aim: To write a program to implement quick sort and merge sort using templates. Algorithm: 1. Start 2. Create a class for stack and queue. 3. For stack include push, pop and display operations. 4. For queue include enqueue, dequeue and display opeartions. 5. Write exception for overflow and underflow of stack and queue using try 7. Stop Sample input and output: EXCEPTION HANDLING -------------------------------1. Stack 2. Queue 3. Exit Enter the option = 1 STACK ---------Enter the number of elements = 3 MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 1 Enter the element = 10 MENU block, throw statement and catch block. 6. Perform each and every operation and check for exceptions.

--------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 1 Enter the element = 20 MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 1 Enter the element = 30 MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 1 Stack is Full!!! MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 4 30 20 10 MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 2 The popped element is 30 MENU --------1. Push 2. Pop 3. Top 4. Display 5. Exit Enter the option = 5 EXCEPTION HANDLING --------------------------------

1. Stack 2. Queue 3. Exit Enter the option = 2 QUEUE ---------Enter the number of elements = 3 MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 2 Queue is empty MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 1 Enter the element = 5 MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 1 Enter the element = 10 MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 1 Enter the element = 15 MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 1 Queue is Full!! MENU --------1. Enqueue 2. Dequeue 3. Exit Enter the option = 3 Result: Thus the program to implement stack and queue using exception handling has been executed successfully.

Vous aimerez peut-être aussi