If I have an array of pointers to objects of different classes that are derived from the same base class, is this an example of abstraction?
If I have an array of pointers to objects of different classes that are derived from the same base class, is this an example of abstraction?
Sounds more like an example of polymorphism.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for the reply laserlight! But doesn't polymorphism mean that the members of a derived class are treated like those of its parent (or so Wikipedia says), whereas in my case it's the actually object that's treated like its parent, not its members?
The whole thing confuses me somewhat...
Last edited by Nereus; 09-17-2007 at 01:34 PM.
Stroustrup defines polymorphism as "providing a single interface to entities of different types". In your example, suppose that you have a virtual function overriden by the derived classes. Then, by looping over the array, you can call this virtual function, and the overriden versions for each object of a given derived class would be called, even though you appeared to call the base class member function. This is polymorphic behaviour since you accessed different member functions (of the derived classes) from a single interface (the base class virtual member function).But does polymorphism mean that the members of a derived class are treated like those of its parent (or so Wikipedia says), whereas in my case it's the actually object that's treated like its parent, not its members?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Sure, I agree about the use of overridden functions is polymorphism in the way you describe, but isn't the ability to treat the different objects (not their methods) as the same an example abstraction? If not, could you give me an example, as Stroustrup's definition of abstraction has left me non the wiser!
No. The fact that you have user defined objects implies that abstraction. Think of abstraction as in saying "swap a and b" as opposed to "create a temporary t, assign a to t, assign b to a, assign t to b". Here, the implementation of a swap is hidden by the interface of a swap function. In the case of objects, the implementation of the objects is hidden by its interface, so we can talk in terms of a car rather than a specific collection of doors, windows, wheels etc (which themselves may be an example of abstraction, heh).isn't the ability to treat the different objects (not their methods) as the same an example abstraction
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)