Multiple Inheritance
C++ allows a special kind of inheritance known as multiple inheritance.
While most object oriented languages support inheritance, not all of them
support multiple inheritance. (Java is one such example).
Multiple Inheritance simply means that a class can inherit properties
from more than one base class. In other words, a class can
inherit from multiple parent classes -- it's not limited to just one.
Example
- An example of code that used to use multiple inheritance was the
former (older) implementation of the stream libraries for file I/O.
- This old implementation has been replaced by a newer library,
involving template classes
- However, the concept used by the old version should serve to
illustrate the idea
- You should have previously learned about the fstream library,
and these classes:
- ifstream -- input file stream class
- ofstream -- output file stream class
- In the older implementations, there was a class called
fstreambase, which contained member functions relating to
file-handling operations, such as open() and close()
(for opening and closing files).
- In the old implementation, the ifstream class inherited both
from istream (for input features) and from fstreambase
(for file features).
class ifstream : public fstreambase, public istream
{ ..... };
- Similarly, ofstream was inherited from ostream
(output features) and fstreambase (file features)
class ofstream : public fstreambase, public ostream
{ ..... };
Multiple Inheritance and OOP
- Multiple inheritance is not a necessary feature of
object-oriented programming, in general
- Some languages support it, others do not:
- C++ allows multiple inheritance
- Java, for example, only allows single inheritance.
Multiple inheritance code example