
November 11th, 2006, 05:35 AM
| | | About Inheritance and its concepts
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? | 
November 11th, 2006, 05:45 AM
| | | Re: About Inheritance and its concepts
note: replace all occurences of "public" by "public and private" in the
post above. | 
November 11th, 2006, 06:55 AM
| | | Re: About Inheritance and its concepts
"p_adib@encs.concordia.ca" <padib.conu.f06@gmail.comwrote in message
news:1163224631.296242.98690@m73g2000cwd.googlegro ups.com Quote:
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.
--
John Carson | 
November 21st, 2006, 11:55 PM
| | | Re: About Inheritance and its concepts
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?
John Carson wrote: Quote:
"p_adib@encs.concordia.ca" <padib.conu.f06@gmail.comwrote in message
news:1163224631.296242.98690@m73g2000cwd.googlegro ups.com Quote:
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.
>
--
John Carson
| | 
November 21st, 2006, 11:55 PM
| | | Re: About Inheritance and its concepts
* p_adib@encs.concordia.ca: 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? | 
November 22nd, 2006, 01:55 AM
| | | Re: About Inheritance and its concepts
Alf P. Steinbach wrote: Quote:
* p_adib@encs.concordia.ca:>
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? | 
November 22nd, 2006, 02:35 AM
| | | Re: About Inheritance and its concepts
* p_adib@encs.concordia.ca: Quote:
>
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: 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? | 
November 22nd, 2006, 02:35 AM
| | | Re: About Inheritance and its concepts
* Alf P. Steinbach: Quote:
>
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.
--
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? | 
November 22nd, 2006, 07:25 AM
| | | Re: About Inheritance and its concepts
On 11 Nov, 06:57, "p_a...@encs.concordia.ca" <padib.conu....@gmail.com>
wrote: Quote:
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.)
--
Erik Wikström | 
November 22nd, 2006, 08:25 AM
| | | Re: About Inheritance and its concepts eriwik@student.chalmers.se wrote: Quote:
(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 | 
November 22nd, 2006, 03:45 PM
| | | Re: About Inheritance and its concepts eriwik@student.chalmers.se <eriwik@student.chalmers.sewrote: Quote:
(Note that you can also have a private pure virtual function but it's
kind of pointless.)
| This article actually gives the opposite advice: virtual functions
(which may or may not be pure) should by default be made private: http://www.gotw.ca/publications/mill18.htm
--
Marcus Kwok
Replace 'invalid' with 'net' to reply |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|