C++ Polymorphism
C++ Polymorphism
2. Function that is redefined must have exactly the same declaration in both base and
derived class, that means same name, same return type and same parameter list.
In this example, function show() is overridden in the derived class. Now let us study how
these overridden functions are called in main() function.
Function Call Binding with class Objects
Connecting the function call to the function body is called Binding. When it is done
before the program is run, its called Early Binding or Static Binding or Compile-
time Binding.
class Base
{
public:
void shaow()
{
cout << "Base class\n";
}
};
int main()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Ocuurs
d.show();
}
In the above example, we are calling the overrided function using Base class and Derived
class object. Base class object will call base version of the function and derived class's
object will call the derived version of the function.
Function Overloading in C++
If any class have multiple functions with same names but different parameters then
they are said to be overloaded. Function overloading allows you to use the same
name for different functions, to perform, either same or different functions in the
same class.
Function overloading is usually used to enhance the readability of the program. If
you have to perform one single operation but with different number or types of
arguments, then you can simply overload the function.
// first definition
int sum (int x, int y)
{
cout << x+y;
}
// first definition
int sum(int x, int y)
{
cout<< x+y;
}
int main()
{
sum (10,20);
sum(10.5,20.5);
}
Operator Overloading in C++
Operator overloading is an important concept in C++. It is a type of polymorphism in
which an operator is overloaded to give user defined meaning to it. Overloaded
operator is used to perform operation on user-defined data type. For example '+'
operator can be overloaded to perform addition on various data types, like for
Integer, String(concatenation) etc.
Almost any operator can be overloaded in C++. However there are few operator
which can not be overloaded. Operator that are not overloaded are follows
scope operator - ::
sizeof
member selector - .
member pointer selector - *
ternary operator - ?:
1. Member Function
2. Non-Member Function
3. Friend Function
4. Cannot redefine the meaning of a procedure. You cannot change how integers are
added.
Overloading Arithmetic Operator in C++
Arithmetic operator are most commonly used operator in C++. Almost all arithmetic
operator can be overloaded to perform arithmetic operation on user-defined data
type. In the below example we have overridden the + operator, to add to
Time(hh:mm:ss) objects.
While normal addition of two numbers return the sumation result. In the case above
we have overloaded the + operator, to perform addition of two Time class objects. We
add the seconds, minutes and hour values separately to return the new value of
time.
In the setTime() funtion we are separately asking the user to enter the values for
hour, minute and second, and then we are setting those values to the Time class
object.