473,387 Members | 1,520 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,387 software developers and data experts.

Protected inheritance

Hello!

I wonder why I can't do a cout on a protected member of the base class
called Base. As long the variable has been declared as public or protected I
should be able to acces that varable called number.
I have two classes one class called Base and a derived class called Derived
as youu can see below.

//Main file
********
#include "base.h"
int main()
{
Derived d;
Base b;
d.printInfo(b);
return 0;
}

#include <iostream>
using namespace std;
//Base class
//*********
class Base
{
public:
Base(int num = 0) :number(0){};
protected:
int number;
};
//Derived class
//***********
class Derived : public Base
{
public:
Derived(){}
void printInfo(const Base& b)
{cout << b.number;} //Why can I not do
this??????????????????????????
};

//Thanks
//Tony
Jul 23 '05 #1
3 1377
gaz
"protected" will only give access to the base member data via the
"this" pointer.
You can not access protected data from another base object even if the
function belongs to the derived class.

A simpler solution would be to implement a printInfo function in the
base class and chain it through the hierarchy.

Jul 23 '05 #2

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:mc*********************@newsc.telia.net...
| Hello!
|
| I wonder why I can't do a cout on a protected member of the base class
| called Base. As long the variable has been declared as public or protected I
| should be able to acces that varable called number.
| I have two classes one class called Base and a derived class called Derived
| as youu can see below.
|
| //Main file
| ********
| #include "base.h"
| int main()
| {
| Derived d;
| Base b;
| d.printInfo(b);
| return 0;
| }
|
| #include <iostream>
| using namespace std;
| //Base class
| //*********
| class Base
| {
| public:
| Base(int num = 0) :number(0){};
| protected:
| int number;
| };
| //Derived class
| //***********
| class Derived : public Base
| {
| public:
| Derived(){}
| void printInfo(const Base& b)
| {cout << b.number;} //Why can I not do
| this??????????????????????????
| };

If that was allowed, then anyone could access 'number'
from anywhere, as long as they had an object of type
'Base'.

From inside the 'printInfo' member, just refer to it
without an object as: cout << number; or explicitly
with the this pointer.

Cheers,
Chris Val
Jul 23 '05 #3

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:mc*********************@newsc.telia.net...
Hello!

I wonder why I can't do a cout on a protected member of the base class
called Base. As long the variable has been declared as public or protected
I should be able to acces that varable called number.
I have two classes one class called Base and a derived class called
Derived as youu can see below.

//Main file
********
#include "base.h"
int main()
{
Derived d;
Base b;
d.printInfo(b);
return 0;
}

#include <iostream>
using namespace std;
//Base class
//*********
class Base
{
public:
Base(int num = 0) :number(0){};
protected:
int number;
};
//Derived class
//***********
class Derived : public Base
{
public:
Derived(){}
void printInfo(const Base& b)
{cout << b.number;} //Why can I not do
this??????????????????????????
};


You can't do it because b is a separate object. But why are you TRYING to
do it like that? Why does printInfo require a parameter at all? Just
define it as "void printInfo();", and output the number member. And define
it inside the Base object instead of the Derived object, if you want
Base-type objects to be able to call it. Or, better yet, make it a virtual
function in Base, and, if you want, override it in Derived. That way, the
Base object can output just its number member, while the Derived version can
output the number member (if you want it to) as well as any members of
Derived that are not also in Base.

-Howard
Jul 23 '05 #4

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

Similar topics

5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
3
by: seesaw | last post by:
Compared to "public", how does "private/protected" in inheritance change relationship between classes what is the purpose to define constructor as "private/protected"? is there any usage to...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
3
by: Mark A. Gibbs | last post by:
Good day, i'm having a bit of trouble with a base class i'm working on. this is what it boils down to: template <typename T> class foo { protected: foo() { T* p = static_cast<T*>(this); }
7
by: TT (Tom Tempelaere) | last post by:
Hi there The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale? Are there plans to change this? This feature forces me to make things public...
8
by: Steven Livingstone | last post by:
I know this is not supported, but i'm just looking for some reasoning and perhaps debate. Is there are reason why this is not supported as in the words of manhy a French poet "it's doing my head...
6
by: Jordi | last post by:
I was wondering, in C++ the syntax for inheritance is basically this: class Descendant : public Parent { .... }; Can someone tell me what would happen when I would replace 'public' by...
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
2
by: saneman | last post by:
Are protected fields not only relevant when using inheritance? I am currently changing some code that use inheritance to code that does not use inheritance and don't see the point in keeping the...
2
by: JB | last post by:
Hi All, I've just experienced a "strange" behaviour with inheritance and the Protected keyword. Can someone please shed a light on this? Is this the normal behaviour? Class Base Protected...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.