Declaring inherited class objects

G

Guest

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/default.asp?url=/library/en-us/csref/html/vclrfvirtualpg.asp
2)
http://msdn.microsoft.com/library/d.../en-us/csref/html/vcwlkversioningtutorial.asp
3)
http://msdn.microsoft.com/library/d.../en-us/csspec/html/vclrfcsharpspec_10_5_3.asp
 
J

John Wood

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 said:
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/default.asp?url=/library/en-us/csref/html/vclrfvirtualpg.asp
http://msdn.microsoft.com/library/d.../en-us/csref/html/vcwlkversioningtutorial.asp
3)
http://msdn.microsoft.com/library/d.../en-us/csspec/html/vclrfcsharpspec_10_5_3.asp
 
G

Guest

Mark Bremmer said:
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.
 

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

Top