C++ vs Java : Problem with Inheritence

V

vj

Hello Group,

I am C++/OOP newbie and was working on a project when i came accross
this puzzleing problem with inheritence.

C++ Code
==================

class Parent
{
protected:
int a;
};

class Child:public Parent
{
public:
int getA(Parent & p)
{return p.a;} //!!Compile time error:: Parent::a in-accessible
};

Java Code
=====================
class Parent
{
protected:
int a;
}

class Child extends Parent
{
public:
int getA(Parent p)
{return p.a;} //!! No Error
}
//************************************

Can anybody please tell me why c++ is not behaving the Java way. After
all 'Child' class was derived from 'Parent' class and thus should have
access to all the proctected members in objects of class 'Parent'.

Java is doing this fine but C++ is NOT!!


Thanks,

VJ
 
A

Alf P. Steinbach

* vj:
I am C++/OOP newbie and was working on a project when i came accross
this puzzleing problem with inheritence.

C++ Code
==================

class Parent
{
protected:
int a;
};

class Child:public Parent
{
public:
int getA(Parent & p)
{return p.a;} //!!Compile time error:: Parent::a in-accessible
};

C++ 'protected' grants access to member functions of Parent and of derived
classes, for the '*this' object.

In your example 'p' could be a Xulcu, derived from Parent.

So if the access was not forbidden you could access the innards of any Xulcu
object just by deriving from Parent, and then it wouldn't be much of a
protection.
 
J

John Harrison

vj said:
Hello Group,

I am C++/OOP newbie and was working on a project when i came accross
this puzzleing problem with inheritence.

C++ Code
==================

class Parent
{
protected:
int a;
};

class Child:public Parent
{
public:
int getA(Parent & p)
{return p.a;} //!!Compile time error:: Parent::a in-accessible
};

Java Code
=====================
class Parent
{
protected:
int a;
}

class Child extends Parent
{
public:
int getA(Parent p)
{return p.a;} //!! No Error
}
//************************************

Can anybody please tell me why c++ is not behaving the Java way. After
all 'Child' class was derived from 'Parent' class and thus should have
access to all the proctected members in objects of class 'Parent'.

Java is doing this fine but C++ is NOT!!

Why should the rules for C++ be the same as Java?

Child being derived from Parent is only one requirement for protected
access, the other is that the access must be via a pointer, reference or
object of the Child class. Your code above fails the second test.

Even the C++ FAQ fails tomention this (as far as I can see) and it is
commonly misunderstood.

john
 
J

John Harrison

Child being derived from Parent is only one requirement for protected
access, the other is that the access must be via a pointer, reference or
object of the Child class.

Or a class derived from the Child class.

Just to be absolutely precise.

john
 
I

ilovecpp

Alf said:
C++ 'protected' grants access to member functions of Parent and of derived
classes, for the '*this' object.

Isn't it a little peculiar, since access rights are normally granted to
classes?
 
A

Alf P. Steinbach

* ilovecpp:
Isn't it a little peculiar, since access rights are normally granted to
classes?

I don't understand the question. What do you mean by "normally granted to
classes"? Do you understand the rationale I sketched (not quoted above)?

Anyway, thanks for letting me comment on my own posting.

That "*this" is more restrictive than C++ actually is; I couldn't think of a
better way to phrase it at the time, but I think it gets the point across.
 
K

Karl Heinz Buchegger

vj said:
Java is doing this fine but C++ is NOT!!

To transform this into a 'real world problem'

Say there is a person. Each person has some keys to their
house or loft.
Now there are taxi drivers. A taxi driver is surely a kind
of person, thus it also has keys.

C++ says: A taxi driver has access to his own keys only
Java says: A taxi driver has access to all keys of all persons.

So which one is to blame?
 

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
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top