Re: What exactly is considered inherited from a base class?
On 13/05/11 16:14, Pete Becker wrote:
On 2011-05-13 01:53:29 -0400, Kris Prad said:
In case a derived class is a ???friend???, then everything in the base
class is inherited. Otherwise, private members are not inherited.
No. Everything (almost; see below) is inherited. Some inherited names
might not be accessible, but that's a separate question. As I said,
*explore* it.
However, consider the following example where even the public members
cannot be said to be inherited:
struct X {};
struct Base
{
void f(const X&) { }
};
struct Derived : public Base
{
// using Base::f; // required if we need to call Base::f(..)
void f(int) { } // hides Base::f, unless using brings it to scope
};
int main(int argc, char** argv)
{
Derived d;
d.f(X()); // fails to compile without ???using??? in Derived
}
While ???friend??? makes private members accessible, ???using??? makes public
members accessible, by bringing the hidden methods into scope.
No, using doesn't affect inheritance. Inheritance is about whether names
from the base class are *visible* in the derived class. Access to those
names is separate from inheritance, and is controlled by access
declarations: public, protected, private. And using directives and
declarations are about overload resolution, not inheritance.
The
former is specified in the base class, and the latter in the derived.
See another blanket definition here:
http://www.cplusplus.com/doc/tutorial/inheritance/
???What is inherited from the base class?
In principle, a derived class inherits every member of a base class
except: Its constructor and its destructor, its operator=() members,
its friends???
Constructors, destructors, and assignment operators are not inherited.
I have seen students getting confused when they know a derived object
physically contains all the members of the base class, but are told
either 1) the derived inherits only part of them or 2) inherits all
but access only a part. Which is right?
So, my understanding of what is inherited:
Whatever the derived class can access. This depends on access
specifiers, ???friend??? and ???using??? declarations.
That's a very confusing definition of inheritance, since it mixes
together inheritance, access, and overloading.
This works for me, but
not sure this is the correct way to teach some one.:-)
class Base {
int i; // private
};
class Derived : public Base {
};
Derived d;
d.i = 3; // Error: i is private
Try it.
Hmmm, inheritance is obviously causing confusion, and I do agree that
you should try doing some programming examples. Here's one below that
you might like to expand upon. It currently works fine, and hopefully
simple enough to be useful. I leave you to hopefully gain some insights
by trying it.
#include <iostream.h>
class W
{
public:
int a;
W() : a(10) { }
};
class X
{
public:
int b;
X() : b(11) { }
};
class Z
{
public:
int c;
Z() : c(13) { }
};
class A : public W,protected X, private Z
{
public:
int get_c() { return c; }
A() { }
};
class B : public A
{
public:
int get_b() { return b; }
B() { }
};
class C : protected B
{
public:
int get_a() { return a; }
int get_b() { return B::get_b(); }
int get_c() { return A::get_c(); }
C() { }
};
void Test()
{
A tmp1;
B tmp2;
C tmp3;
tmp2.a += tmp1.a;
std::cout << tmp1.a << std::endl;
std::cout << tmp2.a << std::endl;
std::cout << tmp2.get_b() << std::endl;
std::cout << tmp2.get_c() << std::endl;
std::cout << tmp3.get_a() << std::endl;
std::cout << tmp3.get_b() << std::endl;
std::cout << tmp3.get_c() << std::endl;
}
HTH
cpp4ever
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]