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 8 1255
"Bart C" <bc@freeuk.comwrote:
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".
On 2007-12-20 20:05:15 -0500, "Bart C" <bc@freeuk.comsaid:
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.
--
-kira
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.
Bart C wrote:
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?
"jalina" <ja****@nospam.please.comwrote in message
news:47*********************@news.orange.fr...
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.
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.
On Dec 21, 10:57 am, Juha Nieminen <nos...@thanks.invalidwrote:
Bart C wrote:
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.
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.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
On Dec 21, 10:57 am, Juha Nieminen <nos...@thanks.invalidwrote:
>Bart C wrote:
>>Animal X X=new Cat
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).
"James Kanze" <ja*********@gmail.comwrote in message
news:a4**********************************@21g2000h sj.googlegroups.com...
On Dec 21, 10:57 am, Juha Nieminen <nos...@thanks.invalidwrote:
>Bart C wrote:
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.
>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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grey Plastic |
last post by:
I have several classes that all keep track of static data. However,
the manner that they keep track of static data is identical, and so
I'm using the template<class Child> class Parent { ... };...
|
by: ypjofficial |
last post by:
Dear All,
According to OOPs , a base class pointer can to point to derived class
object....call this as fact1
But somehow I am not comfortable while understanding this concept.
The explanaition...
|
by: Turamnvia Suouriviaskimatta |
last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ ,
comp.realtime, comp.software-eng" groups regarding selection of a
programming language of C, C++ or Ada for safety critical...
|
by: djake |
last post by:
In the stroustrup C++ programming language (third edition) i can
find example like this:
string s1= "Hello";
So I imagine string is a standard class.
Furthermore the class in the example is...
|
by: christopher diggins |
last post by:
It appears that the following is not considered a class:
template<typename T>
class C { };
?
So officially is this considered: a class, a template, a class template, or
a template class? I...
|
by: JohnGoogle |
last post by:
Hi,
Newbie question...
After a recent article in VSJ I had a go at implementing a Fraction
class to aid my understanding of operator overloading. After a previous
message someone suggested...
|
by: garyusenet |
last post by:
I'm using an example piece of code: -
namespace Wintellect.Interop.Sound{
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
sealed class Sound{
public static...
|
by: MR |
last post by:
Hello All,
I have a question about decorators, and I think an illustration would
be helpful. Consider the following simple class:
#begin code
class Foo:
def fooDecorator(f):
print...
|
by: Anup Daware |
last post by:
Hi Group,
I have a little confusion over the use of static class in C#.
I have a static method in my static class. This method reads an xml
and returns a collection of objects.
This collection...
|
by: notnorwegian |
last post by:
i have some confusion over this.
sure a class is basically a classification, like for example an animal
or flower. and an object/instance of that class is then for example a
cat.
an object is...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |