Understanding virtual functions

  • Thread starter Magnus.Moraberg
  • Start date
M

Magnus.Moraberg

Lets say I have the following -

class Shape
{
...
virtual void Plot();
PrintInfo();
}

class Circle : public Shape
{
...
void Plot();
PrintInfo();
}

....

Are the following correct? -

Circle circle = new Circle();
Shape* pShape = circle;
Circle* pCircle = circle;

circle.Plot(); // Circle's Plot
circle.PrintInfo(); // Circle's PrintInfo

pCircle->Plot(); // Circle's Plot
pCircle->PrintInfo(); // Circle's PrintInfo

pShape->Plot(); // Circle's Plot
pShape->PrintInfo(); // Shape's PrintInfo

((Shape)circle).Plot(); // Shape's Plot
((Shape)circle).PrintInfo(); // Shape's PrintInfo

((Circle*)pShape)->Plot(); // Circle's PrintInfo
((Circle*)pShape)->PrintInfo(); // Circle's PrintInfo

((Shape*)pCircle)->Plot(); // Circle's PrintInfo
((Shape*)pCircle)->PrintInfo(); // Shape's PrintInfo

Thanks,

Barry.
 
G

gw7rib

Are you too dumb or too lazy to just try?

To be fair, "just trying" is not a very good way of finding out how C+
+ works. Things may appear to work misleadingly. Before you know it,
people start saying things like "a = a++; will increment a" or "void
main is fine", becasue they've tried it and found that it works. They
think.
 
G

gw7rib

On 9 Oct, 08:27, (e-mail address removed) 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top