473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Inheritance

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.
Sep 5 '05 #1
6 3466
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 5 '05 #2
Alf P. Steinbach wrote:
* 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.


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.
Sep 5 '05 #3
"Lionel" <li******@gmail.com> wrote in message
news:df***********@bunyip2.cc.uq.edu.au
Alf P. Steinbach wrote:
* 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.


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();
};
--
John Carson

Sep 5 '05 #4
* Lionel:
Alf P. Steinbach wrote:
* 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.


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'?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 5 '05 #5
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.
Sep 5 '05 #6
John Carson wrote:
"Lionel" <li******@gmail.com> wrote in message
news:df***********@bunyip2.cc.uq.edu.au
Alf P. Steinbach wrote:
* 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.

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();
};


Thanks, you nailed it ;).

Lionel.
Sep 5 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.