Module 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

What are Pointers?

A pointer in C++ is a variable that stores the address (or memory location) of another
variable. In other words, a pointer points to the address of another variable. Like regular
variables, pointers in C++ have data types. A pointer should have the same data type as that
of the variable it points to.

C++ Pointer Declaration:

The general form of a pointer declaration is as follows:

Datatype *pointer-variable;

Dereference Operator (*):

The Asterisk symbol (*) is called dereference operator when it is used with pointers. We can
access the values stored in a variable to which pointer points to, by using the pointer's
identifier and the dereference operator.
Reference Operator (&):

The reference operator (&) returns the address of any variable (including pointers).

Pointer to object
A pointer to an object acts the same as Pointer to a variable. But here, in place of the address
of the variable, address of the object is stored. In the main function, when an object is created
to a class, a pointer variable is declared in the same manner as we declared for the variable,
and it will store the object's address. For creating a pointer to an object, we should not use
data type for the Pointer. Instead, we need to use the class name for the object
pointer. If we want to use a member function in the class using the Pointer in the main
function, then we need to use the -> symbol, as shown in the below example.
C++ this Pointer
 In C++ programming, this is a keyword that refers to the current instance of
the class.

 this pointer points to the current object of the class.

 The this pointer is an implicit parameter to all member functions. Therefore, inside a


member function, this may be used to refer to the invoking object.
Pointer to Derived Class
In C++ a pointer of base class can be point to both base class and derived class objects.
But base class pointer can only access those members of derived class which are inherited
from base class not those members which are declared in derived class only. Althought c+
+ language permits a base pointer to point to any object derived from the base the pointer
cannot be directly used to access all the members of the derived class. we may have to use
another pointer declared as pointer to the derived type.
Early Binding:
In early binding, the compiler matches the function call with the correct function definition at
compile time. It is also known as Static Binding or Compile-time Binding. By default, the
compiler goes to the function definition which has been called during compile time. 
using namespace std;
class Animals
{
public:
void sound()
{ cout << "This is parent class" << endl;
};
class Dogs : public Animals
{
public:
void sound()
{ cout << "Dogs bark" << endl;
}
};
int main()
{ Animals *a;
Dogs d;
a= &d;
a -> sound(); // early binding
return 0;
}

Output
This is parent class

Now in this example, we created a pointer a to the parent class Animals. Then by writing a=
&d , the pointer 'a' started referring to the object d of the class Dogs.
a -> sound(); - On calling the function sound() which is present in both the classes by the
pointer 'a', the function of the parent class got called, even if the pointer is referring to the
object of the class Dogs.
This is due to Early Binding. We know that a is a pointer of the parent class referring to the
object of the child class. Since early binding takes place at compile-time, therefore when the
compiler saw that a is a pointer of the parent class, it matched the call with the 'sound()'
function of the parent class without considering which object the pointer is referring to.
Late Binding:
In the case of late binding, the compiler matches the function call with the correct function
definition at runtime. It is also known as Dynamic Binding or Runtime Binding.
In late binding, the compiler identifies the type of object at runtime and then matches the
function call with the correct function definition.
By default, early binding takes place. So if by any means we tell the compiler to perform late
binding, then the problem in the previous example can be solved.
This can be achieved by declaring a virtual function.

Virtual function
Virtual Function is a member function of the base class which is redefined (overridden) in
the derived class. The compiler performs late binding on this function.
To make a function virtual, we write the keyword virtual before the function definition.

#include <iostream>
using namespace std;
class Animals
{ public:
virtual void sound()
{ cout << "This is parent class" << endl;
}
};
class Dogs : public Animals
{ public:
void sound()
{ cout << "Dogs bark" << endl;
}
};
int main()
{ Animals *a;
Dogs d;
a= &d;
a -> sound();
return 0;
}

Since the function sound() of the base class is made virtual, the compiler now performs
late binding for this function. Now, the function call will be matched to the function
definition at runtime. Since the compiler now identifies pointer a as referring to the object
'd' of the derived class Dogs, it will call the sound() function of the class Dogs.

Pure Virtual Function


Pure virtual function is a virtual function which has no definition. Pure virtual functions are
also called abstract functions.
To create a pure virtual function, we assign a value 0 to the function as follows.

virtual void sound() = 0;


Here sound() is a pure virtual area.
Files
Files play an important role in programming. It allows storage of data permanently.
The C++ language provides a mechanism to store the output of a program in a file
and browse from a file on the disk. This mechanism is termed file handling.
File handling is used to store data permanently in a computer. Using file handling we
can store our data in secondary memory (Hard disk).

How to achieve the File Handling


For achieving file handling we need to follow the following steps:-
 STEP 1-Naming a file
 STEP 2-Opening a file
 STEP 3-Writing data into the file
 STEP 4-Reading data from the file
 STEP 5-Closing a file.

In order to perform file handling, some general functions which are used are as
follows:

 open(): This function helps to create a file and open the file in different
modes, like input operations, output operations, binary mode, etc.
 close(): This function helps to close an existing file.
 get(): This function helps to read a single character from the file.
 put(): This function helps to write a single character in the file.
 read(): This function helps to read data from a file.
 write(): This function helps us to write data into a file.

A stream is an abstraction that represents a tool on which operations of input and


output are performed. In C++ there is a group of file handling methods. These
include ifstream, ofstream, and fstream.
fstream Library:  Fstream is a library that consists of
both, ofstream and ifstream which means it can create files, write information to files,
and read information from files. This header file is generally used as a data type that
represents the file stream. Which is used while describing the syntax to open, read, take
input and close the file, etc.

ofstream: This class helps create and write the data to the file obtained from the program’s
output. It is also known as the input stream.

ifstream: We use this class to read data from files and also known as the input stream.

fstream: This class is the combination of both ofstream and ifstream. It provides the
capability of creating, writing and reading a file.

Opening a file
Usually, the first operation performed on an object of one of these classes is to associate it to
a real file. This procedure is known as opening a file.
We can open a file using one of the following methods:

 bypassing the file name in constructor at the time of object creation.

 using the open() function.

 The first method is preferred when a single file is used with a stream. However, for
managing multiple files with the same stream, the second method is preferred. Let's
discuss each of these methods one by one.

Opening File Using Constructors


 We know that a constructor of class initializes an object of its class when it (the
object) is being created. Same way, the constructors of stream classes (ifstream,
ofstream, or fstream) are used to initialize file stream objects with the filenames
passed to them. This is carried out as explained here:

ifstream fin("myfile") ;
Opening Files Using Open() Function

There may be situations requiring a program to open more than one file. The strategy for
opening multiple files depends upon how they will be used. If the situation requires
simultaneous processing of two files, then you need to create a separate stream for each file.
However, if the situation demands sequential processing of files (i.e., processing them one by
one), then you can open a single stream and associate it with each file in turn. 

The function open() can be used to open multiple files that uses same stream object.
File Modes
There are some mode flags used for file opening. These are:

 ios::app: append mode. append to the end of file


 ios::ate: go to end of file on opening. Permit add or modify at any position on file.
 ios::in: open a file in this mode for reading.
 ios::out: open a file in this mode for writing.
 ios::trunk: when any file already exists, its contents will be truncated before the file
opening.

Example…

StreamO bject.open(“filename”,mode);

When any C++ program terminates, it automatically flushes out all the streams, releases all
the allocated memory, and closes all the opened files. But it is good to use the close()function
to close the file-related streams, which are a member of ifsream, ofstream,
and fstream objects.

stream_object.close();

C++ File Pointer And File Manipulators

What is a file pointer?


 A pointer is used to handle and keep track of the files being accessed. Every file maintains two
pointers called get_pointer (in input mode file) and put_pointer (in output mode file), which tells
the current position where reading or writing will take place with the use of opening modes and
their respective manipulators.
Functions for manipulation of file pointers
C++ Sequential input output operations
The file stream classes support a number of member functions for performing the input and
output operation on files. one pair of function put() and get() are designed for handling a single
character at a time. Another pair of functions write() and read() are use to write and read blocks
of binary data.

put() and get() function

 here put() write a single character to the associated stream.

 the get() function reads a single character from the associated stream.

 write() and read() function

 the function write() and read() unlike the function put() and get(), handled the data in
binary form. This means that the values are stored in the disk file in the same format in
which they are stored in the internal memory. in fig show how an int value 2594 is stored,
in the binary and character format. an int takes two bytes to store its value in the binary
form. irrespective of its size but a 4 digit in will take four bytes to store it in the character
form

 The binary format is more accurate for storing the number as they are stored in the exact
internal representation. there are no conversions while saving the data and therefore
saving is much faster the binary input and output functions takes the following form
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 of the variable must be cast to
type char* 

You might also like