473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Understanding virtual functions

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.
Oct 9 '08 #1
3 1963
Are you too dumb or too lazy to just try?
Oct 9 '08 #2
On 9 Oct, 10:54, Matthias Berndt <matthias_ber...@gmx.dewrote:
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.
Oct 14 '08 #3
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.
Oct 14 '08 #4

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
5
by: Dilip | last post by:
In a polymorphic hierarchy is it common practice to have public virtual functions in the base class with a default empty implementation and let the derived classes decide whether they want to...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.