0% found this document useful (0 votes)
18 views21 pages

Unit 4 Set 1

Uploaded by

ia9412738
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views21 pages

Unit 4 Set 1

Uploaded by

ia9412738
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

FFT OOPS (OBJECT ORIENTED

PROGRAMMING LANGUAGE
15 FO
R TE
EN

BY TUS
HIKA R
AWAT

UNIT -4
SET-1

doubt solving
For updates for lectures community for notes profile building
Namespace
Namespace is used to define a scope where identifiers like variables ,functions
,classes etc are declared.
Main purpose of using a namespace is to prevent ambiguity that may occur
when two idedntifiers have same name.
It’s definition is similar to class definition ,the only difference s it doesn’t end
with a semicolon like class does.
There can be unnamed namespace too.
A namespace definition can be continued and extended over multiple files,
they do not be overriden or redefined.
Using scope resolution
operator
The using directive: using declaration:
Using keyword allows you to input an entire we import 1 specific name at a time ,which is
namespace into your program with global space available inside the current scope.
Object Persistance

Often involves serializing (saving) and deserializing (loading) objects.


Serialization is the process of converting an object into a format that can be
stored (such as in a file or a database), and deserialization is the process of
reconstructing the object from that stored format.
Files and streams
iostream standard library provides cin and cout methods

fstream = > supports for simultaneous i/p & o/p operations on files
ifstream =>provides i/p operations on files (read data)
ofstream =>provides o/p operations on files (write data)

file i/p and o/p streams


stream classes

istream , ostream and iostream is for standard input output functions


ifstream,ofstream andfstream is for file input output functions
opening a file
A file must be opened before you can read from it or write to it.
ofstream or fstream obj maybe used to open a file for writing.
ifstream obj is used to open a file for reading purpose only.
syntax for open() function
void open(const char *file name;
ios::openmode mode)
const char *file name=specifies the name and location of file to be
opened.
ios::open mode r/w = defined mode in which file should be opened
Different mode flags
outfile.open ("file.data", cos::out/ios:: trunc):
outfile: This likely refers to an object of the ofstream
class (output file stream).
Example:
.open(): This method opens a file for writing.
"file.data": This is the name of the file to be opened.
ios::out: This is a file mode flag indicating that the file
is being opened for output.
ios::trunc: This is a file mode flag used to truncate the
file before writing. If the file exists, its contents will
be erased before writing.
fstream afile:
fstream: This is the file stream class.
afile: This is a file stream object.
afile. open ("file.dat" (tos:: out/los :: in)
afile: This is the file stream object.
.open(): This method opens a file for reading and
writing.
"file.dat": This is the name of the file to be opened.
ios::out/ios::in: These are file mode flags indicating
that the file is being opened for both output (writing)
and input (reading).
closing a file
Automatically close all the opened files & releases all the allocated
memory. But its a good practice for programmers to close on their own.
syntax:
void close();

writing to a file
We use stream insertion operator(<<) only differernce is we use ofstream or
fstream object instead of cout object

reading from a file


We use stream extraction operator(>>) only differernce is we use ifstream or fstream
object instead of cin object
file i/p o/p with cpp
Reading & writing
operations into the file
C++ Standard Exceptions
Cpp provides a list of standard exceptions defined in <exception> which we can use in
our programs.
These are arranged in parent child class hierarchy.
cpp provides range of built in exception.The base class for all exceptions classes is
“Exception”
All exception classes in cpp are derived from std::Exception class

Process of
converting system
error message into
user friendly error
message is known as
exception handling
It is an event which occurs
during execution of
program,that disrupts the
normal flow of the program.
Exception Handling mechanism

Syntax:
Try block is intended to throw exception which is
followed by catch block,only 1 try block.

Catch block is intended to catch the error & handle the


exception .We can have multiple catch block.

throw keyword ,throws an exception encountered inside


try block .communicates info about errors
Here the program compiles successfully but the program fails during runtime
Example 2:
Output:
Multiple catch blocks

output:
char exception caught
End of program
FFT
15 FOR TEEN

NEW VIDEOS
EVERY WEEK

Chapter-2
Chapter-2
FFT
15 FOR TEEN

doubt solving
For updates for lectures community for notes profile building
Visist
Channel
For More
Subjects

Check Out

Click & Follow


INSTAGRAM YOUTUBE WHATSAPP WEBSITE LinkedIn

You might also like