C++ Polymorphism with no inheritance

Intro

Working Wandbox-ed modern C++ code, is HERE.

C++ Polymorphism with no inheritance is by no means, somewhat advanced concept. But, this is just because it is (in C++) based on different concepts v.s. some other good and popular languages where it is in the foundations  of the language design.

For example in GO LANG polymorphism is definitely not tied to inheritance. And this is one very much popular and good programing language, indeed.

I have prepared “earlier” one (I hope) succinct and useful GO LANG example of interfaces, types and messages dancing together.

GO is simple and that is interactive on line code. So do not be afraid and at least try it once. I have tried to comment it extensively

GO LANG OO concepts are inheriting (pun intended) from SmallTalk. Not from C++ or Simula. GO interface, can be seen conceptually as collection of messages.

After the above, GO structs (types) can implement the message “Start” . They do not inherit the interface in order to implement it, like one does in C++. In GO you implement the message (aka method) that GO interface exhibits.

 

There is no interface keyword in C++. Usually C++ interface is Abstract Base Class (ABC). Admittedly (at least to me) GO LANG ABC’s are unnecessarily complex subject and we better quickly jump back to standard C++ we know and love (well at least some of us). 

Slight Detour

Subtyping is actually what C++ developers think of as polymorphism. Common base class as a parent to subtypes implementing the same interface of the said common base.

Parametric Polymorphism is what C++ developers call “generic programming” . In “typeless” languages (think Java Script) that kind of polymorphism is actually transparent. It just happens. (some are calling this ‘delegation’ but I do not think that is the right name to use)

some_object above is not any kind of base class. It is “just” an object. In JavaScript an collection (dynamic array) of functions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.