C++ class confusion

Discussion in 'C++' started by Bart C, Dec 21, 2007.

  1. Bart C

    Bart C Guest

    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
    Bart C, Dec 21, 2007
    #1
    1. Advertising

  2. Bart C

    Daniel T. Guest

    "Bart C" <> wrote:

    > 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".
    Daniel T., Dec 21, 2007
    #2
    1. Advertising

  3. Bart C

    Kira Yamato Guest

    On 2007-12-20 20:05:15 -0500, "Bart C" <> said:

    > 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
    Kira Yamato, Dec 21, 2007
    #3
  4. Bart C

    jalina Guest

    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.
    jalina, Dec 21, 2007
    #4
  5. 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?
    Juha Nieminen, Dec 21, 2007
    #5
  6. Bart C

    Bart C Guest

    "jalina" <> wrote in message
    news:476b78b1$0$903$...
    > 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.
    Bart C, Dec 21, 2007
    #6
  7. Bart C

    James Kanze Guest

    On Dec 21, 10:57 am, Juha Nieminen <> wrote:
    > 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:
    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, Dec 21, 2007
    #7
  8. James Kanze wrote:
    > On Dec 21, 10:57 am, Juha Nieminen <> wrote:
    >> 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).
    Juha Nieminen, Dec 21, 2007
    #8
  9. Bart C

    Bart C Guest

    "James Kanze" <> wrote in message
    news:...
    On Dec 21, 10:57 am, Juha Nieminen <> wrote:
    >> 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
    Bart C, Dec 21, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Fao, Sean

    Class Confusion

    Fao, Sean, Sep 1, 2004, in forum: C++
    Replies:
    4
    Views:
    432
    Fao, Sean
    Sep 2, 2004
  3. christopher diggins
    Replies:
    16
    Views:
    744
    Pete Becker
    May 4, 2005
  4. Cndistin
    Replies:
    3
    Views:
    258
    Pierre Quentel
    Jan 6, 2004
  5. Joseph Turian
    Replies:
    5
    Views:
    585
Loading...

Share This Page