C++ Polymorphism

Status
Not open for further replies.

Mohan Giri

In Runtime
Messages
144
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??????????
 
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
 
What is mean by Polymorphism. Ability to take many forms. Is it applicable to cellphone or not? (Both text msg & speech)
 
Mohan Giri said:
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.
 
Sorry, I don't understand what you are saying. Can you explain it with some other example?????????
 
Mohan Giri said:
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.
 
U mean polymorphism not only apply to cellphone. Its generally apply to communicator devices?????
 
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.
 
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.
 
Status
Not open for further replies.
Back
Top Bottom