0% found this document useful (0 votes)
9 views

Unit 4 Notes

Uploaded by

Anuj Samdariya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit 4 Notes

Uploaded by

Anuj Samdariya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Object Oriented

Programming with C++

Templates
Exceptions &
STL
Templates, Exceptions and STL Weightage: 15%

 What is template?
 Function templates and class templates
 Introduction to exception
 Try-catch, throw
I like C++ so much
 Multiple catch


Catch all
I like
Rethrowing exception
Rupesh sir
 Implementing user defined exceptions
 Overview and use of Standard Template Library(STL)

Templates, Exceptions and STL 2


Exception

1/0
Introduction to Exception
int main()
{
int a,b,c;
cout<<"Enter value a=";
cin>>a;
I like C++ so much
cout<<"Enter value b=";
cin>>b;
c=a/b;
cout<<"answer="<<c;
I like Rupesh sir
}
Output: Output:
Enter value a=5 Enter value a=5
Enter value b=2 Enter value b=0
answer=2 Abnormal Termination occur
Templates, Exceptions and STL 4
Introduction to Exception(Cont…)

 Runtime errors are termed as exception.


I like C++ so much
 Exception handling is the process to manage the runtime errors
by converting the abnormal termination of a program to normal
I like Rupesh sir
termination of a program.

Templates, Exceptions and STL 5


try, throw and catch

try

I like C++ so much


I like Rupesh sir
throw catch

Templates, Exceptions and STL 6


try, throw and catch
 C++ exception handling mechanism is built upon three keywords
try, throw and catch.
try
try block {
.....
I like C++ so much
Detects and throws an
exception
throw exception; //this block
detects and throws an exception
}
Exception
Object
I like Rupesh sir
catch(type arg)
catch block {
.....
Catches and handles ... //exception handling block
the exception }
.....

Templates, Exceptions and STL 7


int main()
{
try, throw and catch example
int a,b,c;
cout<<"Enter two values=";
cin>>a>>b;
try Output:
{ Enter value a=5
if(b!=0) Enter value b=0
c=a/b; Exception caught: Divide by zero
cout<<"answer="<<c;
else
throw(b);
}
catch(int x)
{
cout<<"Exception caught: Divide by zero\n";
}
}
void test(int x){
try Multiple catch example
{
if(x==1) int main()
throw x; {
else if(x==0) test(1);
throw 'x'; test(0);
else if(x==-1) test(-1);
throw 5.14; }
}
catch(int i){
cout<<"\nCaught an integer";
}
catch(char ch){
cout<<"\nCaught a character"; Output:
} Caught an integer
catch(double i){ Caught a character
cout<<"\nCaught a double"; Caught a double
}
}
Catch all Exception
Catch all exception
 In some situations, we may not predict all possible types of
exceptions and therefore may not be able to design independent
catch handlers to catch them.

Syntax:
catch(...) I like C++ so much
{
I like Rupesh sir
//statements for processing all exceptions
}

Templates, Exceptions and STL 11


Catch all exception example
#include<iostream> int main()
using namespace std; {
void test(int x) test(-1);
{ test(0);
try test(1);
{ }
I like C++ so much
if(x==0) throw x;
if(x==-1) throw 'a';

}
I like Rupesh sir
if(x==1) throw 5.15;

catch(...)
{ Output:
cout<<"Caught an exception\n"; Caught an exception
} Caught an exception
} Caught an exception

Templates, Exceptions and STL 12


Re-Throwing exception

 An exception is thrown from the catch block is known as the re-


throwing exception. I like C++ so much
 It can be simply invoked by throw without arguments.
I like Rupesh sir
 Rethrown exception will be caught by newly defined catch
statement.

Templates, Exceptions and STL 13


void divide(double x, double y){
try
{
int main()
if(y==0)
{
throw y;
try
else
{
cout<<"Division="<<x/y;
divide(10.5,2.0);
}
divide(20.0,0.0);
catch(double)
}
{
catch(double)
cout<<"Exception inside
{
function\n";
cout<<"Exception inside
throw;
main function";
}
}
}
}
Output:
Division=5.25
Exception inside function
Exception inside main function
Exceptions thrown from
functions
#include <iostream>
using namespace std;
void test(int x)
{
cout<<"Inside function:"<<x<<endl;
if(x) throw x;
}
int main()
{
cout<<"Start"<<endl;
try
{
test(0);
test(1);
test(2);
}
catch(int x)
{
cout<<"Caught an int exception:"<< x<<endl;
}

}
User defined Exception

 There maybe situations where you want to generate some user


I like C++ so much
specific exceptions which are not pre-defined in C++.
 In such cases C++ provided the mechanism to create our own
exceptions byI inheriting
like Rupesh sir in C++.
the exception class

Templates, Exceptions and STL 17


#include <iostream> User defined Exception
#include <exception>
class myexception: public exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;
int main (){
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << '\n';
}
}
User defined Exception(Cont…)
double myfunction (char arg) throw (int);
 This declares a function called myfunction, which takes one
argument of type char and returns a value of type double.
 If this function throws an exception of some type other than int,
the function calls std::unexpected instead of looking for a handler
I like C++ so much
or calling std::terminate.
 If this throw specifier is left empty with no type, this means that
I like Rupesh sir
std::unexpected is called for any exception.
 Functions with no throw specifier (regular functions) never call
std::unexpected, but follow the normal path of looking for their
exception handler.
int myfunction (int param) throw(); //all exceptions call unexpected
int myfunction (int param); //normal exception handling

Templates, Exceptions and STL 19


Template
Need of Templates
int add(int x, int y) float add(float x, float y)
{ {
return x+y; return x+y;
} }

char add(char x,char y) double add(double x, double y)


{ I like C++ so much
{
return x+y; return x+y;
} I like Rupesh sir
}

We need a single function that will work for int, float, double etc…

Templates, Exceptions and STL 21


Templates
 Templates concept enables us to define generic classes and
functions.
 This allows a function or class to work on many different data
types without being rewritten for each one.
I like C++ so much
I like Rupesh
Templatessir

Function Class
Template Template
Templates, Exceptions and STL 22
Function Template
Syntax:
template<class Ttype>

Keyword Placeholder name


I likeSpecifies
C++generic
so much
for a datatype

I like Rupesh sir


type in a Template

template<typename Ttype>

Templates, Exceptions and STL 23


Templates
 C++ templates are a powerful mechanism for code reuse, as they
enable the programmer to write code that behaves the same for
any data type.
 By template we can define generic classes and functions.

I like C++ so much
In simple terms, you can create a single function or a class to work
with differentI data
liketypesRupesh sir
using templates.
 It can be considered as a kind of macro. When an object of a
specific type is defined for actual use, the template definition for
that class is substituted with the required data type.

Templates, Exceptions and STL 24


Function Template
 Suppose you write a function printData:
void printData(int value){
cout<<"The value is "<<value;
}
 Now if you want to print double values or string values, then you
I like C++ so
have to overload the function:
void printData(float value){
much
}
I like
cout<<"The Rupesh
value sir
is "<<value;

void printData(char *value) {


cout<<"The value is "<<*value;
}
 To perform same operation with different data type, we have to
write same code multiple time.

Templates, Exceptions and STL 25


Function Template (Cont…)
 C++ provides templates to reduce this type of duplication of code.
template<typename T>
void printData(T value){
cout<<"The value is "<<value;
}

I like C++ so much


 We can now use printData for any data type. Here T is a
template parameter that identifies a type.
I like Rupesh sir
 Then, anywhere in the function where T appears, it is replaced
with whatever type the function is instantiated.
int i=3;
float d=4.75;
char *s="hello";
printData(i); // T is int
printData(d); // T is float
printData(s); // T is string
Templates, Exceptions and STL 26
#include <iostream>  T is a template argument that
using namespace std;
accepts different data types
template <typename T>
 typename is a keyword
T Large(T n1, T n2)
 You can also use
{
return (n1 > n2) ? n1 : n2; keyword class instead of
} typename
int main(){
int i1, i2; float f1, f2; char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
}
Class Template
 Sometimes, you need a class implementation that is same for all
classes, only the data types used are different.
 Normally, you would need to create a different class for each data
type OR create different member variables and functions within a
single class.
I like C++ so much
I like Rupesh sir

Templates, Exceptions and STL 28


Class Template
Syntax:
template<class Ttype>

Keyword Placeholder name


I likeSpecifies
C++generic
so much
for a datatype

I like Rupesh sir


type in a Template

Templates, Exceptions and STL 29


Object of template class
The object of template class are created as follows
class name <data type> object name;

template<class Ttype> int main()


class sample
{ I like C++ so much
{
sample <int>s1;
Ttype a,b;
public: I like Rupesh sirsample <float>s2;
s1.getdata();
void getdata() s1.sum();
{
s2.getdata();
cin>>a>>b;
s2.sum();
}
}
void sum();
};

Templates, Exceptions and STL 30


template<class T1, class T2>
class Sample Class Template
{
T1 a; T2 b; Example
public:
Sample(T1 x,T2 y){
a=x;
b=y;
}
void disp(){
cout<<"\na="<<a<<"\tb="<<b;
}  To create a class template
}; object,
int main(){ define the data type
Sample <int,float> S1(12,23.3); inside a < > at the time of
Sample <char,int> S2('N',12); object creation.
S1.disp();  className<int> classObj;
S2.disp(); className<float> classObj;
}
GTU Programs
1. Write a function template for finding the minimum value
contained in an array.
2. Create a generic class stack using template and implement
common Push and Pop operations for different data types.
I like C++ so much
3. Write program to swap Number using Function Template.

I like Rupesh sir

Templates, Exceptions and STL 32


STL – Standard Template Library

Stack Queue Vector

map
STL- Standard Template Library
 The C++ STL (Standard Template Library) is a powerful set of C++
template classes to provides general-purpose templatized classes
and functions that implement many popular and commonly used
algorithms and data structures like vectors, lists, queues, and
stacks.

I like C++ so much
There are three core components of STL as follows:
I like
1. Containers Rupesh
(an object to store data)sir
2. Algorithms (procedure to process data)
3. Iterators (pointer object to point elements in container)

Templates, Exceptions and STL 34


STL- Containers
 A container is an object the actually stores data.
 The STL containers can be implemented by class templates to hold
different data types.

I like C++ so much


Containers

SequenceI like Rupesh


Associative sir Derived
• vector • set • stack
• deque • multiset • queue
• list • map • Priority-
• multimap queue

Store elements in Direct access to Derived from sequence


linear sequence elements using keys containers

Templates, Exceptions and STL 35


STL Algorithms
 It is a procedure that is used to process data contained in
containers.
 It includes algorithms that are used for initializing, searching,
copying, sorting and merging.
I like C++ so much
 Mutating Sequence Algorithms
like copy(), remove(), replace(), fill(), swap(), etc.,
I like Rupesh sir
 Non Modifying sequence Algorithms
like find(), count(),search(), mismatch(), and equal()
 Numerical Algorithms
accumulate(), partial_sum(), inner_product(), and
adjacent_difference()

Templates, Exceptions and STL 36


STL- Algorithms
 STL provide number of algorithms that can be used of any
container, irrespective of their type. Algorithms library contains
built in functions that performs complex algorithms on the data
structures.

I like C++ so much


 For example: one can reverse a range with reverse() function, sort
a range with sort() function, search in a range with binary_search()
and so on. I like Rupesh sir
 Algorithm library provides abstraction, i.e you don't necessarily
need to know how the the algorithm works.

Templates, Exceptions and STL 37


STL- Iterations
 Iterators behave like pointers.
 Iterators are used to access container elements.
 They are used to traverse from one element to another.

I like C++ so much


I like Rupesh sir

Templates, Exceptions and STL 38


STL components
 STL provides numerous containers and algorithms which are very
useful in completive programming , for example you can very
easily define a linked list in a single statement by using list
container of container library in STL , saving your time and effort.

I like C++ so much


 STL is a generic library , i.e a same container or algorithm can be
operated on any data types , you don’t have to define the same
I like Rupesh sir
algorithm for different type of elements.
 For example , sort algorithm will sort the elements in the given
range irrespective of their data type , we don’t have to implement
different sort algorithm for different datatypes.

Templates, Exceptions and STL 39


Thank You
Object Oriented
Programming with C++

IO & File
Management
I/O and File Management Weightage: 15%

 Concept of streams
 cin and cout objects
 C++ stream classes
 Unformatted and formatted I/O
 Manipulators I like C++ so much


File stream
I like
C++ File stream classes
Rupesh sir
 File management functions
 File modes
 Binary and random Files

I/O and File Management 2


Concepts of Streams
Concept of Streams

Keyboard Display / Printer

File I like
Input C++ so much
Stream
Output
Stream
C++ Program
File

I like Rupesh sir


Program Program

Input source Output target


to stream from stream

I/O and File Management 4


Concept of streams(Cont…)
 A stream is a general name given to a flow of data.
 A stream is a sequence of bytes.
 The source stream that provides data to programs is called input
stream.
I like C++ so much
 The destination stream receives output from the program is called
output stream.
I like Rupesh sir
 In header <iostream>, a set of class is defined that supports I/O
operations.
 The classes used for input/output to the devices are declared in
the IOSTREAM file.
 The classes used for disk file are declared in the FSTREAM file.
I/O and File Management 5
Input/Output streams

Input stream
Input
device

I like C++ so muchProgram


Output stream
Idevicelike Rupesh sir
Output

I/O and File Management 6


Stream class for console I/O operations
General input/output
input stream ios stream class
class
pointer
output stream class
istream streambuf ostream
input output

I likeiostream
C++ so much input/output stream class

I like Rupesh sir


istream_withassign Iostream_withassign ostream_withassign

 istream_withassign, ostream_withassign and iostream_withassign


 ios class contains
iostream basic properties
class inherits facilities that are used ostream
of istream, by all other input
class and
through
 add assignment
istream
ostream class operators
classinherits
inherits to its base
properties
properties ofofiosclasses.
ios
output classes(Necessary
multiple inheritance. for formatted input/output).
 cout which
Extraction
Insertion is directed
operator
operator <<,
>>, toget(),
put()video
and display,
getline()
write() isare
and predefined
read() are object
members members
of of of
ostream
 Alsoclass
The contains
ios pointer
declared to
as athe
buffer object(streambuf)
virtual base class so that only one copy
ostream_withassign.
istream
class class
 streambuf
of provides
its members an interface
inherited to physical device through buffers.
by the iostream.
 Similarly cin is an object of istream_withassign.
I/O and File Management 7
Unformatted and Formatted I/O
put(), get(), getline(), write() - Unformatted I/O Operations
char ch; Get a character from keyboard
cin.get(ch); Similar to cin.get(ch);
ch=cin.get(); The operator >> can also be used to read a
cin>>ch; character but it will skip the white spaces
cout.put(ch); and newline character.
I like C++ so much
cout.put('x'); put() function can be used to display value of
variable ch or character.
I like
char name[20];
Rupesh
cin.getline(name,10); getline() sir
reads whole line of text
line size
that ends with newline character or
cin>>name; upto (size-1).
cin can read strings that do not contain white
spaces. write() displays string of given size, if
cout.write(name,10);
the size is greater than the length of
line, then it displays the bounds of
line.
I/O and File Management 9
ios Format Functions
Function Task
width() To specify the required field size for displaying an output value
precision() To specify number of digits to be displayed after the decimal point of
a float value.
fill() To specify a character that is used to fill the unused portion of a field.
setf()
I like C++ so much
To specify format flags that can control the form of output.
unsetf() I like
To clear Rupesh sir
the flags specified

Example: output:
output:
cout.precision(6);
cout.fill('*');
cout.width(6);
cout.setf(ios::left,ios::adjustfield);
* * * 5 42 3. 6 4 5 7 5
cout.width(10);
cout.width(6);
cout<<"543";
cout.width(6);
cout<<sqrt(7);
cout<<"543"; output:
cout.fill('#');
cout<<"543"; 5 4 3 # # #
I/O and File Management 10
Flags and bit fields
Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
Scientific notation ios::scientific ios::floatfield
Fixed point notation
I likeios::fixed
C++ so muchios::floatfield
Decimal base I likeios::dec
Rupesh sir ios::basefield
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield

setf(arg1, arg2)
arg-1: one of the formatting flags.
arg-2: bit field specifies the group to which the formatting flag belongs.
I/O and File Management 11
Manipulators for formatted I/O operations
 Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream.
 To access manipulators, the file <iomanip> should be included in
the program.
Function I like C++
Manipulator Meaningso much
width() setw() Set the field width.
precision()
I like
setprecision()
Rupesh sir
Set the floating point precision.
fill() setfill() Set the fill character.
setf() setiosflags() Set the format flag.
unsetf() resetiosflags() Clear the flag specified.
“\n” endl Insert a new line and flush stream.

I/O and File Management 12


File stream classes
File input output streams

Input stream
read data data
input
I like C++ so much
Disk Files Program

I like Rupesh
Output stream sir data
output
write data
File input output streams

I/O and File Management 14


File stream classes for file operations
ios

iostream istream streambuf ostream


file

I like C++ so much


iostream

fstream
I like Rupesh
ifstream fstream sir
ofstream filebuf
file
fstream base

To read content To write content


from the file to the file
I/O and File Management 15
File stream classes
class contents
fstreambase  Provides operations common to the file streams.
 Contains open() and close() functions.

ifstream  Provides input operations.


 Contains open() with default input mode.
I like C++ so much
 Inherits get(), getline(), read(), seekg() and tellg() functions from
istream.

ofstream I like Rupesh sir


 Provides output operations.
 Contains open() with default output mode.
 Inherits put(), seekp(), tellp() and write() functions from
ostream.

fstream  Provides support for simultaneous input and output operations.


 Inherits all the functions from istream and ostream from
iostream.

filebuf  Its purpose is to set the file buffers to read and write.

I/O and File Management 16


File handling steps
1. Open / Create a file
2. Read / Write a file
3. Close file

I like C++ so much


I like Rupesh sir

I/O and File Management 17


Create and Write File (Output)
Create object of ofstream class
ofstream send;

Call open() function using ofstream object to open a file


I like C++This
send.open("abc.txt"); so will much
open existing file, if not exist
then it will create file.
I like Rupesh sir
Write content in file using ofstream object
send<<"Hello, this is India";
Call close() function using ofstream object to close file
send.close();
I/O and File Management 18
Open and Read File (Input)
Create object of ifstream class
ifstream rcv;

Call open() function using ifstream object to open a file


I like C++ so much
rcv.open("abc.txt");

Read contentI of
like Rupesh
file using sir
ifstream object
rcv>>name; rcv.getline(name);

Call close() function using ifstream object to close file


rcv.close();

I/O and File Management 19


Opening a file
ofstream outFile("sample.txt"); //output only
ifstream inFile("sample.txt"); //input only
ofstream outFile;
outFile.open("sample.txt");
This creates outFile as an ofstream object that manages the output
stream.
ifstream inFile; I like C++ so much
This object can be any valid C++ name such as myfile, o_file .
inFile.open("sample.txt");
I like Rupesh sir
 Syntax file open() function:
stream-object.open("filename", mode);
 By default ofstream opens file for writing only and ifstream
opens file for reading only.

I/O and File Management 20


File open() function
File open() function
Syntax:
stream-object.open("filename", mode);

 By default ofstream opens file for writing only


 By default ifstream opens file for reading only.
I like C++ so much
Three ways to create a file
1 I like
ofstream Rupesh sir
send("abc.txt"); //constructor
ofstream send;
2
send.open("abc.txt"); //open() function

ofstream send;
3 send.open("abc.txt",ios::out); //open()
function with mode
I/O and File Management 22
File opening modes
Parameter Meaning
ios :: in Open file for reading only
ios :: out Open file for writing only
ios :: app Append to end-of-file
ios :: ate I likeGoC++ so much
to end-of-file on opening
ios :: binary Binary file
ios :: trunc
I likeDelete
Rupesh sir
content of file if exists
ios :: nocreate Open fails if the file does not exists
ios :: noreplace Open fails if the file already exists

I/O and File Management 23


File operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
I like C++ so much
myfile.open("example.txt",ios::out);
I like Rupesh sir
myfile << "This is India.\n";
myfile.close();
}
example.txt

.cpp This is India

I/O and File Management 24


File operations (Cont..)
int main ()
{
char line[50];
ifstream rfile;
rfile.open("example.txt",ios::in)
I like C++ so much
rfile.getline(line,50);
// rfile>>line is also valid;
I
cout<<line;
like Rupesh sir
rfile.close();
}
example.txt
This is India
.cpp This is india

I/O and File Management 25


int main()
{ File operations program
char product[20];
int price;
cout<<"Enter product name=";
cin>>product;
cout<<"Enter price=";
cin>>price;
Opening a file to write
ofstream outfile("stock.txt");
outfile<<product<<endl; data into file
outfile<<price;

ifstream infile("stock.txt"); Opening a file to read


infile>>product; data from file
infile>>price;

cout<<product<<endl;
cout<<price;
}
File handling Program
 Write a program that opens two text files for reading data.
 It creates a third file that contains the text of first file and then
that of second file
(text of second file to be appended after text of the first file, to
I like C++ so much
produce the third file).

I like Rupesh sir

I/O and File Management 27


int main() {
fstream file1,file2,file3;
file1.open("one.txt",ios::in);
file2.open("two.txt",ios::in);
file3.open("three.txt",ios::app);
char ch1,ch2;
while(!file1.eof())
{
file1.get(ch1); cout<<ch1<<endl;
file3.put(ch1);
}
file1.close();
while(!file2.eof())
{
file2.get(ch2); cout<<ch2<<endl;
file3.put(ch2);
}
file2.close(); file3.close();
}
File pointers
 Each file has two associated pointers known as the file pointers.
 One of them is called input pointer (or get pointer) and the other
is called output pointer (or put pointer).
 Input pointer is used for reading the content of a given file
location. I like C++ so much
 Output pointer is used for writing to a given file location.
I like Rupesh sir

I/O and File Management 29


Functions for manipulation of file pointers
Function Meaning
seekg() Moves get pointer (input) to specified location
seekp() Moves put pointer (output) to specified location
tellg() Gives current position of the get pointer
tellp() IGives
likecurrent
C++ soofmuch
position the put pointer

ifstream rcv;
I like Rupesh sir
ofstream send;

rcv.seekg(30); //move the get pointer to byte number 30 in the file


send.seekp(30);//move the put pointer to byte number 30 in the file
int posn = rcv.tellg();
int posn = send.tellp();
I/O and File Management 30
Functions for manipulation of file pointers
Another prototype
seekg ( offset, direction );
seekp ( offset, direction );

Function Meaning
ios::beg Ioffset
likecounted
C++fromsothemuch
beginning of the stream
ios::cur Ioffset
like Rupesh
counted
stream pointer
sir
from the current position of the

ios::end offset counted from the end of the stream

I/O and File Management 31


write() and read() functions
 The functions write() and read(), different from the
functions put() and get(), handle the data in binary form.
infile.read ((char * ) &V,sizeof(V));
outfile.write ((char *) &V ,sizeof(V));
I like C++ so much
 These functions take two arguments. The first is the address of the
variable V, and the second is the length of that variable in bytes.
The address Ioflike
 Rupesh
the variable sirto type char*(i.e pointer
must be cast
to character type).

I/O and File Management 32


Reading & Writing class objects
class inventory
{
char name[10];
float cost;
public:
void readdata()
{
cout<<"Enter Name=";
cin>>name;
cout<<"Enter cost=";
cin>>cost;
}
void displaydata()
{
cout<<"Name="<<name<<endl;
cout<<"Cost="<<cost;
}
};
Reading & Writing class objects
int main()
{
inventory ob1;
cout<<"Enter details of product\n";

fstream file;
file.open("stock.txt",ios::in | ios::app);

ob1.readdata();
file.write((char *)&ob1,sizeof(ob1));

file.read((char *)&ob1,sizeof(ob1));

ob1.displaydata();
file.close();
}
Thank You

You might also like