473,587 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ class confusion

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
Dec 21 '07 #1
8 1265
"Bart C" <bc@freeuk.comw rote:
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 "fundamenta l class".
Dec 21 '07 #2
On 2007-12-20 20:05:15 -0500, "Bart C" <bc@freeuk.coms aid:
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

Dec 21 '07 #3
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.
Dec 21 '07 #4
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?
Dec 21 '07 #5

"jalina" <ja****@nospam. please.comwrote in message
news:47******** *************@n ews.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.
Dec 21 '07 #6
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 21 '07 #7
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).
Dec 21 '07 #8

"James Kanze" <ja*********@gm ail.comwrote in message
news:a4******** *************** ***********@21g 2000hsj.googleg roups.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

Dec 21 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2743
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 { ... }; idiom (don't know the name of it, if there is one). The problem is that I don't want any of my classes to have public constructors. They should be...
1
16226
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 to the fact1 is given as since the derived object always consists of the base part , the base class pointer will always point to the base part in the...
822
29263
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 real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++...
27
2418
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 initialized with a constructor like this
16
2160
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 always thought of it as a parameterized class. What would the rationale be for not considering it as just a 'class'?
37
4001
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 that I implement it as a struct rather than a class which I did and all worked OK. The simplest declaration for the struct is:
70
3338
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 void MessageBeep(BeepTypes type){ if(!MessageBeep((UInt32) type)){ Int32 err = Marshal.GetLastWin32Error();
7
2275
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 "fooDecorator"
2
1645
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 of objects can be different for different users. This method uses some non-static variables which are local to this method. Following is my...
3
1536
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 an instance of a class. that i know, i also know how to program with classes etc. i am just confused about the term object-oriented.
0
7923
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6629
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.