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

C++ vs Java : Problem with Inheritence

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

Sep 14 '05 #1
7 1585
* 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.

--
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 14 '05 #2
vj wrote:
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
Sep 14 '05 #3
>
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
Sep 14 '05 #4
vj wrote:
Can anybody please tell me why c++ is not behaving the Java way.


Can you tell us why a cat is not a dog?

--
Salu2
Sep 14 '05 #5
Alf P. Steinbach wrote:
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?

Sep 14 '05 #6
* ilovecpp:
Alf P. Steinbach wrote:
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?


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.

--
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 14 '05 #7
vj wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 15 '05 #8

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

Similar topics

54
by: Jerry | last post by:
What are the advantages and disadvantages of using Object Oriented PHP vs Java?
54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
25
by: Digital Puer | last post by:
I'm looking at some software development jobs whose listings require C++ experience. My history is that I have a strong C background, and a few years of C++ from undergrad classes. I've been...
8
by: Digital Puer | last post by:
I made the following table to help me (re)learn inheritence basics. Can someone check if it's correct? This table requires a courier-like font. Java C++ ---- ...
30
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with...
0
by: cs_hart | last post by:
After converting a java application to c# using the microsoft conversion assistant, I get multiple errors "type in interface list is not an interface". From reading other posts it appears that this...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.