POINTER TO OBJECTS:
A Variable That Holds an Address value is called a Pointer variable or
simply pointer. We already discussed about pointer that's point to
Simple data types likes int, char, float etc. So similar to these type of
data type, Objects can also have an address, so there is also a pointer
that can point to the address of an Object, This Pointer is Known as This
Pointer.
Pointer pointing to derived
Approach:
A derived class is a class which takes some properties
from its base class.
It is true that a pointer of one class can point to other
class, but classes must be a base and derived class,
then it is possible.
To access the variable of the base class, base class
pointer will be used.
So, a pointer is type of base class, and it can access
all, public function and variables of base class since
pointer is of base class, this is known as binding
pointer.
In this pointer base class is owned by base class but
points to derived class object.
Same works with derived class pointer, values is
changed.
A pointer to derived class is a pointer of base class pointing to
derived class, but it will hold its aspect.
This pointer of base class will be able to
temper functions and variables of its own class and can still point to
derived class object.
Virtual function,
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.
Rules for Virtual Functions
Virtual functions cannot be static.
A virtual function can 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 the same in the base as
well as derived class.
They are always defined in the base class and overridden in a derived
class. It is not mandatory for the derived class to override (or re-
define the virtual function), in that case, the base class version of the
function is used.
A class may have virtual destructor but it cannot have a virtual
constructor.
Early Binding (compile-time time polymorphism) As the name indicates,
compiler (or linker) directly associate an address to the function call. It
replaces the call with a machine language instruction that tells the
mainframe to leap to the address of the function.
By default early binding happens in C++. Late binding (discussed below) is
achieved with the help of virtual keyword)
Late Binding : (Run time polymorphism) In this, the compiler adds
code that identifies the kind of object at runtime then matches the
call with the right function definition (Refer this for details). This can
be achieved by declaring a virtual function.
Early binding and Late binding in C++
Difference between virtual function and pure virtual function in C++
Virtual functionz Pure virtual function
A virtual function is a member function of base class
which can be redefined by derived class.
A pure virtual function is a member function of
base class whose only 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 abstract.
Base class containing pure virtual function
becomes abstract.
Syntax:
virtual<func_type><func_name>()
{
// code
}
Syntax:
virtual<func_type><func_name>()
= 0;
Definition is given in base class. No definition is given in base class.
Base class having virtual function can be instantiated i.e.
its object can be made.
Base class having pure virtual function
becomes abstract i.e. it cannot be instantiated.
If derived class do not redefine virtual function of base
class, then it does not affect compilation.
If derived class do not redefine virtual function
of base class, then no compilation error but
derived class also becomes abstract just like
the base class.

Polymorphism

  • 3.
    POINTER TO OBJECTS: AVariable That Holds an Address value is called a Pointer variable or simply pointer. We already discussed about pointer that's point to Simple data types likes int, char, float etc. So similar to these type of data type, Objects can also have an address, so there is also a pointer that can point to the address of an Object, This Pointer is Known as This Pointer.
  • 4.
    Pointer pointing toderived Approach: A derived class is a class which takes some properties from its base class. It is true that a pointer of one class can point to other class, but classes must be a base and derived class, then it is possible. To access the variable of the base class, base class pointer will be used. So, a pointer is type of base class, and it can access all, public function and variables of base class since pointer is of base class, this is known as binding pointer. In this pointer base class is owned by base class but points to derived class object. Same works with derived class pointer, values is changed.
  • 5.
    A pointer toderived class is a pointer of base class pointing to derived class, but it will hold its aspect. This pointer of base class will be able to temper functions and variables of its own class and can still point to derived class object.
  • 14.
    Virtual function, A virtualfunction 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.
  • 15.
    Rules for VirtualFunctions Virtual functions cannot be static. A virtual function can 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 the same in the base as well as derived class. They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re- define the virtual function), in that case, the base class version of the function is used. A class may have virtual destructor but it cannot have a virtual constructor.
  • 16.
    Early Binding (compile-timetime polymorphism) As the name indicates, compiler (or linker) directly associate an address to the function call. It replaces the call with a machine language instruction that tells the mainframe to leap to the address of the function. By default early binding happens in C++. Late binding (discussed below) is achieved with the help of virtual keyword) Late Binding : (Run time polymorphism) In this, the compiler adds code that identifies the kind of object at runtime then matches the call with the right function definition (Refer this for details). This can be achieved by declaring a virtual function. Early binding and Late binding in C++
  • 17.
    Difference between virtualfunction and pure virtual function in C++ Virtual functionz Pure virtual function A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only 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 abstract. Base class containing pure virtual function becomes abstract. Syntax: virtual<func_type><func_name>() { // code } Syntax: virtual<func_type><func_name>() = 0; Definition is given in base class. No definition is given in base class. Base class having virtual function can be instantiated i.e. its object can be made. Base class having pure virtual function becomes abstract i.e. it cannot be instantiated. If derived class do not redefine virtual function of base class, then it does not affect compilation. If derived class do not redefine virtual function of base class, then no compilation error but derived class also becomes abstract just like the base class.