VIRTUAL BASE CLASS
When more than one types of inheritance are
involved, the child may have two base classes
who themselves have a common base class.
Here the child or derived class will have
duplicate sets of members inherited from the
grand parent via parent class. This causes
ambiguity.
The duplication of members due to multiple
paths can be avoided by making the common
base class as virtual base class.
When a class is made virtual base class, C++
sees to it that only one copy of that class is
inherited regardless of many inheritance
paths.
ABSTRACT CLASS
It is a class that is not used to create
objects.
It is created to be inherited by other
classes.
CONSTRUCTORS IN DERIVED CLASSES
If a base class contains a constructor
with one or more arguments, then the
derived class should have constructor
and pass arguments to the base class
constructor.
When both derived and base class
contain constructor, first the base
constructor is executed and then the
derived constructor is executed
NESTING OF CLASSES
When a class contain an object of
another class as its member, then it is
called nesting a class
VIRTUAL FUNCTIONS AND
POLYMORPHISM
Polymorphism stands for many forms.
In C++, polymorphism stands for
functions with same name and different
behaviors based on the arguments
passed to them.
The concept of polymorphism is
implemented by overloaded functions
and operators.
STATIC BINDING/EARLY BINDING
The overloaded functions are invoked by
matching the number and type of
arguments. This information is known to
the compiler at compile time and therefore
the compiler is able to select the
appropriate function for a particular call at
the compile time itself. This is called early
binding or static binding or static linking.
This is also called COMPILE TIME
POLYMORPHISM.
The object is bound to its function at
compile time.
DYNAMIC BINDING/LATE BINDING
Here the appropriate function is selected at
runtime.
C++ uses the concept of virtual functions to
achieve runtime polymorphism.
At runtime, it is known what class objects are
under consideration. Then the appropriate
version of function is called. Since the function
is linked with a class much later after the
compilation, this process is termed as late
binding.
It is also called dynamic binding because the
selection of appropriate function is done
dynamically at runtime.
Dynamic binding requires pointers to objects
POINTERS TO OBJECTS
Eg:
item x;
item *ptr;
Object pointers can be used to access public members
of an object
Member functions can be access using dot operator or
arrow operator
Eg:
[Link]() or ptr→getdata();
This can also be written as
(*ptr).getdata();
NOTE: dot operator has higher precedence than *.
THIS POINTER
The keyword “this” represents the
pointer that points to the object for which
this function was called.
[Link](); will set the “this”
pointer to the address of object student.
POINTER TO DERIVED CLASS
If B is the base class and D is the
derived class, then a pointer to B can
also be a pointer to D
Using this pointer we can access the
members inherited from B and not the
original members of D
VIRTUAL FUNCTIONS
When we use the same function name in
both the base and derives classes, the
function is declared as virtual preceding its
normal declaration.
When a function is declared virtual, C++
decides which function to use at run time
based on the type of object pointed to by
the base pointer.
Thus by making pointer point to different
objects, we can execute different versions
of the virtual functions
RULES FOR VIRTUAL FUNCTIONS
Must be members of the some class
Cannot be static members(A static member is something
that does not relate to any instance, only to the class.
A virtual member is something that does not relate
directly to any class, only to an instance.)
Virtual function can be a friend of another class_
A virtual function in a base class must be defined even
though it may not be used.
The prototype of the base class version and derived class
version should be the same
We cannot have virtual constructors. But we can have
virtual destructors
Base pointer can point to derived object, but reverse is
not true
When base pointer points to derived class, increment and
decrement are relative to its base type
Virtual functions are defined in base class. They need not
be redefined in derived class