You are here:

C++/in multiple inheritence at second level of hierarchy i could nt able to access base class memeber attribute.

Advertisement


Question
#include<iostream.h>

class base{
public:
int i;
};

class derived1: public base
{
public :
int j;
};

class derived2: public base
{
public:
int k;
};

class derived3:public derived1,public derived2 {
public :
int sum;
};

void main()
{
derived3 ob;
base o;
derived1 d1;
ob.base::i=10;//Error
ob.derived1::i=20;//It works fine
ob.derived2::i=30;//It works fine
}


Error: 'base' is not a public base class of 'derived3'

Question:

      Why i coud not able to acces CLASS 'base' i-(ob.base::i) via CLASS 'derived3' object. Here i think CLASS 'derived' will have two i's one from derived1 and from derived2(virtul class concept).My question is not about virtual class concept.why such error?


compiler:

Turboc++
versio 3.0
from Borland internationl.

Answer
Hello sadasivam, thank you for the question.

Yeah, you can't do that. This is why inheritence usually stops after the first level of inheritence. Remember, the third level derived object does not "know" that it has a grandparent class. That's the reason for your error.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.