overriding
.
Polymorphism in CPP can be defined as the capability of objects of different types responding to functions with same name in their own ways. overridden
. The process is called Function overriding.
Function overriding involves Inheritance.Without inheritance, one cannot perform overriding on functions.
[c]
//Example for function overriding
class base_class
{
public:
void splessons()
{
cout << "Welcome to splessons.";
}
};
class derived_class : public base
{
public:
void splessons()
{
cout << "You are reading splessons";
}
}
[/c]
Here, splessons in parent class is overridden by child class. Binding
.
If binding process is done before run-time, it is called as Compile-time binding or Static binding or Early binding.
[c]
//Example for early binding
#include<iostream>
using namespace std;
class parent_class
{
public:
void splessons()
{
cout << "Welcome to splessons.";
}
};
class child_class:public parent_class
{
public:
void splessons()
{
cout << "You are reading c++";
}
};
int main()
{
parent_class p; //object of parent class
child_class c; //object of child class
p.splessons(); //Compile-time binding or early binding
c.splessons();
}[/c]
Output
[c]
Welcome to splessons.You are reading c++[/c]
In compile-time binding, even if the child class is pointed and called, the compiler will point to parent class only.
VTABLES
and vpointers
. vpointers stores the address of objects created in classes of virtual functions. When a virtual function is called the pointer binds to the correct address in the VTABLE.