Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,386 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,209 people online right now. Registration is fast and FREE... Join Now!




Polymorphism

 
Reply to this topicStart new topic

Polymorphism

sumlani
post 31 Aug, 2008 - 04:59 AM
Post #1


New D.I.C Head

*
Joined: 26 Jan, 2008
Posts: 26



Thanked 3 times
My Contributions


How does polymorphism allow for programming in the general form rather than the specific form?
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 Aug, 2008 - 07:12 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Polymorphism allows you to program in the general form by boiling down characteristics and functionality common to multiple different classes which derive from, and share, the same base type. This allows you to program code generic to all class types and overriding only in specific types of subclasses. The best way to show this is with a brief example...

Consider a Car class. A 67 mustang, a 1996 accent, and a 1964 Chevy Impala are all types of cars, but have their own unique characteristics. Polymorphism allows us to create a function like Drive(), which is common to all Cars and is the same (in theory) and place it into the base class, a generic form in our Car base class. All of our classes then inherit the basic principle of Drive... a generic form of the activity. But as you may or may not know, the 1996 accent had the gear shifter on the middle console, so the instructions to drive would be a bit different than the other classes who may have their gear shift handle on the steering column. So for our Accent class we can create our own version of Drive which we override the base class and implement the specific form that takes the shifter on the console.

So Drive in our base class would be designed as generic that could apply to all subclasses and has functionality that is shared. By doing this we cut down on repetitive code (because we don't have to apply the same code to each subclass) and we provide basic functionality.

Hopefully this makes sense. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month