Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is just what subclassing does: adds functionality not present in a base class.</p> <pre><code>class MyVehicle : public Vehicle { public: void MyNewFunction() ... </code></pre> <p>There are two (really just) different flavors of inheritance: public and private, reflecting the Is-A and Has-A relationships respectively. With public inheritance, you're directly adding stuff to a class. If I have class Animal with methods Eat() and Walk(), I may make a subclass called Cat which has the method Purr(). A Cat then has public methods Eat, Walk, and Purr.</p> <p>In the case of a Stack based on a LinkedList however, I may say that a Stack HAS-A LinkedList internally. As such, I do not expose any features of the base class publically, I retain them as private and have to explicitly offer whatever I choose as public. A list may have a method Insert(), but for the Stack, I restrict the implementation and rexpose it as Push(). No previous public methods are exposed.</p> <p>In C++, this is defined by the access modifier given before the base class. Above, I'm using public inheritance. Here, I use private inheritance:</p> <pre><code>class MyVehicle : private Engine { </code></pre> <p>This reflects that MyVehicle HAS-An Engine.</p> <p>Ultimately, subclassing takes everything available in the base class and adds new stuff to it. </p> <p><strong>EDIT:</strong> With this new information it seems that you're really looking for, it seems, is interfaces as stated by an earlier (voted down) comment. This is one of the big problems with inheritance - granularity. One of C++'s big complaints is its implementation of multiple inheritance (an option to accomplish this.) Can you state specifically what language you're using so we can advise properly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload