 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
p_adib@encs.concordia.ca Guest
|
Posted: Sat Nov 11, 2006 10:10 am Post subject: 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? |
|
| Back to top |
|
 |
p_adib@encs.concordia.ca Guest
|
Posted: Sat Nov 11, 2006 10:10 am Post subject: Re: About Inheritance and its concepts |
|
|
note: replace all occurences of "public" by "public and private" in the
post above. |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Sat Nov 11, 2006 10:11 am Post subject: Re: About Inheritance and its concepts |
|
|
"p_adib (AT) encs (DOT) concordia.ca" <padib.conu.f06 (AT) gmail (DOT) com> wrote in message
news:1163224631.296242.98690 (AT) m73g2000cwd (DOT) googlegroups.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 |
|
| Back to top |
|
 |
Kirit Sælensminde Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: About Inheritance and its concepts |
|
|
eriwik (AT) student (DOT) 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 |
|
| Back to top |
|
 |
eriwik@student.chalmers.s Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|