this is probably the most rediculous question i probably will ever ask..
what are actual uses of polymorphism and inheritance..
this is probably the most rediculous question i probably will ever ask..
what are actual uses of polymorphism and inheritance..
variant data types?
http://cboard.cprogramming.com/showt...threadid=39087
download that file. comment out most of the code in main, then uncomment the huge chunk.
You can even check uninitialized data.
Anytime you need a switch statement based on the type of the object pointed to by the base class pointer.
I have an example of inheritance. I'm working on a game, and in it there are several different objects (items, enemies, bullets, special effects etc...). Most of these use the same kind of data, like all of them have a position, a dimension, a direction etc... Instead of rewriting the same stuff over and over for each type, I made a base class containing that data. Then all of my game objects inherited from that base class, and made som add-ons specific for that type.
It saves time and space.
As for polymorphsm, I have no good example. Basically it lets you use the same names for different arguments.
So instead of typing:
You type:Code:int AddInt(int X, int Y); float AddFloat(float X, float Y); double AddDouble(double X, double Y);
Voila! Using the same name, it's easier to remember them (duh!) and can make the coding easier... maybe...Code:int Add(int X, int Y); float Add(float X, float Y); double Add(double X, double Y);
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Polymorphism allows you to write a function that handles different specified data types in the same intended way. Think about the "cout" object... It allows you to do something like this:
cout << (int)iVal << (double)dVal << (char)ch << (const char*)str;
The reason you can do that is polymorphism... Without it, you would have to do something like this:
cout.printInt(iVal);
cout.printDouble(dVal);
cout.printChar(ch);
cout.printString(str);
Inheritance is a very important part of C++. With it you can create new classes based on existing class without having to recode tons of tried and true code.. Think about if you want to create a specialized button in a gui app. If you had to somehow code a class from scratch that does everything that a button does just so you can add some new functionality, the computer programmer suicide rate would sky-rocket... Instead, you can create a new button class derived from CButton and add the functionality you want to it...
Make sense?
Last edited by LuckY; 05-06-2003 at 01:54 PM.
Look up templates, too. They're even more useful... except my compiler (VC++6.0) has a ton of template related bugs in their code.