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? 10 1239
note: replace all occurences of "public" by "public and private" in the
post above.
"p_****@encs.concordia.ca" <pa************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com
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
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:
"p_****@encs.concordia.ca" <pa************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com
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
* p_****@encs.concordia.ca:
[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?
Alf P. Steinbach wrote:
* p_****@encs.concordia.ca:
[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?
* p_****@encs.concordia.ca:
>
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?
* 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.
--
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?
On 11 Nov, 06:57, "p_a...@encs.concordia.ca" <padib.conu....@gmail.com>
wrote:
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 er****@student.chalmers.se wrote:
(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 er****@student.chalmers.se <er****@student.chalmers.sewrote:
(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David MacQuigg |
last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating
classes in Python. I'm trying to figure out what fundamental benefit
there is to having classes. Is all this complexity...
|
by: vidalsasoon |
last post by:
Ok, this is the structure of my classes.
" class Global
" |
" -------------------------------------------
" | ...
|
by: scorpion53061 |
last post by:
I am just now really starting to get into inheritance - controls, datasets,
views all that stuff and I am seeing the enormous savings in performance and
even watching the exe file size go down...
|
by: Michael S |
last post by:
Why do people spend so much time writing complex generic types?
for fun?
to learn?
for use?
I think of generics like I do about operator overloading.
Great to have as a language-feature, as...
|
by: KraftDiner |
last post by:
I was under the assumption that everything in python was a refrence...
so if I code this:
lst =
for i in lst:
if i==2:
i = 4
print lst
I though the contents of lst would be modified.....
|
by: utab |
last post by:
Dear,
I have experince in C( numerical projects, like engineering problems,
scientific applications) I have the basic notion of C++ also, I have
read Accelerated C++ until Chapter 7, however it...
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: Tom Cole |
last post by:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two...
|
by: Adam Nielsen |
last post by:
Hi everyone,
I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
|
by: JoeC |
last post by:
I have been writing games and I also read about good programming
techniques. I tend to create large objects that do lots of things. A
good example I have is a unit object. The object controls...
|
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,...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
| |