Module 5
Module 5
Module 5
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.
Datatype *pointer-variable;
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 (&):
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.
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.
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.
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:
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.
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:
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();
the get() function reads a single character from the associated stream.
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*