473,387 Members | 1,541 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.

C++ quiz question: private inheritance

Hello everybody, today I had another quiz question
"if class X is privately derived from base class Y what is the scope of
the public, protected, private members of Y will be in class X"

By scope they meant public/protected/private access modifiers :)
Obviously all members of the base privately inherited class will be
private, and that was my answer. However, the teacher checked my
answers when I handed in, and the problem was that I had to specify
that private members of base class are not inherited (that the way he
called that they were inaccessible from inside of the derived class),
however, it doesn't make my answer incorrect - all of them are still
private. The question from the beginning didn't mean anything that I
had to specify if the private members were accessible or not, I only
had to specify their "scope".

Was my answer correct or not?
thanks

Oct 20 '05 #1
8 7807
__PPS__ wrote:
Hello everybody, today I had another quiz question
"if class X is privately derived from base class Y what is the scope of
the public, protected, private members of Y will be in class X"

By scope they meant public/protected/private access modifiers :)
You should definitely suggest the change in terminology to them.
Obviously all members of the base privately inherited class will be
private, and that was my answer.
That's all? Seems too simple an answer...
However, the teacher checked my
answers when I handed in, and the problem was that I had to specify
that private members of base class are not inherited (that the way he
called that they were inaccessible from inside of the derived class),
however, it doesn't make my answer incorrect - all of them are still
private. The question from the beginning didn't mean anything that I
had to specify if the private members were accessible or not, I only
had to specify their "scope".

Was my answer correct or not?


Correct? I don't know, I am not the one who asked. Incomplete, for
sure. This is a bit more complete:

(assuming X is not made a friend of Y)

Private in Y -> inaccessible in X (inheritance doesn't matter)
Protected in Y -> accessible in X (through inheritance)
Public in Y -> accessible in X (like anywhere)

Any member of X::Y is not available to anybody outside of X unless
it's a friend of X. For a friend of X only public and protected
members of X::Y are visible through X (just like inside X itself).

If you derive another class from X (and it's not a friend), then no
member of X::Y is visible to it (despite the fact that Y has it own
public members). That's a bit of a pitfall in the language, but if
you are aware of it, you're not going to fall in.

class Y {
protected:
void foo();
};

class X : Y {
protected:
void bar() { Y::foo(); } // fine
};

class W : X {
void baz() {
X::bar(); // fine
Y y; // inaccessible
this->X::Y::foo(); // error - inaccessible
}
};

V
Oct 20 '05 #2
__PPS__ wrote:
Obviously all members of the base privately inherited class will be
private, and that was my answer. However, the teacher checked my
answers when I handed in, and the problem was that I had to specify
that private members of base class are not inherited (that the way he
called that they were inaccessible from inside of the derived class),
however, it doesn't make my answer incorrect - all of them are still
private. The question from the beginning didn't mean anything that I
had to specify if the private members were accessible or not, I only
had to specify their "scope".

Was my answer correct or not?


I'd say "no". A member is private in a class if that class and its friends
(and only those) can access it. But a member that was private in the base
class cannot be accessed at all in the derived class, so it's not private
in that derived class.

Oct 21 '05 #3
> > By scope they meant public/protected/private access modifiers :)

You should definitely suggest the change in terminology to them.
Done that :) I put a line about the real class scope of the members. If
I didn't ask these questions about my quizes here, there was a chance
that I would start to use their terminology; most likely everybody in
the class will use this terminology
Obviously all members of the base privately inherited class will be
private, and that was my answer.
That's all? Seems too simple an answer...


This is a basic c++ class, no details (especialy friends etc) are
necessary; we had almost no space to write this answer - there was
enough space for about 10-15 words of normal handwriten text (on the
answer sheets provided by the tutor)
> However, the teacher checked my
answers when I handed in, and the problem was that I had to specify
that private members of base class are not inherited (that the way he
called that they were inaccessible from inside of the derived class),
however, it doesn't make my answer incorrect - all of them are still
private. The question from the beginning didn't mean anything that I
had to specify if the private members were accessible or not, I only
had to specify their "scope".

Was my answer correct or not?
Correct? I don't know, I am not the one who asked. Incomplete, for
sure. This is a bit more complete:


I agree that it's not complete when it comes to details, but does the
original question ask for it? or it just asks for the "scope"?
When I read the question I even had no idea that it was related to
accessibility of that members, although I know that they become
inaccessible
(assuming X is not made a friend of Y)

Private in Y -> inaccessible in X (inheritance doesn't matter)
Protected in Y -> accessible in X (through inheritance)
Public in Y -> accessible in X (like anywhere)

Any member of X::Y is not available to anybody outside of X unless
it's a friend of X. For a friend of X only public and protected
members of X::Y are visible through X (just like inside X itself).

If you derive another class from X (and it's not a friend), then no
member of X::Y is visible to it (despite the fact that Y has it own
public members). That's a bit of a pitfall in the language, but if
you are aware of it, you're not going to fall in.
I assume it will happen only with private inheritance, right?

class Y {
protected:
void foo();
};

class X : Y {
protected:
void bar() { Y::foo(); } // fine
};

class W : X {
void baz() {
X::bar(); // fine
Y y; // inaccessible
This really looks strange to me - we cannot create temporary object of
class Y that has public constructor :) If I didn't check it with a
compiler I would think that you were wrong about that!
this->X::Y::foo(); // error - inaccessible
Here syntax looks a bit strange to me, I always write it like
X::Y::foo(); without this->. Is it ok to do so?
}
};

V


Oct 21 '05 #4
> I'd say "no". A member is private in a class if that class and its friends
(and only those) can access it. But a member that was private in the base
class cannot be accessed at all in the derived class, so it's not private
in that derived class.

You are right, it's not private according to your reasoning, but how is
it called then if it's not private/protected/public?
Privately inherited private members, basicly, become private for class
scope members. How do we call such private private members then?
Our teacher in class called them "not inherited" which is a little bit
misleading - it's likely to make students believe that such members are
excluded from objects of the class. This term seems to be ok for
methods, but not for data.

Oct 21 '05 #5
On 20 Oct 2005 18:36:21 -0700, "__PPS__" <i-*********@yandex.ru>
wrote:
I'd say "no". A member is private in a class if that class and its friends
(and only those) can access it. But a member that was private in the base
class cannot be accessed at all in the derived class, so it's not private
in that derived class.

You are right, it's not private according to your reasoning, but how is
it called then if it's not private/protected/public?
Privately inherited private members, basicly, become private for class
scope members. How do we call such private private members then?
Our teacher in class called them "not inherited" which is a little bit
misleading - it's likely to make students believe that such members are
excluded from objects of the class. This term seems to be ok for
methods, but not for data.


These "not inherited" functions get called by other functions in the
parent class. This is actually no different from public inheritance;
the parent's private functions are always inaccessible to the child
class (ignoring the case of friend class/functions), and so they only
are executed when instances of the parent class call them.

In the following example, a child class that uses private inheritance
can indirectly call one of its parent's private functions:

// Parent class
class Vehicle {
private:
int mySpeed;

void setSpeed(int speed) {
mySpeed = speed;
}

public:
void move(int speed) {
setSpeed(speed);
std::cout << "moving at " << speed << " mph"
}
};

// Child class that privately inherits Vehicle
class Car : private Vehicle {
public:
void drive() {
move(25); // <-- private function setSpeed gets called
}
}
Oct 21 '05 #6
__PPS__ wrote:
[..]
class W : X {
void baz() {
X::bar(); // fine
Y y; // inaccessible
This really looks strange to me - we cannot create temporary object of
class Y that has public constructor :) If I didn't check it with a
compiler I would think that you were wrong about that!


It came up a couple of times over the past month. The 'Y' member
is found first, and it's inaccessible. You need to make it

::Y y;

to instantiate the _automatic_ (not temporary) 'y'.
this->X::Y::foo(); // error - inaccessible
Here syntax looks a bit strange to me, I always write it like
X::Y::foo(); without this->. Is it ok to do so?


"this->" is superfluous, but I wanted to emphasize what we're trying
to accomplish there.
}
};

V


V
Oct 21 '05 #7
if there were no way to "indirectly" call private functions then they
would not be any useful. Just think about it ... they are always
inderectly called by some means

Oct 21 '05 #8
__PPS__ wrote:
I'd say "no". A member is private in a class if that class and its
friends (and only those) can access it. But a member that was private in
the base class cannot be accessed at all in the derived class, so it's
not private in that derived class.

You are right, it's not private according to your reasoning,


Well, the C++ standard says the same. Its definition of a private member is:
"it's name can be used only by members and friends of the class in which it
is declared."
but how is it called then if it's not private/protected/public?
The C++ standard calls them "inaccessible".
Privately inherited private members, basicly, become private for class
scope members. How do we call such private private members then?
Our teacher in class called them "not inherited" which is a little bit
misleading - it's likely to make students believe that such members are
excluded from objects of the class.
Inheritance is not about what data an object contains, but about what can be
accessed though its interface.

A more complete quote than the above from the C++ standard:

<Quote>
A member of a class can be

-- private; that is, its name can be used only by members and friends of the
class in which it is declared.

-- protected; that is, its name can be used only by members and friends of
the class in which it is declared, and by members and friends of classes
derived from this class.

-- public; that is, its name can be used anywhere without access
restrictions.
</Quote>

So, as you already noted, if something was private in a base class (it
doesn't matter how you derived from it), it's neither private, nor
protected, nor public in the derived class.
Since a member must have one of those three accessibilities, one could say
that it's not a member of that class, or that the class didn't inherit the
member from the base class.
This term seems to be ok for methods, but not for data.


I think it's ok if explained in more detail. On its own, it could be
misunderstood.

Oct 21 '05 #9

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

Similar topics

2
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
by: Vandana Rola | last post by:
Hello Everyone, I am a beginner in Javascript.I want to create fun quiz tool using javascript (no database). The feature of that tool is every question has five choices. The user needs to select...
0
by: Tim | last post by:
Dear Developers, Firstly, I'm not sure where this post should go so I apologise if this is in the wrong group or area. I'm currently interviewing for a vb.net developer who doesn't mind...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
4
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
16
by: C++ Hell | last post by:
Hey everyone just designing a quiz for school and managed to write the code for the questions and answers thou i have to add scores and at the end an overall score does anyone have any idea what to...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: dradhakr | last post by:
I need to put 5 question only,that's randomly in flash,how can i write script in flash <?xml version="1.0"?> <!DOCTYPE quiz > <quiz> <title>The Quiz</title> <items> <item>...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.