PC Review


Reply
Thread Tools Rate Thread

Declaring inherited class objects

 
 
=?Utf-8?B?TWFyayBCcmVtbWVy?=
Guest
Posts: n/a
 
      4th Jan 2005
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
 
Reply With Quote
 
 
 
 
John Wood
Guest
Posts: n/a
 
      4th Jan 2005
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 (E-Mail Removed)> wrote in message
news:30BCCE51-1F91-40DA-9FCB-(E-Mail Removed)...
> 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


 
Reply With Quote
 
=?Utf-8?B?TWFya1Q=?=
Guest
Posts: n/a
 
      4th Jan 2005
"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.
 
Reply With Quote
 
=?Utf-8?B?TWFyayBCcmVtbWVy?=
Guest
Posts: n/a
 
      4th Jan 2005
Thanks guys, that really cleared up a lot.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing inherited control re:Ken Tucker - Inherited DataGrid Class Doug Bell Microsoft VB .NET 2 6th Oct 2004 10:07 PM
Class design issue: how to store unique inherited objects? J.J. Feminella Microsoft C# .NET 1 15th Jul 2004 10:37 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft C# .NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET Framework 7 3rd Jun 2004 02:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.