0% found this document useful (0 votes)
7 views14 pages

CPP Unit-4

The document covers pointers in C++, including pointers to members and objects, as well as the 'this' pointer for accessing class members. It also explains file handling, including opening and closing files, file modes, and sequential I/O operations, along with random access file updates. Additionally, it discusses command line arguments and exception handling, detailing the process of throwing and catching exceptions to manage runtime errors.

Uploaded by

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

CPP Unit-4

The document covers pointers in C++, including pointers to members and objects, as well as the 'this' pointer for accessing class members. It also explains file handling, including opening and closing files, file modes, and sequential I/O operations, along with random access file updates. Additionally, it discusses command line arguments and exception handling, detailing the process of throwing and catching exceptions to manage runtime errors.

Uploaded by

tach8126ddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit -4

Pointer, File
Handling
&
Exception Handling
PREPARED BY :
BRIJESH KHODA
Prepared by
Brijesh Khoda
Pointer to member
 Pointer to members allows you to store and
manipulate pointer to class members.
 We can assign the address of data members
into pointer variable.
 Class temp
 { int no,*p;
 };
 We can also declare pointer out side of class
using (::*)pointer to data member operator.
 Example:
 Int temp ::* p = & temp::no;
Pointer to Object
 A pointer to an object in C++ is a variable that contains an
object's memory address.
 Pointers provide indirect access to class members. They are
especially helpful when we need to dynamically allocate memory
for objects.
 For this, we need to create pointer of class type.
 Syntax: class-name *pointer;

 After creating pointer, we can assign address of object as


following.
 Pointer = & object.

 We can use arrow operator (->) to access members of an object


using pointer variable.
 Example: pointer -> data member;
This Pointer
 The compiler supplies an implicit pointer
along with the names of the functions as
‘this’.
 The ‘this’ pointer is passed as a hidden

argument to all member function calls and


is available as a local variable within the
body of all functions.
 Now, we can access all data members of

current object using this pointer.


 Syntax: this -> data member.
File stream classes
 Stream:
 A stream is a sequence of bytes which transfers between source to
destination during I/O operation.
 It act either as a source from which the input can be obtained or as
a destination to which the data can be sent.
Opening and Closing file
 To open a file, first we must create a file stream using ifstream,
ofstream or fstream, and then link that stream to the filename.
 For this, Create a file stream object to manage file stream using
ifstream or ofstream class. Ofstream class is used to create output
stream and ifstream class is used to create input stream.

 Then file can be opened in two ways:


 1. using constructor
 Initialize file steam object with desired filename.
 Syntax : filestream_class object(“filename”);
 Example: ofstream fout(“abc.txt”);

 2. using open() function.


 Use open() with file stream object.
 Syntax : object.open(“filename”);
 Example : fout.open(“abc.txt”);
File Modes
 Mode of file specifies the purpose of opening a file.
 Open() uses two arguments:
 Syntax : fileobject.open(“filename”,mode);
 We can use following modes defined in ios class.

Mode Meaning
Ios::app Appen to the end of file.
Ios::ate Go to end of the file on opening
Ios::binary Binary file
Ios::in Open file for reading only
Ios::out Open file for writing only
Ios::trunc Delete the contents of file
Ios::nocreate Open fails if the file does not exists
Ios::noreplace Open fails if the file already exists
File pointers
 Each file has two pointers known as file pointers.
 One is called get pointer and other is called put pointer. Get
pointer is used for reading the contents of file and put pointer is
used for writing to a file.
 At a time only one operation can be performed either input or
output. So, when we open a file for reading, get pointer is
automatically set at beginning of file. When we open a file for
witting, the existing contents are deleted and put pointer is
automatically set at the beginning of file.
Sequential I/O operations
 The file stream classes supports member functions to performing
input and output operations on file.
 Get() and put() are used for handling single character at a time.
Read() and write() are used for reading and writing block of data.

 Get(char): Get() reads a single char from stream (file object) and
store into char variable.
 Syntax: fileobj.get(ch);

 Put(char) : put() writes a single character to the stream. It fetches


a character from char variable or string, and stored into stream.
 Syntax : fileobj.put(char);
Updating a file : Random
access
 To randomly update a file, the file pointers required to move to a
particular location in file.
 To access a file randomly, following manipulators are used in file.
 1. seekg() : It moves get pointer to a specified location.
 Syntax: fileobj.seekg(offset, refposition);
 Offset represents the number of bytes the file pointer is to be
moved from the location specified by refposition.
 Refposition takes 3 constants defined in ios class:
 Ios::beg : start of the file
 Ios:: cur : current position of the pointer
 Ios:: end : end of the file

 2. seekp() : It moves put pointer to the specifie location.


 3. tellg() : it gives current position of get pointer.
 4. tellp() : it gives current position of put pointer.
Command Line Argument
(CLA)
It is possible to pass some values from the command line to your
main() function. These values are called command line arguments.
 Syntax : main(int argc, char * argv[] )
 argc refers to the number of arguments passed, and argv[] is a
pointer array which points to each argument passed to the
program.
 argv[0] holds the name of the program itself and argv[1] is a
pointer to the first command line argument supplied, and *argv[n]
is the last argument.
Exception Handling
 Exception is an event (error) generated at runtime.
 Exception handling is a process to handle runtime error in C++. It
prints exception message and terminates the program, if we don't
handle the exception.
 All exceptions are derived from std::exception class.
 Following steps need to be followed to handle an exception.
 1. find the problem (find exception)
 2. inform that an error has occurred (throw the exception)
 3. receive the error information (catch the exception)
 4. take action. (code to handle the exception)
EXCEPTION HANDLING COMPONENTS

 Main()
 { …….
 Try
 { block of statement;
 throw exception;
 }
 Catch (type argu)
 { code to handle exception;
 }
}

 Try : it is a block of statement in which exception may generate.


 Throw : when exception is detected in try block, it is thrown using
throw statement.
 Catch : it catches the exception and execute the code to handle
the exception. It must be written immediately after try block.
Needs of Exception
Handling
 Exception handling can control run tune errors that occur in the program.
 It can avoid abnormal termination of the program.
 It can provide a facility to handle exceptions, throws message regarding exception
and completes the execution of program by catching the exception .
 It can separate the error handling code and normal code by using try-catch block.
 It develops a powerful coding which ensures that the exceptions can be prevented.

You might also like