473,386 Members | 1,962 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,386 software developers and data experts.

Declaring inherited class objects

I've been reading about virtual functions and I keep seeing code like this in
the examples:

Dimensions c = new Circle(r);

The Circle class is derived from Dimensions class.

I understand the benefits of inheritance but I don't understand why you
would declare a circle as a Dimensions class. This seems to be a common
practice, too. The following three articles from MSDN all do it but I don't
understand why. Are they just trying to demonstrate which method would be
called under these special declaration circumstances?
Why not do this:

Circle c = new Circle(r);
1)
http://msdn.microsoft.com/library/de...fvirtualpg.asp
2)
http://msdn.microsoft.com/library/de...ngtutorial.asp
3)
http://msdn.microsoft.com/library/de...pec_10_5_3.asp
Nov 16 '05 #1
3 2249
Usually the reason is polymorphism. If, for example, the Dimensions 'c'
variable is later assigned to another instance, that is *not* a circle but
is still a Dimensions... then you would do it.

I think in that particular example on MSDN, they were just demonstrating
polymorphism and that's why they chose to declare the type as Dimensions.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"Mark Bremmer" <Mark Br*****@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
I've been reading about virtual functions and I keep seeing code like this in the examples:

Dimensions c = new Circle(r);

The Circle class is derived from Dimensions class.

I understand the benefits of inheritance but I don't understand why you
would declare a circle as a Dimensions class. This seems to be a common
practice, too. The following three articles from MSDN all do it but I don't understand why. Are they just trying to demonstrate which method would be
called under these special declaration circumstances?
Why not do this:

Circle c = new Circle(r);
1)
http://msdn.microsoft.com/library/de...fvirtualpg.asp 2)
http://msdn.microsoft.com/library/de...ngtutorial.asp 3)

http://msdn.microsoft.com/library/de...pec_10_5_3.asp
Nov 16 '05 #2
"Mark Bremmer" wrote:
I've been reading about virtual functions and I keep seeing code like this in
the examples:
Dimensions c = new Circle(r);


It is only the reference that is of type Dimensions. The object is a Circle.

A Dimension reference can refer to any derived type (Circle, Rectangle, etc.).

Using the base reference has some advantages and some disadvantages.

The main advantage is that it makes your code more flexible. For example,
you can write generic methods like this:

double Area(Dimensions d) { ... }

Since the parameter is the base reference so you can pass in any derived
type - and calling virtual functions inside the method gives you the whole
polymorphism thing.

The disadvantage is that you are limited in what you can do to the objects
through a base reference. In your example, you can only access stuff declared
in the Dimensions class and not anything that is Circle specific (eg you
couldn't get/set the radius of the Circle using a Dimension reference).

This is a big topic that requires about an hour's worth of discussion to do
it justice, but hopefully that's enough to get you started.
Nov 16 '05 #3
Thanks guys, that really cleared up a lot.

Nov 16 '05 #4

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

Similar topics

2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
35
by: MuZZy | last post by:
Hi All, I got a issue here and hope someone can help me: Let's consider this code: // =================== CODE START ================================= using System; using System.Data; ...
0
by: brian.mills | last post by:
I have a web service in a technology called Jade. It is returning a StudentObject, which has an array of Application Objects on it. The Application objects never are actually Application Instances,...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
0
by: porter_wss | last post by:
I am a bit confused on serializing objects and inherited objects. What I am trying to accomplish is creating a base object and an inherited object with additional properties. The sender of these...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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,...
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.