February 21st, 2012, 10:32 AM
Re: Simple question - mem addresses
Perhaps a different example would make things clearer. Consider a Shape base class that has an area() virtual function, and two separate derived classes, Circle and Rectangle. These two types of Shapes need to use different equations to compute their area(), but both of them have a concept of area. So if you wanted to write a function that needed the area of a shape, but didn't care what kind of shape it was other than that, you only need to write one function (taking a Shape* parameter), instead of needing to write two separate functions (one for Circle and one for Rectangle) which are largely identical except for the type.
Originally Posted by mm12463
Incidentally, the above inheritance example could be re-posed as a template example without too much trouble. The major difference between inheritance and templates is: If the actual type of an object (eg, Rectangle or Circle) can be known at compile time, use templates; if it cannot be known until runtime, use inheritance.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.