Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-28-2005, 03:43 PM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default C++ Polymorphism

Hi,
everybody. How to achieve polymorphism in real life application using C++.

For example, using Cellphone we can send and viewText messages, we can speak. Its a polymorphism. How its implememted??????????
Mohan Giri is offline  
Old 03-28-2005, 05:02 PM   #2 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Using a cellphone to both text message and talk isn't an example of polymorphism as a programing concept. In C++, polymorphism involves using pointers to objects which don't necessarily match the objects data type to change (morph) the object into many (poly) different types of objects when you need to make certian code use multiple types of objects without writing code for each possible type.

If you have a class "phone" which inherits from class "communicator", all phones are communicators. When you want to perform a function that is common to all communicators, "send communication", for example, you can use functions which only care aboue the communicator core of the phone. Something that knows what a communicator is, but not what a phone is, can still communicate using phone.

The communicator could be a phone, or a letter, or anything else. If you "send communication" on it, the objects should perform any type-specific actions to fulfil general "send communication" action. Perhaps phone then requests input from a microphone and sends it wirelessly to a cell tower, and letter requests input from a pen and goes to the post office.

In either case, you can use either as a "communicator" to "send communication" without having to write seperate code to communicate using a phone, a letter, or shouting long distances. When you do that, the objects appear to be simple "communicators". They become only what you need them to be. Then, when you want to "dial 555-1212", you can view the communicator phone as phone again, and do just that.

Sorry if this wasn't the best explanation. I haven't tought programming in quite a while.

http://www.cplusplus.com/doc/tutorial/tut4-4.html
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-28-2005, 05:50 PM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

What is mean by Polymorphism. Ability to take many forms. Is it applicable to cellphone or not? (Both text msg & speech)
Mohan Giri is offline  
Old 03-28-2005, 06:00 PM   #4 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Quote:
Originally posted by Mohan Giri
What is mean by Polymorphism. Ability to take many forms. Is it applicable to cellphone or not? (Both text msg & speech)
Not to a cell phone, no. The cell phone's form is constant. You could say communication is polymorphic, though, depending on how you look at it.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-28-2005, 06:08 PM   #5 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

Sorry, I don't understand what you are saying. Can you explain it with some other example?????????
Mohan Giri is offline  
Old 03-28-2005, 06:54 PM   #6 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Quote:
Originally posted by Mohan Giri
Sorry, I don't understand what you are saying. Can you explain it with some other example?????????
Animals need water.

A cat is a animal. A dog is a animal.

When you want to give water to a cat, you don't care that its a cat. You only care that it is an animal and needs water. You also don't care if its a dog. You just give water to an animal.

This is a basic example of polymorphism. You have a cat, but treat it as the more basic animal when giving it water. There are no special cases between giving water to a dog or cat. Either way, water goes in the bowl, which goes on the floor. You don't need to know that the animal is a dog or cat, only that it is an animal.

When you feed either, you treat them differently. Cats get cat food, and dogs get dog food. Cats and dogs are cats and dogs when you need to do something specific to a cat or dog. When you are dealing with something common to animals and general, they are just animals.

When talking about polymorphism, the term polymorphism actualy shouldn't be used. "Selective masking" is a bit more appropriate. If a person is male or female, child or adult, they are still a person. If you need to see if the person should go into the men's or woman's restroom, you only care if they are male or female. You don't care about them being an adult or child. If you want to see if someone can vote, you only look at the adult/child aspect, you don't look at the male/female part.

As far as programming is concerned, polymorphism involves treating different objects as the same type of object sometimes, and their individual selves at others.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-31-2005, 06:24 PM   #7 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

U mean polymorphism not only apply to cellphone. Its generally apply to communicator devices?????
Mohan Giri is offline  
Old 03-31-2005, 07:07 PM   #8 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

An operation that has different meanings depending on which object it is bound to at run-time is called a polymorphic operation. Polymorphism is the ability of objects of different classes related by inheritance to respond differently to the same member function caller.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 03-31-2005, 07:43 PM   #9 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

How to accomplish it in real life problems or situations???????
Mohan Giri is offline  
Old 04-01-2005, 12:05 AM   #10 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Ok you have a high school student and a college student. They are both students but a college student is not a high school student and vice versa. I could ask both the same question and get a different response. That is like calling a function which both have in their class but you get a different response depending on who answers the question.

To be able to do this in our program we use Dynamic binding. Dynamic binding takes place when a member function is declared as virtual. When we ask the question, the ask function will be defined as virtual in our base class of type student. We don't know who will answer so we make sure that it can handle both answers. With Dynamic binding (late binding) the compiler will run through and determine who's version of ask to call as the program runs.

Dynamic binding is nice because you don't have to think up ten different print function names that do basically the same thing but are slightly different for each class.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On