0% found this document useful (0 votes)
43 views

C++ Polymorphism

Polymorphism allows objects of derived classes to be treated as objects of their base class. Function overriding occurs when a derived class defines a function with the same name and parameters as a function in the base class, providing a new implementation. For overriding to occur, the function declarations must be identical in the base and derived classes. In an example, the show() function is overridden in the derived class, so calling show() on a derived class object will call that class's implementation instead of the base class implementation. Operator overloading allows operators to work on user-defined types by defining corresponding functions. Common operators like + can be overloaded to perform type-specific operations like adding two Time objects together.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

C++ Polymorphism

Polymorphism allows objects of derived classes to be treated as objects of their base class. Function overriding occurs when a derived class defines a function with the same name and parameters as a function in the base class, providing a new implementation. For overriding to occur, the function declarations must be identical in the base and derived classes. In an example, the show() function is overridden in the derived class, so calling show() on a derived class object will call that class's implementation instead of the base class implementation. Operator overloading allows operators to work on user-defined types by defining corresponding functions. Common operators like + can be overloaded to perform type-specific operations like adding two Time objects together.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Polymorphism in C++

Polymorphism means having multiple forms of one thing. In inheritance,


polymorphism is done, by method overriding, when both super and sub class have
member function with same declaration but different definition.

Function Overriding in C++


If we inherit a class into the derived class and provide a definition for one of the base
class's function again inside the derived class, then that function is said to
be overridden, and this mechanism is called Function Overriding

Requirements for Overriding a Function


1. Inheritance should be there. Function overriding cannot be done within a class. For
this we require a derived class and a base class.

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.

Example of Function Overriding in C++


class Base
{
public:
void show()
{
cout << "Base class";
}
};

class Derived:public Base


{
public:
void show()
{
cout << "Derived Class";
}
}

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";
}
};

class Derived:public Base


{
public:
void show()
{
cout << "Derived 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.

Different ways to Overload a Function


1. By changing number of Arguments.
2. By having different types of argument.

Function Overloading: Different Number of


Arguments
In this type of function overloading we define two functions with same names but
different number of parameters of the same type. For example, in the below
mentioned program we have made two sum() functions to return sum of two and
three integers.
int main()
{
// sum() with 2 parameter will be called
sum (10, 20);

//sum() with 3 parameter will be called


sum(10, 20, 30);
}
Here sum() function is said to overloaded, as it has two defintion, one which accepts two
arguments and another which accepts three arguments. Which sum() function will be called,
depends on the number of arguments.

// first definition
int sum (int x, int y)
{
cout << x+y;
}

// second overloaded defintion


int sum(int x, int y, int z)
{
cout << x+y+z;
}
Function Overloading: Different Datatype of
Arguments
In this type of overloading we define two or more functions with same name and
same number of parameters, but the type of parameter is different. For example in
this program, we have two sum() function, first one gets two integer arguments and
second one gets two double arguments.

// first definition
int sum(int x, int y)
{
cout<< x+y;
}

// second overloaded defintion


double sum(double x, double 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 - ?:

Implementing Operator Overloading in C++


Operator overloading can be done by implementing a function which can be :

1. Member Function

2. Non-Member Function

3. Friend Function

Operator overloading function can be a member function if the Left operand is an


Object of that class, but if the Left operand is different, then Operator overloading
function must be a non-member function.
Operator overloading function can be made friend function if it needs access to the
private and protected members of class.
Restrictions on Operator Overloading in C++
Following are some restrictions to be kept in mind while implementing operator
overloading.

1. Precedence and Associativity of an operator cannot be changed.

2. Arity (numbers of Operands) cannot be changed. Unary operator remains unary,

binary remains binary etc.

3. No new operators can be created, only existing operators can be overloaded.

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.

Example: overloading + Operator to add two Time


class object
#include <iostream.h>
#include <conio.h>
class Time
{
int h,m,s;
public:
Time()
{
h=0, m=0; s=0;
}
void setTime();
void show()
{
cout<< h<< ":"<< m<< ":"<< s;
}
//overloading '+' operator
Time operator+(time);
};
Time Time::operator+(Time t1) //operator function
{
Time t;
int a,b;
a = s+t1.s;
t.s = a%60;
b = (a/60)+m+t1.m;
t.m = b%60;
t.h = (b/60)+h+t1.h;
t.h = t.h%12;
return t;
}
void time::setTime()
{
cout << "\n Enter the hour(0-11) ";
cin >> h;
cout << "\n Enter the minute(0-59) ";
cin >> m;
cout << "\n Enter the second(0-59) ";
cin >> s;
}
void main()
{
Time t1,t2,t3;

cout << "\n Enter the first time ";


t1.setTime();
cout << "\n Enter the second time ";
t2.setTime();
t3 = t1 + t2; //adding of two time object using '+' operator
cout << "\n First time ";
t1.show();
cout << "\n Second time ";
t2.show();
cout << "\n Sum of times ";
t3.show();
getch();
}

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.

You might also like