Lab Oriented Programming and C++
Lab Oriented Programming and C++
Lab Oriented Programming and C++
sruIncrease
oC eht fothe
ezifont
s tnosize
f ehof
t esthe
aerCourse
cnI .1 Name.
.egaP revoC e2.ht nuse
i rethe
daefollowing
h a sa gniwasolaloheader
f eht esin
u the
.2 Cover Page.
130 24
]CGU-DRHM yb ytisrevinU I–yrogeand
taC Graded
sa dedarasG Category–I
dna University by MHRD-UGC]
300 036 – IDUKIARA
KARAIKUDI
K – 630 003
NOITACUDE ECNATSIDDIRECTORATE
FO ETAROTCEOF
RIDDISTANCE EDUCATION
LAB: OBJECT ORIENTED
PROGRAMMING AND C++
II - Semester
itnem sYou
a egaare
p reinstructed
voc eht etatodpupdate
u ot dethe
tcurcover
tsni erpage
a uoYas mentioned below:
.emaN e1.sruIncrease
oC eht fothe
ezifont
s tnosize
f ehof
t esthe
aerCourse
cnI .1 Name.
aP revoC e2.ht nuse
i rethe
daefollowing
h a sa gniwasolaloheader
f eht esin
u the
.2 Cover Page.
ISREVINUALAGAPPA
APPAGALAUNIVERSITY
LAB: OBJECT ORIENTED
rihT eht ni )46.3:APGC( CA[Accredited
AN yb edarGwith
’+A’’A+’
htiwGrade
detidby
ercNAAC
cA[ (CGPA:3.64) in the Third Cycle
]CGU-DRHM yb ytisrevinU I–yrogeand
taC Graded
sa dedarasG Category–I
dna University by MHRD-UGC]
300 036 – IDUKIARA
KARAIKUDI
K – 630 003
TACUDE ECNATSIDDIRECTORATE
FO ETAROTCEOF
RIDDISTANCE EDUCATION PROGRAMMING AND C++
II - Semester
ALAGAPPA UNIVERSITY
[Accredited with ‘A+’ Grade by NAAC (CGPA:3.64) in the Third Cycle
and Graded as Category–I University by MHRD-UGC]
(A State University Established by the Government of Tamil Nadu)
KARAIKUDI – 630 003
All rights reserved. No part of this publication which is material protected by this copyright notice
may be reproduced or transmitted or utilized or stored in any form or by any means now known or
hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording
or by any information storage or retrieval system, without prior written permission from the Alagappa
University, Karaikudi, Tamil Nadu.
Information contained in this book has been published by VIKAS® Publishing House Pvt. Ltd. and has
been obtained by its Authors from sources believed to be reliable and are correct to the best of their
knowledge. However, the Alagappa University, Publisher and its Authors shall in no event be liable for
any errors, omissions or damages arising out of use of this information and specifically disclaim any
implied warranties or merchantability or fitness for any particular use.
Work Order No. AU/DDE/DE1-291/Preparation and Printing of Course Materials/2018 Dated 19.11.2018 Copies - 500
LAB: OBJECT ORIENTED PROGRAMMING AND C++
BLOCK 1: INTRODUCTION
1. Writing simple C++ programs
2. Using if and switch constructs Programs
3. Looping, Arrays, Structure statements: for, while, do-while, Strings and Matrices Programs Problems
10. Generic Programming with Templates: Demonstrating class templates, classtemplate with multiple arguments
11. Function template: Demonstrating function template with multiple arguments.Inheritance of class template.
12. Streams with Files: opening and closing of files, file pointers and theirmanipulators, sequential access to a file, file
input/output with stream class, random access to a file.
Self-Instructional
Material
Lab: Object Oriented
INTRODUCTION Programming and C++
When we can copy .exe file to any other computer which contain window operating
system then it works properly, because the native code of application of operating
system is same.
RECOMMENDED SYSTEM / SOFTWARE REQUIREMENTS
1. Intel based desktop PC of 166MHz or faster processor with at least 64
MB RAM and 100 MB free disk space.
Self-Instructional
Material 1
Lab: Object Oriented 2. Turbo C++ compiler or GCC compilers.
Programming and C++
In this manual we have used Turbo C++. To write C++ code first we need
to open Turbo C++. For every C++ program we need to follow following
steps for writing and executing a program.
NOTES
Write a program code-> save your program (F2)->compile (Alt+F9)-
>Run(Ctrl +F9)
Step 1:Click on Turbo C++ from start menu or double click on Turbo C++ on
desktop.
Self-Instructional
2 Material
Lab: Object Oriented
Programming and C++
NOTES
Step 2:Click on Start Turbo C++. After clicking on Start Turbo C++ button
following screen will appear:
Self-Instructional
Material 3
Lab: Object Oriented
Programming and C++
NOTES
Step 5:Compile program i.e. hello.cpp by pressing Alt+F9 keys or by using menu
option Compile->Compile:
Self-Instructional
4 Material
Step 6:Run program i.e. hello.cpp by pressing Ctrl +F9 keys or by using menu Lab: Object Oriented
Programming and C++
option Run->Run.
NOTES
Output:
1. Write a program that takes two numbers as input and print their sum
and average.
Self-Instructional
Material 5
Lab: Object Oriented
Programming and C++
NOTES
Output:
Output:
Self-Instructional
6 Material
3. Write a C++ program to input your name and display it. Lab: Object Oriented
Programming and C++
NOTES
Output:
Try yourself:
(i) Write a program to calculate volume of cylinder.
Volume of cylinder= PI*r*r*h.
(ii) Write a program to calculate curved surface area of cylinder.
Curved surface area of cylinder= 2*PI*r*h
(iii) Write a program to print ASCII value of digits,uppercase and lowercase
alphabets.
4. Write a program check whether the given number is even or odd.
Self-Instructional
Material 7
Lab: Object Oriented
Programming and C++
NOTES
Output:
Enter a number 2
Number is even
Self-Instructional
8 Material
Lab: Object Oriented
Output: Programming and C++
NOTES
Self-Instructional
Material 9
Lab: Object Oriented Output:
Programming and C++
Try yourself:
Self-Instructional
10 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 11
Lab: Object Oriented Output:
Programming and C++
NOTES
= 27 + 343 + 0
= 370
Self-Instructional
12 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
10. Write a program to print table of any number using do while loop.
Self-Instructional
Material 13
Lab: Object Oriented Output:
Programming and C++
NOTES
Try yourself:
(i) Write a program to reverse a given number.
(ii) Write a program to check whether a number is prime or not.
(iii) Write a program to convert binary number to decimal number.
11. Write a program that takes values in an array and also display them.
Self-Instructional
14 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
Material 15
Lab: Object Oriented 13. Write a program to search an element in the array using binary search.
Programming and C++
Algorithm: Binary Search
Self-Instructional
16 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 17
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
18 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
1. BEGIN
2. FOR I=1 THROUGH N DO
3. FOR J=N THROUGH I+1 DO
4. IF A[J] < A[J-1] THEN
Self-Instructional
Material 19
Lab: Object Oriented SET TEMP =A[J]
Programming and C++
SET A[J] =A[J-1]
SET A[J-1] = TEMP
NOTES 5. END
Self-Instructional
20 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
NOTES
Self-Instructional
22 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
Material 23
Lab: Object Oriented 17. Write a program for merge sort.
Programming and C++
Algorithm:Two-Way Merge Sort
TWO_WAY_MERGE_SORT (LIST, START, FINISH)
NOTES
1. [COMPUTE THE SIZE OF CURRENT SUB-TABLE]
SET SIZE := FINISH - START+1
2. [TEST BASE CONDITION FOR SUB-TBLE OF SIZE ONE]
IF SIZE <=1 THEN
RETURN
3. [ CALCULATE MID POINT POSITION OF CURRENT SUB-
TABLE]
SET MID :=START + SIZE /2 -1
4. [RECURSIVELY SORT THE FIRST SUB-TABLE]
CALL TWO_WAY_MERGE_SORT (LIST, START, MID)
5. [RECURSIVELY SORT THE SECOND SUB-TABLE]
CALL TWO_WAY_MERGE_SORT (LIST, MID + 1 , FINISH)
6. CALL SIMPLE_MERGE (LIST, START, MID+1, FINISH)
7. RETURN
Self-Instructional
24 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 25
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
26 Material
Try Yourself: Lab: Object Oriented
Programming and C++
(i) Write a program to sort n numbers in descending order using bubble sort.
(ii) Write a program to implement selection sort method using functions.
NOTES
(iii) Write a program to sort the n names in an alphabetical order.
18. Write a Program that takes string as input and print it.
Output:
19. Write a program to print the length of a given string without using
string function.
Self-Instructional
Material 27
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
28 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Try yourself:
Self-Instructional
Material 29
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
30 Material
22. Write a program to find out the product of two matrices. Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 31
Lab: Object Oriented
Programming and C++
NOTES
Output:
Try yourself:
(i) Write a program to print sum of diagonalvalues of a square Matrix.
(ii) Write a program to find highest and lowest element of a Matrix.
(iii) Write a program to convert first letter of each word of a string to uppercase
and other to lowercase.
(iv) Write a program to find substring in string (Pattern Matching).
Self-Instructional
32 Material
23. Write a program to print factorial of a given number using user Lab: Object Oriented
Programming and C++
defined function.
NOTES
Output:
24. Write a program to check a year is leap year or not using function.
Self-Instructional
Material 33
Lab: Object Oriented
Programming and C++
NOTES
Output:
25. Write a program to print array elements using user defined function.
Self-Instructional
34 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
26. Write a program for sequential search using user defined function.
Algorithm:Sequential Search
INPUT : LIST OF SIZE N, TARGET VALUE T
OUTPUT : POSITION OF T IN THE LIST
1. BEGIN
2. SET FOUND: = FALSE
SET I: = 0
3. WHILE Id”N AND FOUND IS FALSE
IF LIST [I] = T THEN
SET FOUND: = TRUE
EXIT
ELSE
SET I: =I+1
[END OF STEP 3 LOOP]
4. IF FOUND = FALSE THEN
Self-Instructional
Material 35
Lab: Object Oriented WRITE: T IS NOT IN LIST
Programming and C++
ELSE
WRITE: T IS FOUND AT I LOCATION
NOTES [END OF IF]
5 . END
Self-Instructional
36 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
Material 37
Lab: Object Oriented 28. Write a program to print Fibonacci series using recursive function.
Programming and C++
NOTES
Output:
Self-Instructional
38 Material
Inline function Lab: Object Oriented
Programming and C++
1. We must keep inline functions small, small inline functions have better
efficiency.
2. Inline functions do increase efficiency, but we should not make all the functions NOTES
inline. Because if we make large functions inline, it may lead to code bloat,
and might affect the speed too.
3. Hence, it is adviced to define large functions outside the class definition
using scope resolution ::operator, because if we define such functions inside
class definition, then they become inline automatically.
4. Inline functions are kept in the Symbol Table by the compiler, and all the call
for such functions is taken care at compile time.
29. Write a program to demonstrate the concept of inline function.
Output:
Try yourself:
Self-Instructional
40 Material
Lab: Object Oriented
Programming and C++
Output:
NOTES
Output:
Self-Instructional
Material 41
Lab: Object Oriented 32. Write a program using static variable and static function.
Programming and C++
NOTES
Self-Instructional
42 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
Material 43
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
44 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 45
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
46 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
36. Write a program to add two matrices. Create two objects of the class
and each of which refers one 2D matrix. Use constructor to allocate memory
dynamically and use copy constructor to allocate memory when one array
object is used to initialize another.
Self-Instructional
Material 47
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
48 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 49
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
50 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Output:
Self-Instructional
Material 51
Lab: Object Oriented Try yourself:
Programming and C++
(i) Write a program to swap two numbers using class.
(ii) Write a program to print numbers from 1 to N using class.
NOTES (iii) Write a program to calculate area of a circle, a rectangle or a triangle
depending on input using overloaded calculate function.
39. Write a program to illustrate the concept of function overloading on
sum function.
Output:
Self-Instructional
52 Material
40. Write a program to overload ++ operator. Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
Material 53
Lab: Object Oriented 41. Write a program to demonstrate the overloading of + operators.
Programming and C++
NOTES
Self-Instructional
54 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 55
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
56 Material
43. Write a program to get and print student data using single inheritance. Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 57
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
58 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 59
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
60 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Try Yourself:
(i) Write a program to demonstrate the multilevel inheritance.
(ii) Write a program to demonstrate the multiple inheritance.
(iii) Write a program to demonstrate the virtual derivation of a class.
46. Write a Program to print array value using Pointers
Self-Instructional
Material 61
Lab: Object Oriented
Programming and C++
NOTES
Output:
Enter 5 numbers: 1
2
3
4
5
Array values using pointer
1
2
3
4
5
47. Write a program to print the sum of two number using pointer.
Self-Instructional
62 Material
Output: Lab: Object Oriented
Programming and C++
Self-Instructional
Material 63
Lab: Object Oriented
Programming and C++
NOTES
Output:
Self-Instructional
64 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
NOTES
Self-Instructional
66 Material
Lab: Object Oriented
Programming and C++
NOTES
Output:
Try yourself:
(i) Write a program that overloads the + operator and relational operators
(suitable) to perform the following operations:
a) Concatenation of two strings.
b) Comparison of two strings.
(ii) Write a programs functions to find the GCD of two given integers using
pointer.
51. Write a program to demonstrate the use of function template.
Self-Instructional
Material 67
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
68 Material
Lab: Object Oriented
Programming and C++
Output:
NOTES
Self-Instructional
Material 69
Lab: Object Oriented
Programming and C++
NOTES
Output:
Try yourself:
i. Write a program of insertion sort using templates.
ii. Write a program of quick sort using templates.
iii. Write a program to implement merge sort using templates.
54. Write a C++ program to create file (data.txt).
Self-Instructional
70 Material
55. Write a program to create and write on a text file. Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 71
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
72 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 73
Lab: Object Oriented Try yourself:
Programming and C++
(i) What task does the following program perform?
#include<iostream.h>#include<fstream.h> int main() { ofstream ofile;
NOTES ofile.open (“text.txt”); ofile << “geeksforgeeks” << endl; cout <<
“Data written to file” << endl; ofile.close(); }
(ii) Write a program which copies one file to another.
(iii) Write aprogram to that counts the characters, lines and words in the text
file.
58. Write a program to demonstrate the concept of exception handling.
Output:
Self-Instructional
74 Material
59. Write a program to handle division by zero error using exception Lab: Object Oriented
Programming and C++
handling.
NOTES
Self-Instructional
Material 75
Lab: Object Oriented Output:
Programming and C++
NOTES
Self-Instructional
76 Material
Output: Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 77
Lab: Object Oriented
Programming and C++
NOTES
Output:
62. Write a C++ program to control the run-time error using try () ‚catch
() and throw ().
Self-Instructional
78 Material
Lab: Object Oriented
Programming and C++
NOTES
Self-Instructional
Material 79
Lab: Object Oriented
Programming and C++
Output:
NOTES
Self-Instructional
80 Material
.emaN e1.sruIncrease
oC eht fothe
ezifont
s tnosize
f ehof
t esthe
aerCourse
cnI .1 Name.
.egaP revoC e2.ht nuse
i rethe
daefollowing
h a sa gniwasolaloheader
f eht esin
u the
.2 Cover Page.
130 24
]CGU-DRHM yb ytisrevinU I–yrogeand
taC Graded
sa dedarasG Category–I
dna University by MHRD-UGC]
300 036 – IDUKIARA
KARAIKUDI
K – 630 003
NOITACUDE ECNATSIDDIRECTORATE
FO ETAROTCEOF
RIDDISTANCE EDUCATION
LAB: OBJECT ORIENTED
PROGRAMMING AND C++
II - Semester
itnem sYou
a egaare
p reinstructed
voc eht etatodpupdate
u ot dethe
tcurcover
tsni erpage
a uoYas mentioned below:
.emaN e1.sruIncrease
oC eht fothe
ezifont
s tnosize
f ehof
t esthe
aerCourse
cnI .1 Name.
aP revoC e2.ht nuse
i rethe
daefollowing
h a sa gniwasolaloheader
f eht esin
u the
.2 Cover Page.
ISREVINUALAGAPPA
APPAGALAUNIVERSITY
LAB: OBJECT ORIENTED
rihT eht ni )46.3:APGC( CA[Accredited
AN yb edarGwith
’+A’’A+’
htiwGrade
detidby
ercNAAC
cA[ (CGPA:3.64) in the Third Cycle
]CGU-DRHM yb ytisrevinU I–yrogeand
taC Graded
sa dedarasG Category–I
dna University by MHRD-UGC]
300 036 – IDUKIARA
KARAIKUDI
K – 630 003
TACUDE ECNATSIDDIRECTORATE
FO ETAROTCEOF
RIDDISTANCE EDUCATION PROGRAMMING AND C++
II - Semester