gw7rib
10/14/2008 7:55:00 PM
On 9 Oct, 08:27, Magnus.Morab...@gmail.com wrote:
Well, if no-one else will reply to this...
> Lets say I have the following -
>
> class Shape
> {
> ...
> virtual void Plot();
> PrintInfo();
>
> }
Presumably the "..." includes a "public:". And you need a type for the
return value of PrintInfo. And a semicolon after the "{".
> class Circle : public Shape
> {
> ...
> void Plot();
> PrintInfo();
>
> }
>
> ...
>
> Are the following correct? -
>
> Circle circle = new Circle();
This won't work. "new" gives you a pointer to an object. So you would
need something like either:
Circle* circle = new Circle();
or
Circle circle;
> Shape* pShape = circle;
> Circle* pCircle = circle;
If you've gone for the second approach you need &circle, not circle.
> circle.Plot(); // Circle's Plot
> circle.PrintInfo(); // Circle's PrintInfo
If you've gone for the second approach then yes, this looks right.
> pCircle->Plot(); // Circle's Plot
> pCircle->PrintInfo(); // Circle's PrintInfo
Yes.
> pShape->Plot(); // Circle's Plot
Yes. This is what virtual functions do.
> pShape->PrintInfo(); // Shape's PrintInfo
Yes. However, if you find yourself doing this sort of thing in real
code then there is probably something badly wrong with your design.
> ((Shape)circle).Plot(); // Shape's Plot
> ((Shape)circle).PrintInfo(); // Shape's PrintInfo
Before I tried it, I wasn't at all convinced these casts would even
work. But they seem to.
> ((Circle*)pShape)->Plot(); // Circle's PrintInfo
> ((Circle*)pShape)->PrintInfo(); // Circle's PrintInfo
>
> ((Shape*)pCircle)->Plot(); // Circle's PrintInfo
> ((Shape*)pCircle)->PrintInfo(); // Shape's PrintInfo
Er, yes. But I don't think that messing about with casts like this is
really going to help you understand what is going on.
Hope that is useful.
Paul.