About Inheritance and its concepts

P

p_adib

Hi all.
In the context of inheritance in c++, I was taught in class that every
derived class is "part base class content - part new content".
Moreover, I was taught that the constructor of the base class is used
to initialize base class content and that the derived class constructor
is used to initialize the new contents.

Ok, here is where I'm confused. If the derived class is part base
content - part new content, then the keyword "protected" becomes
unnecessary, since part of the base class is included in the derived
class apart from the "protected" mechanism. This would be true unless
public data members and functions are not included in the derived
class. Then that would mean that the "part base class content" only
contains protected data members and protected functions of the base
class.

If that were true though, it would make no sense to use the base
class's constructor to initialize the "part base class content" since
some of this content might not be public and would then not be
inherited by the derived class and would therefore not require
initialization.

Can anyone clear me up on this?


Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?
 
J

John Carson

Hi all.
In the context of inheritance in c++, I was taught in class that every
derived class is "part base class content - part new content".
Moreover, I was taught that the constructor of the base class is used
to initialize base class content and that the derived class
constructor is used to initialize the new contents.

Ok, here is where I'm confused. If the derived class is part base
content - part new content, then the keyword "protected" becomes
unnecessary, since part of the base class is included in the derived
class apart from the "protected" mechanism. This would be true unless
public data members and functions are not included in the derived
class. Then that would mean that the "part base class content" only
contains protected data members and protected functions of the base
class.

If that were true though, it would make no sense to use the base
class's constructor to initialize the "part base class content" since
some of this content might not be public and would then not be
inherited by the derived class and would therefore not require
initialization.

Can anyone clear me up on this?


Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?

*All* of the base class content becomes part of any derived class object.
However, not all of the base class content may be accessible to derived
class functions (or to functions outside the class that have access to a
derived object). Public, private and protected govern what is accessible,
not what is included.
 
P

p_adib

John, that makes sense.
So, to clarify:
1) My mistake was in assuming that the protected keyword indicates what
is included in a derived class.
2) All data members and functions of the base class are included in the
derived class, no matter their visibility. They are included with their
visibility.

A new question arises:
If a data member (class variable) is declared as private in the base
class, when the base class content is copied into the subclass, then
how do the visibilities apply?

Using the below example,

******************************
Person
******************************
protected string lastName
private string firstName
public string nationality
******************************

*************************************
Professor (Generalizes Person)
*************************************
private string department
*************************************

Will a Professor object's "firstName" variable be private to him? Then
what is the purpose of the protected keyword?
 
P

p_adib

Alf said:
* (e-mail address removed):
[top-posting]

See FAQ item 5.4.

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

Thank you M. Steinback for being clear.

As for my post, I read something very clear about it in the FAQ so I
will point to it.
Item [24.6].

To quote what is necessary:
"None of the subclasses can access anything that is private in B [the
base class]."

Though I am still wondering. If you are using dynamic binding, which of
the following two cases is legal?

1) Base b = new SubC(...);
2) SubC sc = new SubC(...);

I would assume case 1. Am I right?
 
A

Alf P. Steinbach

* (e-mail address removed):
As for my post, I read something very clear about it in the FAQ so I
will point to it.
Item [24.6].

To quote what is necessary:
"None of the subclasses can access anything that is private in B [the
base class]."

Though I am still wondering. If you are using dynamic binding, which of
the following two cases is legal?

1) Base b = new SubC(...);
2) SubC sc = new SubC(...);

I would assume case 1. Am I right?

For (2), the object being declared is of type SubC, and the
initialization expression, using 'new', produces a SubC* pointer
(pointing to a dynamically allocated SubC object).

That will only work if SubC has a constructor that accepts a SubC pointer.

Presumably you meant to write

Base b* = new SubC(...); // 1
SubC sc* = new SubC(...); // 2

where both are valid if Base is, at this place in the code, an
/accessible/ base class of SubC.

It would be the same for

Base const& b = SubC( ... ); // 1
SubC const& sc = SubC( ... ); // 2

For example, if SubC is publicly derived from Base, then Base is always
an accessible base class.

There are two separate issues: implicit pointer (and reference) "upcast"
to a base class, and accessibility of said base class.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
Base b* = new SubC(...); // 1
SubC sc* = new SubC(...); // 2

The inventor of "transpose back": step forth, show yourself, and make
Mozilla implement this feature in Thunderbird. Now!

Now I have to resque some things out of the oven.
 
E

eriwik

Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?

You might have figured this out by now but anyway... The usage of pure
virtual functions and protected (or private and public) are different
things. First of, a class with a pure virtual function can not be
instanciated, but can beuseful to assure that the derived classes have
a certain interface, for instance. But sometimes there might exist a
"default" bahaviour for a function, in which case it can be nice to
implement this in the base-class, so if the derived class does not want
to change the default behaviour it does not have to worry about that
function.
As you see there may be both public and protected pure virtual
functions, but those two concepts does not affect each other much.
(Note that you can also have a private pure virtual function but it's
kind of pointless.)
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

(Note that you can also have a private pure virtual function but it's
kind of pointless.)

I'm not so sure about that. It is saying that sub-classes must
implement the method, but may not call it. This means that the method
may only be called as part of the implementation of the abstract
super-class that contains the private pure virtual.

The problem here is of course that the protection from not being called
is weak as any derived class can change the access specifier.

class Base {
public:
void doSomething() {
partOfProcess();
std::cout << " world" << std::endl;
}
private:
virtual void partOfProcess() = 0;
};

class Derived : public Base {
private: // Can change this to public to make the call legal
void partOfProcess() {
std::cout << "Hello";
}
};

int main() {
Derived d;
d.doSomething();
d.partOfProcess(); // Illegal unless Derived puts it in its public
space
}

You should certainly question anybody who implements a virtual method
with a more liberal access specifier.


K
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top