Professional Documents
Culture Documents
Compile Time
Polymorphism Run time Polymorphism
Assignment operator =
function call operator ()
subscriping operator []
class member access operator ->
The process of overloading involves the
following steps :
1) Create a class that defines the data type
that is to be used in the overloading
operation.
2) Declare the operator function operator op()
in the public part of the class. It may be
either a member function or a friend
function.
3) Define the operator function to implement
the required operations.
Operator functions must be either member
functions (non-static) or friend functions.
Examples:
operator+
Return_type operator op(argument-list);
operator-
operator*
--- operator is a function operator/
--- op is one of C++ operator symbols (+, -, =, etc..)
14
Definition Syntax of operator function :
15
Unary operator: are operators that act upon a
single operand to produce a new value.
Example:
This conversion is exactly like conversion of
basic to user defined data type i.e. one
argument constructor is used.
Conversion routine is defined as a one
argument constructor in destination class and
takes the argument of the source class type
E.g.
classA objA;
classB objB;
objA=objB;
Here, objB is the source of the
class classB and objA is the destination object
of class classA.
the conversion routine should exist
in classA (destination class type) as a one
argument constructor.
Example
This conversion is exactly like conversion of user-
defined type to basic type i.e. overloading the
cast operator is used.
E.g.
classA objA;
classB objB;
objA=objB;
objB is the source of the class classB and objA is
the destination object of class classA. Conversion
routine is specified as conversion (cast) operator
overloading for the type of destination class.
//destination object class
class classA
{
//body of classA
} //source object class
class classB { private: //…. public:
operator classA () //cast operator destination
types { //code for conversion from
classB //to classA } };
Converting an expression of a given type into
another type is known as type-casting.
There are two types of casting as
1. Implicit Casting
2. Explicit Casting
Implicit conversions do not require any
operator. They are automatically performed
when a value is copied to a compatible type.
For example:
float a=20.10;
int b;
b=a;
the value is converted from float to int and we
have not had to specify any type-casting
operator.
This is known as a standard conversion.
Standard conversions affect fundamental data
types, and allow conversions such as the
conversions between numerical types
(short to int, int to float, double to int...), to or
from bool, and some pointer conversions.
Some of these conversions may imply a loss of
precision, which the compiler can signal with a
warning.
This warning can be avoided with an explicit
conversion.
Implicit conversions also include constructor or operator
conversions, which affect classes that include specific
constructors or operator functions to perform conversions.
For example:
class A {};
class B {
public: B (A a) {}
};
A a;
B b=a;
(type) expression
e.g.
float a=20.20;
int b;
b =(int) a;
example
const_cast<type> (expr) −
The const_cast operator is used to explicitly
override const in a cast.
The target type must be the same as the
source type except for the alteration of its
const attributes.
This type of casting manipulates the const
attribute of the passed object, either to be set
or removed.
Example
dynamic_cast<type> (expr) −
dynamic_cast can be used only with pointers
and references to objects.
Its purpose is to ensure that the result of the
type conversion is a valid complete object of
the requested class.
.
Member area could have been accessed with the
pointers to Polygon if area were a member
of Polygon instead of a member of its derived
classes, but the problem is
that Rectangle and Triangle implement different
versions of area, therefore there is not a single
common version that could be implemented in
the base class.
A virtual function is a member function which
is declared within a base class and is re-
defined(Overriden) by a derived class.
When you refer to a derived class object
using a pointer or a reference to the base
class, you can call a virtual function for that
object and execute the derived class’s version
of the function.
Virtual functions ensure that the correct
function is called for an object, regardless of
the type of reference (or pointer) used for
function call.
They are mainly used to achieve Runtime
polymorphism
Functions are declared with a virtual keyword
in base class.
The resolving of function call is done at Run-
time.
Example
Virtual functions cannot be static and also cannot
be a friend function of another class.
Virtual functions should be accessed using
pointer or reference of base class type to achieve
run time polymorphism.
The prototype of virtual functions should be
same in base as well as derived class.
They are always defined in base class and
overridden in derived class. It is not mandatory
for derived class to override (or re-define the
virtual function), in that case base class version
of function is used.
A class may have virtual destructor but it cannot
have a virtual constructor.
NOTE:
If we have created a virtual function in the base
class and it is being overridden in the derived
class then we don’t need virtual keyword in
the derived class, functions are automatically
considered as virtual functions in the derived
class.
If a class contains a virtual function then
compiler itself does two things:
If object of that class is created then a virtual
pointer(VPTR) is inserted as a data member of the
class to point to VTABLE of that class.
For each new object created, a new virtual
pointer is inserted as a data member of that
class.
Irrespective of object is created or not, a static
array of function pointer called VTABLE where
each cell contains the address of each virtual
function contained in that class
A pure virtual function (or abstract function)
in C++ is a virtual function for which we
don’t have implementation, we only declare
it.
A pure virtual function is declared by
assigning 0 in declaration.
Syntax:-
virtual void show() = 0;
Example:-
These are the concepts of Run-time
polymorphism.
Prototype i.e. Declaration of both the
functions remains the same throughout the
program.
These functions can’t be global or static.
Virtual Function Pure Virtual Function
A virtual function is a member function A pure virtual function is a member
of base class which can be redefined function of base class whose only
by derived class. declaration is provided in base class
and should be defined in derived class
otherwise derived class also becomes
abstract.
Classes having virtual functions are not Base class containing pure virtual
abstract. function becomes abstract.
Syntax:-
Syntax:-
virtual<func_type><func_name>()
virtual<func_type><func_name>()
= 0;
{
// code
}
Definition is given in base class. No definition is given in base class.
Base class having virtual function can Base class having pure virtual function
be instantiated i.e. its object can be becomes abstract i.e. it cannot be
1) A class is abstract if it has at least one pure
virtual function.
2)We can have pointers and references of
abstract class type.
3) If we do not override the pure virtual
function in derived class, then derived class
also becomes abstract class.
4) An abstract class can have constructors.
Deleting a derived class object using a
pointer of base class type that has a non-
virtual destructor results in undefined
behaviour.
To correct this situation, the base class
should be defined with a virtual destructor.
Example:-
Abstract Class is a class which contains
atleast one Pure Virtual function in it.
Abstract classes are used to provide an
Interface for its sub classes.
Classes inheriting an Abstract Class must
provide definition to the pure virtual function,
otherwise they will also become abstract
class.
Thank You