Protected inheritance

T

Tony Johansson

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
 
G

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.
 
C

Chris \( Val \)

| 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
 
H

Howard

Tony Johansson said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top