Lab Oriented Programming and C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

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

B.Sc. [Computer Science] YTISREVINUALAGAPPA


APPAGALAUNIVERSITY
elcyC drihT eht ni )46.3:APGC( CA[Accredited
AN yb edarGwith
’+A’’A+’
htiwGrade
detidby
ercNAAC
cA[ (CGPA:3.64) in the Third Cycle

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

B.Sc. [Computer Science]


130 24

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

B.Sc. [Computer Science]


II - Semester
130 24

LAB: OBJECT ORIENTED


PROGRAMMING AND C++
Author:
Dr. Kavita Saini, Associate Professor, Galgotias University, Greater Noida

"The copyright shall be vested with Alagappa University"

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.

Vikas® is the registered trademark of Vikas® Publishing House Pvt. Ltd.


VIKAS® PUBLISHING HOUSE PVT. LTD.
E-28, Sector-8, Noida - 201301 (UP)
Phone: 0120-4078900  Fax: 0120-4078999
Regd. Office: 7361, Ravindra Mansion, Ram Nagar, New Delhi 110 055
 Website: www.vikaspublishing.com  Email: [email protected]

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

BLOCK 2: OOPS CONCEPT


4. Functions: static function, friend function, constructor, destructor and operator overloading and Recursive programs
5. Inheritance: Inheritance types
6. Polymorphism: Polymorphism types, Virtual function

BLOCK 3: FILE AND POINTERS


7. File: File Handling C++ Programs, opening and closing a data file - creating adata file, processing a data file.
8. Pointers: Pointers and Pointers with Arrays Programs
9. Virtual functions: Pure virtual functions

BLOCK 4: TEMPLATES AND FILES

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.

BLOCK 5: EXCEPTION HANDLING


13. Exception Handling: programs using exception handling, Exceptions inconstructors and destructors
14. Other Exception Handling methods: Handling uncaught exceptions, exceptions in operator overloaded functions,
exception in inheritance tree, exceptions in class templates, memory allocation failure exception.
INTRODUCTION

Object Oriented Programming (OOP) is a programming paradigm that uses 'objects'


NOTES - data structures consisting of data fields and methods together with their interactions
- to design applications and computer programs. Programming techniques may
include features, such as data abstraction, encapsulation, modularity, polymorphism,
and inheritance. It was not commonly used in mainstream software application
development until the early 1990s. Many modern programming languages now
support OOP. C++ is an object oriented programming language created by Bjarne
Stroustrup. This high level language maintains almost all aspects of the C language
while simplifying memory management and adding several features including a
new data type known as 'class' to allow object oriented programming.
Lab: Object Oriented Programming and C++ is intended for the Second
semester students of B.Sc in Computer Science. This manual typically contains
practical/lab Sessions related OOP covering various aspects related the subject
to enhanced understanding. The main objective of the manual is to understand and
solve logical and mathematical problems through C++ language. Students must go
through this manual rather than only topics mentioned in the syllabus, as practical
aspects are the key to understanding and conceptual visualization of theoretical
aspects covered in the books.

Self-Instructional
Material
Lab: Object Oriented
INTRODUCTION Programming and C++

C++ language is invented by Bjarne Stroustrup in 1980 at Bell Laboratories,


New Jersey. C++ language was initially called “C with Classes” but in 1983 this NOTES
name was changed to C++. C++ is a superset of C.
The purpose of C++ is to overcome this limit and provide a better way to
manage larger, more complex programs, by using object oriented programming
(OOP). C++ is very popular language as it has many features as mentioned below:
 Classes and objects
 Encapsulation
 Information hiding
 Inheritance
 Polymorphism
Portable Language
It is the concept of carrying the instruction from one system to another system. In
C++ language .cpp file contain source code, we can also edit this code and .exe file
contain application, only we can execute this file. When we write and compile any
C++ program on window operating system then that program easily run on other
window based system.

Fig. 1.1Representing the C++ object file running on Windows.

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.

After clicking on Turbo C++ following screen will appear:

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:

This is the editor where we will write code of C++ programs.


Step 3:Write a program to print “Hello” on screen (Hello.cpp).

Self-Instructional
Material 3
Lab: Object Oriented
Programming and C++

NOTES

Step 4:Save program by name hello.cpp by pressing F2 key or by using menu


option File->Save As.

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:

2. Write a program to swap two numbers without using a third variable.

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

5. Write a program to print the largest number among three numbers


given by the user.

Self-Instructional
8 Material
Lab: Object Oriented
Output: Programming and C++

NOTES

6. Write a program to print sum, difference, product and division of two


numbers according to the user choice using Switch case.

Self-Instructional
Material 9
Lab: Object Oriented Output:
Programming and C++

Enter two numbers: 56


9
NOTES Enter operator : *
Multiplication of two numbers 504

Try yourself:

(i) Write a program to convert a lowercase alphabet to uppercase and vice-


versa.
(ii) Write a program to check whether a year is leap year or not.
(iii) Write aprogram to check whether a given character is uppercase or
lowercase alphabet or a digit or a special character.
7. Write a program to print table of any number using for loop.

Self-Instructional
10 Material
Output: Lab: Object Oriented
Programming and C++

NOTES

8. Write a program to print Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13, 21...).

Self-Instructional
Material 11
Lab: Object Oriented Output:
Programming and C++

NOTES

9. Write a program to check whether a given number is Armstrong.


A number is known as Armstrongnumber if sum of the cubes of its digits is equal
to the number itself.
For example:

370 is an Armstrong number because:

370 = 3*3*3 + 7*7*7 + 0*0*0

= 27 + 343 + 0

= 370

Self-Instructional
12 Material
Lab: Object Oriented
Programming and C++

NOTES

Output:

Enter a number: 370


Number is Armstrong

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

12. Write a program to print the largest value in an array.

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

INPUT : SORTED LIST OF SIZE N, KEY


NOTES VALUE KEY
OUTPUT : POSITION OF KEY IN THE LIST =
KEY
1. BEGIN
2. [INTIALIZE ]
SET MAX := SIZE
SET MIN := 1
SET FOUND := FALSE
3.WHILE (FOUND IS FALSE AND MAX e”MIN )
SET MID := ( MAX + MIN )/2
4. IF KEY = LIST [MID] THEN
SET I : = MID
SET FOUND := TRUE
EXIT
ELSE IF KEY < LIST [MID] THEN
SET MAX := MID -1
ELSE
SET MIN := MID +1
[END OF IF]
[END OF LOOP]
5. IF FOUND = FALSE THEN
WRITE : VALUE IS NOT IN LIST
ELSE
WRITE VALUE FOUND AT MID LOCATION
6. END

Self-Instructional
16 Material
Lab: Object Oriented
Programming and C++

NOTES

Self-Instructional
Material 17
Lab: Object Oriented Output:
Programming and C++

NOTES

14. Write a program to sort an array using selection sort.

Self-Instructional
18 Material
Lab: Object Oriented
Programming and C++

NOTES

Output:

15. Write a program for bubble sort.


Algorithm: Bubble Sort
INPUT - LIST [ ] OF N ITEMS
OUTPUT - LIST [ ] OF N ITEMS IN SORTED ORDER

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

16. Write a program for quick sort.


Algorithm:Quick Sort
QUICK_SORT (ARRAY, FIRST, LAST)
1. SET LOW:= FIRST
SET HIGH:= LAST
SET PIVOT: =ARRAY [( LOW + HIGH) /2]
2. REPEAT THROUGH STEP 7 WHILE (LOW d”HIGH)
3. REPEAT STEP 4 WHILE (ARRAY [LOW] <PIVOT)
4. SET LOW: = LOW+1
5. REPEAT STEP 6 WHILE (ARRAY [HIGH]>PIVOT)
6. SET HIGH: = HIGH-1
7. IF (LOW <=HIGH)
ARRAY [LOW] <->ARRAY [HIGH]
SET LOW: = LOW+1
SET HIGH:= HIGH-1
8. IF (FIORST<HIGH) THEN
QUICK_SORT (ARRAY, FIRST, HIGH)
9. IF (LOW < LAST)
QUICK_SORT (ARRAY, LOW, LAST)
10. END
Self-Instructional
Material 21
Lab: Object Oriented
Programming and C++

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:

Enter your name: Rajan


Welcome Rajan

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:

20. Write a program to check whether a given string is palindrome or not.

Self-Instructional
28 Material
Lab: Object Oriented
Programming and C++

NOTES

Output:

Try yourself:

(i) Write a program to insert an element in an array.


(ii) Write a program to find sum of elements of an array.
(iii) Write a program to find largest number from an array.
21. Write a program to print sum of two matrices.

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

27. Write a program to print factorial of a number using recursive


function.

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:

(i)Write a C++ program to find average marks of three subjects of N students in


a class.
Self-Instructional
Material 39
Lab: Object Oriented (ii) Write a C++ program to take input of two distances in inch-feet system
Programming and C++
and stores in data members of two structure variables. Also, this program
calculates the sum of two distances and displays it.
(iii) Write a C++ program in which user is asked to enter two time periods and
NOTES
these two periods are stored in structure variables. The program calculates
the difference between these two time periods.
(iv) Write a C++ Program to find total salary of N employees in a department
where DA=35% basic_salary and HRA=10% of basic_salary.
30. Write a program to demonstrate the concept of class and object.

Self-Instructional
40 Material
Lab: Object Oriented
Programming and C++

Output:
NOTES

31. Write a program that shows the use of this pointer.

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:

33. Write a program using static class and variable.

Self-Instructional
Material 43
Lab: Object Oriented
Programming and C++

NOTES

Output:

34. Write a C++ program using constructor in a class.

Self-Instructional
44 Material
Lab: Object Oriented
Programming and C++

NOTES

Self-Instructional
Material 45
Lab: Object Oriented Output:
Programming and C++

NOTES

35. Write a program to demonstrate the use of constructor and destructor.

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

37. Write a program to demonstrate the use of dynamic constructor.

Self-Instructional
50 Material
Lab: Object Oriented
Programming and C++

NOTES

Output:

38. Write a program using static variable.


//C++ program for static variable

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

42. Write a program to demonstrate the overloading of binary arithmetic


operators (+,-,* and /).

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:

44. Write a program to demonstrate the concept of multiple inheritance.

Self-Instructional
58 Material
Lab: Object Oriented
Programming and C++

NOTES

Self-Instructional
Material 59
Lab: Object Oriented Output:
Programming and C++

NOTES

45. Write a program to demonstrate the concept of multilevel inheritance.

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++

Enter two number : 23


25
Sum of the two number is 48 NOTES
48 Write a program to demonstrate function overriding.

Self-Instructional
Material 63
Lab: Object Oriented
Programming and C++

NOTES

Output:

49. Write a program using virtual function.

Self-Instructional
64 Material
Lab: Object Oriented
Programming and C++

NOTES

Output:

Pure Virtual Function


A virtual function will become pure virtual function when you append “=0” at the
end of declaration of virtual function. Pure virtual function doesn’t have body or
implementation. We must implement all pure virtual functions in derived class.
Pure virtual function is also known as abstract function.
Self-Instructional
Material 65
Lab: Object Oriented 50. Write a program using pure virtual function/ abstract function.
Programming and C++

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

52. Write a program to implement Bubble Sort using templates in C++.

Self-Instructional
68 Material
Lab: Object Oriented
Programming and C++
Output:

NOTES

53. Write a program for creating template array class.

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

56. Write a program to retrieve data from a text file.

Self-Instructional
Material 71
Lab: Object Oriented
Programming and C++

NOTES

57. Write a program to read and write on a binary file.

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

60. Write a program to demonstrate exception handling in constructor


and destructor.

Self-Instructional
76 Material
Output: Lab: Object Oriented
Programming and C++

NOTES

61. Write a C++ program to demonstrate exception handling in


constructor and destructor.

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.

B.Sc. [Computer Science] YTISREVINUALAGAPPA


APPAGALAUNIVERSITY
elcyC drihT eht ni )46.3:APGC( CA[Accredited
AN yb edarGwith
’+A’’A+’
htiwGrade
detidby
ercNAAC
cA[ (CGPA:3.64) in the Third Cycle

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

B.Sc. [Computer Science]


130 24

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

You might also like