[Cs3] upcasting downcasting

Mikhail Nesterenko mikhail at cs.kent.edu
Sun Nov 5 19:16:27 EST 2017


> class Square: public Figure
> 
> 
> For this line I realize I don't understand what the purpose of public
> is. I realize we are saying that class Square is a derived class from
> class Figure, but what does it mean to write public there? Are there
> instances pertinent to this course where we would write protected or
> even private(which probably doesn't make any sense).

it very much does. We have just studied Adapter Design Pattern where
Class Adapter used private inheritance. Specifically Adapter privately
inherited from Adaptee while publicly inheriting from Abstract
Interface. Adapter used features of Adaptee to implement the Interface.

Here is the example code:

   http://vega.cs.kent.edu/~mikhail/classes/cs3/Examples/Adapter/AdapterClass.cpp

Notice how SquareAdapter privately inherits from LegacyRectangle. 

In general public inheritance specifies is-a relation between base and
derived class while private inheritance specifies
implemented-in-terms-of relation.

Thanks,
--
Mikhail


More information about the cs3 mailing list