0% found this document useful (0 votes)
118 views14 pages

Compile Time Polymorphism

Compile time polymorphism refers to techniques like function and operator overloading that allow the same function or operator to have different implementations depending on the types of its arguments. This is handled at compile time rather than runtime. It has zero runtime cost but increases developer turnaround time due to longer compiles. Runtime polymorphism using inheritance and virtual functions relies on binding functions at runtime through pointers and references. Compile time polymorphism using templates can increase performance by avoiding unnecessary virtual function binding while still providing flexibility, though it may not be suitable for every situation.

Uploaded by

Arpita Nayyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views14 pages

Compile Time Polymorphism

Compile time polymorphism refers to techniques like function and operator overloading that allow the same function or operator to have different implementations depending on the types of its arguments. This is handled at compile time rather than runtime. It has zero runtime cost but increases developer turnaround time due to longer compiles. Runtime polymorphism using inheritance and virtual functions relies on binding functions at runtime through pointers and references. Compile time polymorphism using templates can increase performance by avoiding unnecessary virtual function binding while still providing flexibility, though it may not be suitable for every situation.

Uploaded by

Arpita Nayyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPILE TIME

POLYMORPHISM
WHAT IS POLYMORPHISM??
>> Polymorphism: from the Greek having
multiple forms

>> In programming languages, the ability to
assign a different meaning or usage to
something in different contexts.

>> Polymorphism is the ability for a variable
(of a superclass type) to contain different
objects of a subclass type at different
points in time

Kinds of Polymorphism
Runtime/Dynamic polymorphism
(Based on virtual methods)

Compile time/Static polymorphism
(Based on templates)

These 2 are largely orthogonal techniques,
but many ways where one could be
replaced with the otherand thats where
the confusion lies.
DIFFERENCE..?
Compile time
polymorphism is
done using functions
and operators
overloading.


Runtime time
polymorphism is
done using
inheritance and
virtual functions.


COMPILE TIME
POLYMORPHISM
Compile time polymorphism includes:-

1) Function Overloading

2) Operator Overloading
Function Overloading

Function overloading means two or more
functions can have the same name but either
the number of arguments or the data type of
arguments has to be different, also note that
return value has no role because function will
return a value when it is called and at compile
time we will not be able to determine which
function to call.
Lets see an Example..!
OUTPUT
Here is int 10
Here is float 10.1
Here is char* ten
#include <iostream>
using namespace std;

void print(int i) {
cout << " Here is int " << i << endl;
}

void print(double f) {
cout << " Here is float " << f <<
endl;
}

void print(char* c) {
cout << " Here is char* " << c <<
endl;
}

int main()
{
print(10);
print(10.10);
print("ten");
}

Operator Overloading
Operator overloading is the ability to tell the
compiler how to perform a certain operation
when its corresponding operator is used on
one or more variables.
In object oriented programming, operator
overloading,less commonly known as
operator ad-hoc polymorphism,is a specific
case of polymorphism, where different
operators have different implementations
depending on their arguments.
Operator overloading is generally defined by
the language, the programmer, or both.

The number of arguments in the overloaded
operators argument list depends on two
factors:

1)Whether its a unary operator (one
argument) or a binary operator (two
arguments).

2)Whether the operator is defined as a global
function (one argument for unary, two for
binary) or a member function (zero arguments
for unary, one for binary the object becomes
the left-hand argument).
Another Example
#include<iostream.h>
#include<conio.h>

class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value
of Complex Numbers a,b:";
cin>>a>>b;
}

complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}

void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};

void main()
{
clrscr();
complex obj1,obj2,result;

obj1.getvalue();
obj2.getvalue();

result = obj1+obj2;

cout<<"Input Values:\n";
obj1.display();
obj2.display();

cout<<Addition:";
result.display();

getch();
}



OUTPUT:

Enter the value of Complex Numbers
a, b
4 5
Enter the value of Complex Numbers
a, b
2 2

Input Values
4 + 5i
2 + 2i

Addition:
6 + 7i


Cost of compile time
polymorphism

ZERO runtime cost (Abstraction without
abstraction penalty)

However developer turn around time
increased due to longer compiles
(This could be avoided too)
Summary

Using templates for compile-time polymorphism
can increase performance when they are used
to avoid needless virtual function binding. With
careful design, templates can be used to give
non-virtual classes all the capabilities that virtual
classes have, except for runtime binding.
Although such compile-time polymorphism is
not appropriate for every situation, a careful
decision by the programmer as to where virtual
functions are actually needed can dramatically
improve code performance, without incurring a
loss of flexibility or readability.

You might also like