Inheritance

L

Lionel

Quick and easy question.

If we have classes A, B and C. C extends/inherits B and B extends A. I
have discovered C++ doesn't like C using protected members of A, what do
I need to do so that C can use these? Hopefully not make them public.

Thanks,

Lionel.
 
A

Alf P. Steinbach

* Lionel:
If we have classes A, B and C. C extends/inherits B and B extends A. I
have discovered C++ doesn't like C using protected members of A,

Sorry, that description doesn't say anything about your problem: post code.
 
L

Lionel

Alf said:
* Lionel:



Sorry, that description doesn't say anything about your problem: post code.

I'm going to have to make something up then because the code is
sensitive for a start and there is too much of it! I thought this might
have been a straight forward problem but let's try this analogy.

class A {
public:
A();
protected:
int aVariable;
};

******************************
#inlcude "a.h"

class B : A {
public:
B();
};

***********************
#include "b.h"

class C : B {
public:
C();
void someFunction();

};

***************** Implementation of C
C::C() : B() {}

void C::someFunction() {
cout << aVariable << endl;
}


The error I get is along these lines:

c.h: "line number": error: `int A::aVariable' is protected
c.cpp:"line number": error: within this context



However, if I make aVariable public in A then I get this error:


a.h:"line number": error: `int A::aVariable' is inaccessible

Is this sufficient?

Thanks,

Lionel.
 
J

John Carson

Lionel said:
I'm going to have to make something up then because the code is
sensitive for a start and there is too much of it! I thought this
might have been a straight forward problem but let's try this analogy.

class A {
public:
A();
protected:
int aVariable;
};

******************************
#inlcude "a.h"

class B : A {
public:
B();
};

***********************
#include "b.h"

class C : B {
public:
C();
void someFunction();

};

There are three kinds of inheritance: private, protected and public. With
classes, the default is private inheritance, which means that both the
protected and public members of the base class become private members of the
derived class.

To make protected and public members of the base class preserve their status
in the derived class (i.e., be protected and public respectively in the
derived class), you need to use public inheritance. Thus you should write:

class B : public A {
public:
B();
};

class C : public B {
public:
C();
void someFunction();
};
 
A

Alf P. Steinbach

* Lionel:
I'm going to have to make something up then because the code is
sensitive for a start and there is too much of it!

Then you should really make an example that compiles, or that demonstrates
the compilation error. And copy+paste that code, no manual writing. But
OK, this time... ;-)

I thought this might
have been a straight forward problem but let's try this analogy.

class A {
public:
A();
protected:
int aVariable;
};

******************************
#inlcude "a.h"

class B : A {

This is private inheritance, is that what you intended?

public:
B();
};

***********************
#include "b.h"

class C : B {
public:
C();
void someFunction();

};

***************** Implementation of C
C::C() : B() {}

void C::someFunction() {
cout << aVariable << endl;

Can't access private members of B.
}


The error I get is along these lines:

c.h: "line number": error: `int A::aVariable' is protected
c.cpp:"line number": error: within this context

Advice: be technically accurate. Check whether your error message actually
says 'protected'. Doesn't it say, 'private', or just 'inaccessible'?
 
L

Lionel

Alf P. Steinbach wrote:
[...]
Then you should really make an example that compiles, or that demonstrates
the compilation error. And copy+paste that code, no manual writing. But
OK, this time... ;-)


I thought this might
have been a straight forward problem but let's try this analogy.

class A {
public:
A();
protected:
int aVariable;
};

******************************
#inlcude "a.h"

class B : A {


This is private inheritance, is that what you intended?


[...]
The error I get is along these lines:

c.h: "line number": error: `int A::aVariable' is protected
c.cpp:"line number": error: within this context


Advice: be technically accurate. Check whether your error message actually
says 'protected'. Doesn't it say, 'private', or just 'inaccessible'?

Right with you, thanks for the help I won't have that problem again.

It did say protected, I copied and pasted then renamed, I'm not sure how
I missed the keyword private when I copied and pasted, but it is
certainly there in my error message.

I've mostly programmed in Java hence these sorts of mistakes seem to
come up regularly in C++ but hopefully one day there will be no more
surprises :).

Lionel.
 
L

Lionel

John said:
There are three kinds of inheritance: private, protected and public.
With classes, the default is private inheritance, which means that both
the protected and public members of the base class become private
members of the derived class.

To make protected and public members of the base class preserve their
status in the derived class (i.e., be protected and public respectively
in the derived class), you need to use public inheritance. Thus you
should write:

class B : public A {
public:
B();
};

class C : public B {
public:
C();
void someFunction();
};

Thanks, you nailed it ;).

Lionel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top