C++ class confusion

B

Bart C

Have just started looking at C++, and tutorials about virtual functions have
thrown up some confusing issues.

These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.

But this is my problem:

Animal X
X=new Cat

I would have thought X was an Animal and not a Cat. This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.

So X carries within it something that identifies the actual class at
runtime?

If Animal was derived from, say Mammal, could X be of that class too?

What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?

If that were possible, it would be pretty powerful, so I'm sure I must be
missing something here.

Thanks,
Bart
 
D

Daniel T.

Bart C said:
Have just started looking at C++, and tutorials about virtual functions have
thrown up some confusing issues.

Let's see what we can do about that... :) You might also want to
subscribe to comp.object.
These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.

But this is my problem:

Animal X
X=new Cat

Since this is a C++ newsgroup, lets use C++.

Animal* X;
X = new Cat;
I would have thought X was an Animal and not a Cat.

Assuming Cat is derived from Animal, however indirectly, the object that
X points to is both an Animal *and* a Cat.
This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.

More than a dozen, there could be hundreds of them. It's pretty cool
when you think about it. Someone can write a class that you never
conceived of, derive it from Animal, and it will work in all that code
you wrote that uses Animal pointers and Animal references, *without*
having to change of of your code!

Read more about it here
(http://www.objectmentor.com/resources/articles/ocp.pdf)
So X carries within it something that identifies the actual class at
runtime?

I expect so.
If Animal was derived from, say Mammal, could X be of that class too?

No. X could only hold objects that are Animals. If Mammal is the base
class, then an object could be a Mammal without being an Animal.
What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?

There is no "fundamental class".
 
K

Kira Yamato

Have just started looking at C++, and tutorials about virtual functions have
thrown up some confusing issues.

These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.

But this is my problem:

Animal X
X=new Cat

I would have thought X was an Animal and not a Cat.

At this point, X is just an Animal. It cannot do what a Cat can do
that an Animal cannot. For example, you cannot tell X to cough up a
fur-ball, something that presumably only a Cat could do.
This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.

No, only as an Animal.
So X carries within it something that identifies the actual class at
runtime?

Yes, that can happen. But you would have to "down-cast" it to that
derived class first before it learns new tricks.
If Animal was derived from, say Mammal, could X be of that class too?
Yes.


What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?

Only if you know what the actual derived class is and only if you
down-cast it to that class first.
If that were possible, it would be pretty powerful, so I'm sure I must be
missing something here.

OOP is quite powerful, but it's for a different reason that you're thinking.
 
J

jalina

Bart C a écrit :
Have just started looking at C++, and tutorials about virtual functions have
thrown up some confusing issues.

These always seem to use example classes such as Animal and derived classes
Cat and Dog or whatever.

But this is my problem:

Animal X
X=new Cat

I would have thought X was an Animal and not a Cat. This code suggests that
X can be not only of it's declared class but any of perhaps dozens of
derived classes. Maybe of a class I know nothing about if I've created
Animal and someone extends it.

So X carries within it something that identifies the actual class at
runtime?

If Animal was derived from, say Mammal, could X be of that class too?

What's to stop someone creating some variable Y of the fundamental class
(Object?), then Y could assume, by simple assignment, *any* derived class,
in other words, any class?

If that were possible, it would be pretty powerful, so I'm sure I must be
missing something here.

Thanks,
Bart
He he. Trying to get into OO programming.
 
J

Juha Nieminen

Bart said:
Animal X
X=new Cat

I would have thought X was an Animal and not a Cat.

But a Cat *is* an Animal, and thus can be used anywhere an Animal is
expected. That's basic OOP.

Another way of saying the same is that if you write a piece of code
which handles an Animal, any Animal can be given to it and it will work
equally well. The code doesn't (and shouldn't) care, nor does it even
need to care what type of Animal there really is behind the point/reference.
So X carries within it something that identifies the actual class at
runtime?

Yes.
If Animal was derived from, say Mammal, could X be of that class too?

Don't you mean the other way around?
 
B

Bart C

jalina said:
Bart C a écrit :
He he. Trying to get into OO programming.

Actually, trying to design classes into a language, based on the C++ model,
then write a compiler. Without any experience of OO. A little crazy I know.
 
J

James Kanze

But a Cat *is* an Animal, and thus can be used anywhere an Animal is
expected. That's basic OOP.

Yes and no. In his example, X is an object of Animal type. A
given object can never, ever change its (most-derived) type.

Of course, the next line, with new, suggests that what he really
wanted was a pointer. A pointer to an Animal can in fact point
to a Cat, and (providing that the functions are virtual) will
act like a cat.

For the rest, there's a lot, lot more to it than that, and I
fear that Bart will have to get some books, because it's really
a lot more than one can explain in an answer here. The
important C++ relevant parts are, however, that in C++,
polymorphism (using something that is really a Cat where the
original program only knows about Animal) only works through
pointers and references, that C++ (like the other OO languages I
know) doesn't allow an actual object to ever change its type,
once it has been constructed, and the C++, unlike most "pure" OO
languages, supports and uses value semantics by default; if you
don't say otherwise, a declaration declares an actual object,
not a reference to an object.
 
J

Juha Nieminen

Of course, the next line, with new, suggests that what he really
wanted was a pointer.

I assumed that he simply made a typo, or that he was writing in
pseudocode (which would also be indicated by him not using semicolons).
 
B

Bart C

Yes and no. In his example, X is an object of Animal type. A
given object can never, ever change its (most-derived) type. ....

For the rest, there's a lot, lot more to it than that, and I
fear that Bart will have to get some books, because it's really
a lot more than one can explain in an answer here.

Thanks for all the replies. Have now obtained a book (not very good, but the
only one in the library). And I have a C++ compiler somewhere so perhaps I
can even try some examples! Although at this stage I only wanted to
understand, not actually do any programming.

And the code lines were pseudo-code, or at least not meant to be tested C++
syntax.

Bart
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top