inheritance

A

Asapi

When a derived class inherits from a base class, does the former inherits
everything, including public/protected/private instance data, static data,
and various methods(static, private/public, etc.) in base class? What
happens to static data and methods during inheritance?

What is an accurate definition of inheritance?

Thanks!
 
D

David White

Asapi said:
When a derived class inherits from a base class, does the former inherits
everything, including public/protected/private instance data, static data,
and various methods(static, private/public, etc.) in base class?
Yes.

What
happens to static data and methods during inheritance?

They are just there, available for use if their access rights allow it.
What is an accurate definition of inheritance?

Good question. In OO terms, a derived class is a specialized form of a base
class, e.g., Shape and Ellipse. The derived class usually makes itself more
specific by adding to or altering the base class behaviour. So, an Ellipse
class adds members to extend Shape, which is likely an abstract, unspecified
shape, into an ellipse. In C++ terms, I guess the answer is a lot more
technical, and I'm not sure I can do a good job of it, especially if you
include multiple inheritance and virtual inheritance. But, essentially, it's
as you described it. The derived class inherits everything in the base
class. However, a derived class is not in the same scope as the base class.
That means that names in the derived class hide the same names in the base
class.

DW
 
J

Jeff Schwab

Calvin said:
Shouldn't the private instance data and methods will NOT be inherited?


Sure they will. They just won't be accessible directly from the derived
class.
 
D

David White

Calvin Lai said:
Shouldn't the private instance data and methods will NOT be inherited?

They have to be inherited because they are necessary for the base-class
sub-object to work properly. By "inherited" I mean simply that they are
present. They are part of the object. If they are private, the derived class
cannot access them, but the base-class part of the object still needs them.

DW
 
J

jeffc

David White said:

I would not say that static variables are inherited. In what sense would
you say they get inheritance? The only thing a subclass can do that
everyone else in the world can't do is access a protected static variable.
But that isn't inheritance exactly. You could just as easily define a
friend that can also access your protected static variables. Static
variables are just "there" - global variables within a namespace.
 
D

David White

jeffc said:
I would not say that static variables are inherited. In what sense would
you say they get inheritance? The only thing a subclass can do that
everyone else in the world can't do is access a protected static variable.
But that isn't inheritance exactly. You could just as easily define a
friend that can also access your protected static variables. Static
variables are just "there" - global variables within a namespace.

class B
{
public:
static int K;
};

int B::K;

class D : public B
{
};

int main()
{
int k = D::K;
}

Surely class D has inherited B::K?

DW
 
J

jeffc

David White said:
class B
{
public:
static int K;
};

int B::K;

class D : public B
{
};

int main()
{
int k = D::K;
}

Surely class D has inherited B::K?

Maybe. It's not what I think of as inheritance, but it's an interesting
point.
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top