0% found this document useful (0 votes)
454 views26 pages

Sri Venkateswara College of Engineering Pennalur, Sriperumbudur

The document is a lab manual for an Object Oriented Programming lab. It contains instructions for 12 experiments involving concepts like income tax calculation using default arguments, categorizing employees by designation using static members, adding private data members using friend functions, and matrix vector multiplication using friend functions. It provides sample inputs, outputs, and algorithms for each experiment.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
454 views26 pages

Sri Venkateswara College of Engineering Pennalur, Sriperumbudur

The document is a lab manual for an Object Oriented Programming lab. It contains instructions for 12 experiments involving concepts like income tax calculation using default arguments, categorizing employees by designation using static members, adding private data members using friend functions, and matrix vector multiplication using friend functions. It provides sample inputs, outputs, and algorithms for each experiment.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

 

Prepared by Approved by
Signature    

Name S.Jayanthi Dr.Susan Elias


SL / CS HOD/CS
M.S.Roopakala
L/CS

Date 13-07-2009   13-07-2009

Sri Venkateswara College of Engineering


Pennalur, Sriperumbudur.

LAB MANUAL

CS2209 OBJECT ORIENTED PROGRAMMING LAB

YEAR : II Yr B.E CSE

SEMESTER : 3
LM-CS2209
LAB MANUAL
LM Rev. No:00

Sub Code & Name: CS2209 OBJECT ORIENTED Date:13-07-09

PROGRAMMING LAB Page 2

Branch: CS Semester: 3

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

EX NO : 2 Matrix Vector Multiplication

EX NO : 3 Manipulation Of Complex Numbers Using Operator Overloading


And Type Conversions

EX NO : 4 Matrix Manipulation Using Dynamic Memory Allocation

EX NO : 5 Overloading New And Delete Operators

EX NO : 6 Implementation Of Linked List Using Templates

CYCLE II

EX NO : 7 A) Implementation Of Bubble Sort And Insertion Sort Using


Templates
B) Implementation Of Quik Sort And Merge Sort Using
Templates

EX NO : 8 Implementation Of Stack And Queue Using Exception Handling

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


LM-CS2209
LAB MANUAL
LM Rev. No:00

Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09

PROGRAMMING LAB Page 3

Exercise: 1a Branch: CS Semester: 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 : Ramu
Enter the salary amount : 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 : Samuel
Enter the salary amount : Rs. 15000
Enter the tax percent : 0.05

NAME : Samuel
SALARY : Rs.15000
INCOME TAX : Rs. 750

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.
LM-CS2209
LAB MANUAL
LM Rev. No:00

Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09

PROGRAMMING LAB Page 7

Exercise : 1b Branch: CS Semester : 3

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 display the total number of managers, engineers and
employees.
6. Stop.

Sample input and output:

Enter the number of employees = 3

Enter the employee ID : 5001


Enter the name : Ramu
Enter the designation : Manager

Enter the employee ID : 5002


Enter the name : Ramnaresh
Enter the designation : Engineer

Enter the employee ID : 5003


Enter the name : Sankar
Enter the designation : Manager

MENU
---------
1. Manager’s List
2. Engineer’s List
3. Total Employees
4. Exit

Enter the option = 1

EMPLOYEE ID : 5001
NAME : Ramu

EMPLOYEE ID : 5003
NAME : Sankar

The total number of managers = 2

MENU
---------
1. Manager’s List
2. Engineer’s List
3. Total Employees
4. Exit

Enter the option = 2

EMPLOYEE ID : 5002
NAME : Ramnaresh

The total number of engineers = 1

MENU
---------
1. Manager’s List
2. Engineer’s List
3. Total Employees
4. Exit

Enter the option = 3


The total number of employees = 3

Result:
Thus a C++ program to categorize employees based on
designation using static data members has been executed successfully.
LM-CS2209
LAB MANUAL
LM Rev. No:00

Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09

PROGRAMMING LAB Page 11

Exercise: 1c Branch: CS Semester: 3

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.
LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 14

Exercise: 2 Branch: CS Semester: 3

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] = 1
Enter the value of a[0][1] = 2

Enter B matrix
-----------------
Enter the value of b[0][0] = 3
Enter the value of b[0][1] = 4
Enter the value of b[1][0] = 5
Enter the value of b[1][1] = 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] = 1
Enter the value of a[0][1] = 2

Enter B matrix
-----------------
Enter the value of b[0][0] = 3
Enter the value of b[0][1] = 4
Enter the value of b[1][0] = 5
Enter the value of b[1][1] = 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

LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 18

Exercise : 3 Branch: CS Semester : 3

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 the option = 1


Enter the real and imaginary part : 7 + i12
Enter the real and imaginary part : 15 + i10
Sum = 22 + i22
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 = 2


Enter the real and imaginary part : 7 + i12
Enter the real and imaginary part : 15 + i10
Difference = -8 + i2

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 15
The number in integer form is = 30

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

LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 23

Exercise: 4 Branch: CS Semester: 3

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 2

Enter A matrix
------------------
Enter the value of a[0][0] = 5
Enter the value of a[0][1] = 6
Enter the value of a[1][0] = 8
Enter the value of a[1][1] = 10

Enter B matrix
------------------
Enter the value of b[0][0] = 2
Enter the value of b[0][1] = 7
Enter the value of b[1][0] = 12
Enter the value of b[1][1] = 15

MENU
--------
1. Addition
2. Subtraction
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.
LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 27
Exercise: 5 Branch: CS Semester: 3

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.

LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 29
Exercise: 6 Branch: CS Semester: 3

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 number (Press 999 for the end) = 10
Enter the number (Press 999 for the end) = 20
Enter the number (Press 999 for the end) = 30
Enter the number (Press 999 for the end) = 40
Enter the number (Press 999 for the end) = 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


10202530405060END

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2025304050END

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 number (Press 999 for the end) = 10.56
Enter the number (Press 999 for the end) = 20.23
Enter the number (Press 999 for the end) = 30.14
Enter the number (Press 999 for the end) = 40.18
Enter the number (Press 999 for the end) = 50.89
Enter the number (Press 999 for the end) = 60.54
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.26

LINKED LIST MENU


--------------------------
1. INSERTION
2. DELETION
3. DISPLAY
4. EXIT

Enter the option = 3


10.5620.2325.2630.1440.1850.8960.54END

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.5620.2325.2630.1440.1850.89END

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.
LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 39
Exercise: 7a Branch: CS Semester: 3

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 1
The sorted array is
1 2 4 5 8

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 3.55 4.89

The sorted array is


2.17 3.55 4.89 5.54 5.62 6.14

Result:
Thus the program to implement bubble sort and insertion sort
using templates has been executed successfully.
LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 45
Exercise: 7b Branch: CS Semester: 3

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 1

The sorted array is


1 2 4 5 8

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 3.55 4.89

The sorted array is


2.17 3.55 4.89 5.54 5.62 6.14

Result:
Thus the program to implement quick sort and merge sort using
templates has been executed successfully.
LM-CS2209
LAB MANUAL
LM Rev. No:00
Sub Code & Name : CS2209 OBJECT ORIENTED Date: 13-07-09
PROGRAMMING LAB
Page 49
Exercise: 8 Branch: CS Semester: 3

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 block, throw statement and catch block.
6. Perform each and every operation and check for exceptions.
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
---------
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.

You might also like