Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

What's the difference between overloading and polymorphism

polymorphism

  • Please log in to reply
7 replies to this topic

#1 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 14 October 2011 - 12:24 PM

Hi

The passage quoted below is from a book. I'm still not clear about the distinction between 'polymorphism' and 'overloading'. Could you please let me know how to differentiate between the two? Thanks.


Using operators or functions in different ways, depending on what they are operating on, is called polymorphism (one thing with several distinct forms). When an existing operator, such as + or =, is given the capability to operate on a new data type, it is said to be overloaded. Overloading is a kind of polymorphism; it is also an important feature of OOP.


  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 14 October 2011 - 12:46 PM

I guess you can say overloading is polymorphism for operators. Polymorphism is general to all kinds of functions while overloading is specific to operators.

---------- Post added at 04:46 PM ---------- Previous post was at 04:45 PM ----------

First line from Wikipedia page for "Operator overloading":

In object oriented computer 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.


  • 0
Latinamne loqueris?

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 14 October 2011 - 04:16 PM

Polymorphism is allowing a variable to have different behaviors depending on which version is implemented. A poodle is a kind of dog is a kind of mammal. A Persian is a kind of cat is a kind of mammal. You use Polymorphism to implement a poodle or a Persian as the object referenced by a pointer to mammal. Polymorphism means it will behave as what it is, not as a generic mammal.

Overloading is the process of creating several versions of the same function, generally distinguished by the parameters. That lets you do things like create print(int var), print(int_matrix var), etc. and have each print the object in an appropriate manner.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 14 October 2011 - 07:35 PM

I guess you can say overloading is polymorphism for operators. Polymorphism is general to all kinds of functions while overloading is specific to operators.[COLOR="Silver"]


Hi Collin

The book I use has a topic title "Overloaded Functions". So, according to the book, functions can also be overloaded. By the way, the passage in my last posting was also taken from the same book.

Polymorphism is allowing a variable to have different behaviors depending on which version is implemented. A poodle is a kind of dog is a kind of mammal. A Persian is a kind of cat is a kind of mammal. You use Polymorphism to implement a poodle or a Persian as the object referenced by a pointer to mammal. Polymorphism means it will behave as what it is, not as a generic mammal.

Overloading is the process of creating several versions of the same function, generally distinguished by the parameters. That lets you do things like create print(int var), print(int_matrix var), etc. and have each print the object in an appropriate manner.


Hi WP

I appreciate your help. I'm sorry but I'm still unclear about the distinction between 'overloading' and 'polymorphism'. Could you please elaborate it a bit? It would be kind of you. Thanks.

Best wishes
Jackson
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 15 October 2011 - 02:32 PM

Polymorphism involves having parent/child classes.

Overloading involves having multiple versions of the same function name. Note: for these purposes, an operator is a strangely written function. Overloading can occur completely outside the context of classes.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 17 October 2011 - 04:31 PM

Polymorphism involves having parent/child classes.

Overloading involves having multiple versions of the same function name. Note: for these purposes, an operator is a strangely written function. Overloading can occur completely outside the context of classes.


Hi WP

It is said overloading is a kind of polymorphism. I understand what overloading is. I haven't read about parent and child classes yet but I just checked my book it doesn't say anything about polymorphism in that topic. Interestingly, it doesn't say anything in detail about polymorphism anywhere in the book. At one place it only says that polymorphism is one of the cornerstones of C++.

I still don't understand the distinction between overloading and polymorphism. I understand overloading is subset of polymorphism. But what is polymorphism in itself in the context of C++? If it's not possible to translate this complex idea into simple words, then it's okay. Thanks a lot.

Regards
Jackson
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#7 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 17 October 2011 - 05:43 PM

Taken from wikipedia:
class Animal {
public:
        Animal(const string& name)
			: name(name)
		{
		}
		
        virtual string talk() = 0;
		
        const string name;
};
 
class Cat : public Animal {
public:
        Cat(const string& name)
			: Animal(name)
		{
		}
		
        virtual string talk() {
			return "Meow!";
		}
};
 
class Dog : public Animal {
public:
        Dog(const string& name)
			: Animal(name)
		{
		}
		
        virtual string talk() {
			return "Woof! Woof!";
		}
};
This code demonstrates inheritance (Animal is base/parent class, Dog and Cat are derived/child classes) and polymorphism (method talk() has the same signature/header but behaves differently for each animal; cats don't bark).
  • 0

The roots of education are bitter, but the fruit is sweet.


#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 18 October 2011 - 05:37 AM

It sounds like you are using a poor source. Polymorphism is a concept in object-oriented programming where items from lower levels in a class hierarchy can be used in place of objects at higher levels, but still use their refined methods. If you haven't studied inheritance in classes, the concept really won't make much sense.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download