Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

difference between overloading and polymorphism

Status
Not open for further replies.

LBdgWgt

Member level 5
Joined
Mar 6, 2006
Messages
83
Helped
5
Reputation
10
Reaction score
0
Trophy points
1,286
Activity points
2,066
Hi all,

I am quite used to program in C, but for OOP i dont have the concept quite deep yet. I have (maybe) a simple question: what is the difference between (function) overloading concept in C and polymorphism in C++? as i noticed, they have almost the same purposes: to provide single definition (name) for many types of data. but i also noticed that in polymorphism, it is not just for functions, but also for operators. any other differences?

thanks for the answer,
 

Actually, overloading and polymorphism is the same thing ……….
Let me give you an example:
Say you have a function that prints what you send to it (int, char, …..)

In case you don’t use overloading (polymorphism) you will need to make a new function (a different function) for each type:

void displayOneInt (int x)
{
cout<<x;
}

void displayOneChar (char c)
{
cout<<c;
}

void displayTwoInt (int x, int y)
{
cout<<x <<y;
}

void displayThreeChar (char c1, char c2, char c3)

{
cout<<c1<<c2<<c3;
}

and so on………..

But in case you use overloading (polymorphism) you will have one function ( the same name) but with different implementations:

void display (int x)
{
cout<<x;
}

void display (int x, int y)
{
cout<<x<<y;
}

void display (char c)
{
cout<<c;
}
void display (char c1, char c2)
{
cout<<c1<<c2;
}

as you can see, in this case all you have to do is call the function “display” from the main and give it one int or two int or a char and so on, and the compiler will map your call the correct function prototype. That is what we call overloading or polymorphism.

About the operators’ part …….. The operators are functions too. Only they are not written by you ……. so they are encapsulated for protection (OOP core feature). So, you can overload them as well but you can’t edit their original code. So basically you are doing the same thing as the example above.
I hope that answers your question. :D
 

Overloading is an implementation of polymorphism.

Polymorphism is an abstract concept.
 

Hello Friend,


Actually Polymorphism is the concept in which we have a function name is same but either Argument or return type is different. Overloading is practical approach to achieve Polymorphism.


Have a nice day.......
 

Polymorphism means one entity existing in multiple forms. It provides flexibility to application systems. It simplifies coding and reduces the rework involved in modifying and developing an application. It refers to the ability of an object to take on different forms depending upon the situation.

According to the polymorphism design principle, the same message sent to different objects results in different behavior. The behavior depends on the type of object that receives the message.

Example:
Every key of a keyboard performs a specific action when a keystroke message is generated for that key. However, by using polymorphism, the same code with a small change can be used by different keys of the keyboard to trigger specific actions.

polymorphism can be used inheritance to clear concept make a base class and in it define a function name CanEat(); then make others classes which inherit the base class Override this function in every class in Main function create the object of each class convert each object to base type then call each function with base reference now same code is required to call function of each class but every Make different result.

but like overloading is only the functions with same name but different parametre so these two are quite different Concepts.

any confusion send private message .
 

overloading is a static polymorphism or early binding.
early binding means knowing execution procedure at compile time.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top