#### Archived

This topic is now archived and is closed to further replies.

# Confusion with inheritance

This topic is 5798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just started learning Class Inheritance a few days ago and the book i''m reading hasn''t been too clear on it. First I''ll paste my test code which sets up my problem:

// Inheritance.cpp

#include <iostream.h>

class level1 {
public:
int test;
level1():test(1) {}
};

class level2a : public level1 {
public:
int test;
level2a():test(2) {}
};

class level2b : public level1 {
public:
int test;
level2b():test(2) {}
};

class level3 : public level2a, public level2b {
public:
int test;
level3():test(3) {}
};

int main() {
level3 TestClass;

//cout << "test from level1 is set to " << TestClass.level1::test << "\n"; // Ambiguous

cout << "test from level2a::level1 is set to " << TestClass.level2a::level1::test << "\n";
cout << "test from level2b::level1 is set to " << TestClass.level2b::level1::test << "\n";

return 0;
}

I know that if level2a and level2b defined their inherited class as virtual it would fix the compile problem, but what I can''t understand is why there is a compile problem at all? Isn''t ''::'' a sort of inherited-class accessor similar to ''.''? If not, then how do I access the level1.test from either levels specifically? NOTE: The code above is meant for DOS.

##### Share on other sites
quote:
Original post by Shambles
I know that if level2a and level2b defined their inherited class as virtual it would fix the compile problem...

No, it wouldn''t. You should declare virtual destructors though.

quote:
Original post by Shambles
...but what I can''t understand is why there is a compile problem at all? Isn''t ''::'' a sort of inherited-class accessor similar to ''.''?

Nope. ''::'' is the scope resolution operator, meaning it allows you to access elements within a specific namespace inlcuding the relative global namespace (namespace outside current), which it can access anonymously.

quote:
Original post by Shambles
If not, then how do I access the level1.test from either levels specifically?

You have a very big problem. level2a and level2b are okay (sorta), but with level3 you experience the dreaded "diamond" inhertiance, where a class inherits a property twice through two base classes which themselves have a common base class. This creates a reference ambiguity. One solution is to use protected inheritance, but that raises issues of its own, though none of them affect your example.

quote:
Original post by Shambles
NOTE: The code above is meant for DOS.

First, it makes no difference. Second, it probably isn''t; it''s probably meant for the Win32 console, which you erroneously think is DOS.

##### Share on other sites
quote:
Original post by Oluseyi
[quote]Original post by Shambles
I know that if level2a and level2b defined their inherited class as virtual it would fix the compile problem...

No, it wouldn''t. You should declare virtual destructors though.

Actually it does work, but maybe I explained it wrong.. If I do:

  class level2a : virtual public level1 {//...class level2b : virtual public level1 {

it allows only one instance of level1 as an inherited class, which will allow me to access it directly from level3.

quote:
Original post by Oluseyi
[quote]Original post by Shambles
NOTE: The code above is meant for DOS.

First, it makes no difference. Second, it probably isn''t; it''s probably meant for the Win32 console, which you erroneously think is DOS.

Yeah, I though Win32 console was DOS... The only type of projects I''ve tried coding for this far is Win32 and Win32 Console.

Thanks for your help though. I guess I know why Java left out multiple inheritance. The creator(s) of C++ should have thought more about this.

##### Share on other sites
Here are a couple of good reads on Multiple Inheritance...

http://www.gotw.ca/gotw/037.htm
http://www.gotw.ca/gotw/038.htm

Also, get rid of that <iostream.h> and just use <iostream>. Also remember that cout is in the std:: namespace in <iostream>.

How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631714
• Total Posts
3001861
×