#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    What's the point of polymorphism?


    I get it well, still studying it, but I just wrote up this code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Numbers {
    protected:
    	int begin;
    	int end;
    public:
    	void assign(int x,int y)
    	{
    		begin = x;
    		end = y;
    	}
    	virtual void Increment(int up)
    	{
    		int i = begin;
    		for(;begin < end;begin++)
    		{
    			cout << begin << endl;
    		}
    		begin = i;
    		cout << "i liek # " << up << endl;
    	}
    };
    
    class increase:public Numbers
    {
    public:
    	void Increment(int up)
    	{
    		int i = begin;
    		for(;begin < end;begin += up)
    		{
    			cout << begin << endl;
    		}
    		begin = i;
    	}
    };
    
    int main()
    {
    	increase x;
    	x.assign(2,10);
    	Numbers &y = x;
    	y.Increment(2);
    	getchar();
    	return 0;
    }
    works fine. Does what I want and if I remove the virtual keyword near the function in the class then it runs the function in the Numbers class which I get. The only question I have is why can't I just change the name of the function in the increase class? What's the use of polymorphism?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,692
    Rep Power
    1958
    If you haven't already read about it, I suggest you take a look at Polymorphism and Virtual function.

    Basically, IMO, it is a concept to make a standard for a range of object that has similar functionality but still different in how it is done.
    A classic example is the Animal object; you don't know what animal will be needed/available, but you know that all animals should be able to eat, move and make a sound.

    There is a code example for this in the link above.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    A couple decades ago I was playing around with a MS-DOS graphics program (yes, I wrote it in Borland C++). I wanted to have several objects of different shape moving off in random directions and bouncing off the "wall" (ie, the sides of the screen).

    So I declared a Shape base class with virtual methods Move, Draw, Erase (which is just Draw with the background color). All things that a space would do. From Space I derived Square, Zircle (Circle was already taken by the graphics library, I seem to recall), Triangle, and Star. Then I gave each one its own Draw method. Then I created about 10 shapes of various shapes, colors, and sizes. Then periodically I had them all move.

    By using polymorphism, I had one single array of Shape with each shape being a different shape. All I needed was a simple for-loop calling the Move method of each shape in that array and because of polymorphism each object in that Shape array knew which Draw method to call. Slick and easy.

    Without polymorphism, it would have been a mess. Or at least nowhere near as slick and neat and cool. I guess I would have had to have created separate arrays for each different possible shape, one for circles and another for squares and another for triangles and another for stars. And whenever I would have to handle a specific shape object I would also have to explicitly figure out what kind of shape it was so I could handle it explicitly.

    But polymorphism takes care of all that.

    Comments on this post

    • MrFujin agrees
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    Originally Posted by miz6565
    I get it well,[...]
    I am not sure that you do "get it". There is a world of difference betweem merely knowing the syntax and mechanism of polymorphism and actually understanding polymorphism. Your example employs the language feature of polymorphism perhaps, but for no useful purpose. Such and example is bound to fail to expose the usefulness of polymorphism.

    Originally Posted by miz6565
    The only question I have is why can't I just change the name of the function in the increase class? What's the use of polymorphism?
    That's two questions! :rolleyes:

    In this case you could change the name in the sub-class, but that is not the point. It is hard to discuss your example because it is a poor example of OOD/OOP let alone polymorphism. A class is a "type of thing", and as such should generally be named with a noun. Member functions "do stuff" so should generally be names with a verb. In this case "increase" is a nonsensical name for a class.

    In an abstract sense, say I have a class A and a subclass of A called B. Now say I pass a B object to an interface that accepts a A object, I can do that, the code receiving the object does not need to know that it is a B object in order to use the interface exposed by A, but when it does so the actions defined for B will occur.

    As a practical example, imagine a robot with sensors that provide distance and direction. The actual sensors might be ultrasonic, IR, bump-switches, LIDAR or any other technology. Using polymorphism one could define a generic interface class providing distance and direction. The code that processes and responds to detected physical objects in the environment need not be concerned with the sensor technology and will use the generic sensor interface while the device driver code for each sensor type will use the sensor specific specialisations.

    It's about the concepts of generalisation and specialisation, any example that does not include those concepts in any useful sense (like yours) is not really going to help clarify this in your mind.

IMN logo majestic logo threadwatch logo seochat tools logo