Thread: inheritance and polymorphism uses...

  1. #1

    inheritance and polymorphism uses...

    this is probably the most rediculous question i probably will ever ask..

    what are actual uses of polymorphism and inheritance..

  2. #2
    Registered User
    Join Date
    Sep 2002
    variant data types?

    download that file. comment out most of the code in main, then uncomment the huge chunk.

    You can even check uninitialized data.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Anytime you need a switch statement based on the type of the object pointed to by the base class pointer.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    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:
    int AddInt(int X, int Y);
    float AddFloat(float X, float Y);
    double AddDouble(double X, double Y);
    You type:
    int Add(int X, int Y);
    float Add(float X, float Y);
    double Add(double X, double Y);
    Voila! Using the same name, it's easier to remember them (duh!) and can make the coding easier... maybe...

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    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:

    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.

  6. #6
    Registered User
    Join Date
    Sep 2002
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. polymorphism and inheritance
    By stewie griffin in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2009, 02:45 AM
  2. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 08:47 AM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. Polymorphism, inheritance and containers
    By musicalhamster in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2007, 10:23 AM
  5. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM