Indirect Inheritance

In addition to multiple inheritance, you will also encounter situations where you will inherit from a class that is, in turn, inheriting from another class. In fact, later in this book, when we get to Visual C++, you will see a lot of this. Essentially if your class is derived from a class, you inherit all its protected and public members, even if those members were inherited from yet another class. You can have inheritance going back any number of classes and you will have inherited it all.

Example 12.5

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using namespace std; class baseclass1 {  public:    void basefunc1(); }; void baseclass1::basefunc1() {  cout << "This is in the first base class \n"; } class baseclass2: public baseclass1 {  public:    void basefunc2(); }; void baseclass2::basefunc2() {   cout << "This is in the second base class \n"; } class baseclass3: public baseclass2 { public:   void basefunc3(); }; void baseclass3::basefunc3() {   cout << "This is in the third base class\n"; } class derivedclass : public baseclass3 { }; int main ()  {  derivedclass myclass;   myclass.basefunc1();   myclass.basefunc1();   myclass.basefunc3();     return 0; }

Step 2: Compile and execute your program.

You should see something much like what is depicted in Figure 12.4.

click to expand
Figure 12.4: Indirect inheritance.

You can see that the derived class you actually used has access to all the public functions it inherited from each class. This example shows several layers of indirect inheritance.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: