Click here to Skip to main content
15,892,927 members
Home / Discussions / C#
   

C#

 
GeneralI2C communication Pin
yyyychan4-Mar-04 21:05
yyyychan4-Mar-04 21:05 
GeneralRe: I2C communication Pin
Heath Stewart5-Mar-04 6:19
protectorHeath Stewart5-Mar-04 6:19 
GeneralQuestion about Casting and Inheritance Pin
tsunsau7174-Mar-04 19:37
tsunsau7174-Mar-04 19:37 
GeneralRe: Question about Casting and Inheritance Pin
partyganger4-Mar-04 20:50
partyganger4-Mar-04 20:50 
GeneralRe: Question about Casting and Inheritance Pin
tsunsau7174-Mar-04 21:44
tsunsau7174-Mar-04 21:44 
GeneralRe: Question about Casting and Inheritance Pin
partyganger5-Mar-04 10:10
partyganger5-Mar-04 10:10 
GeneralRe: Question about Casting and Inheritance Pin
Roman Rodov4-Mar-04 23:55
Roman Rodov4-Mar-04 23:55 
GeneralRe: Question about Casting and Inheritance Pin
killerslaytanic5-Mar-04 14:23
killerslaytanic5-Mar-04 14:23 
Seems to me you have some misunderstanding about the concepts:

First of all the methods "f" belong to which class(es)?
Second: The typecast (shape)a2 is redundant (don´t remember if it is syntactically ok but if so it has nonsense anyway)a2 is a Square so it IS a Shape anyway

how inheritance and polymorphism works...
I guess you wanted to do something like this:
You have the classes and their respective methods:


class Shape<br />
{<br />
   virtual void f{} to treat the Shape object (Whatever it be) who owns it<br />
}<br />
<br />
class Square : Shape <br />
{<br />
 override void f{}//supposedly to treat the Square object who owns it<br />
<br />
}<br />
<br />
class Circle : Shape <br />
{<br />
 override void f{}//supposedly to treat the Circle object who owns it<br />
}


if you do
Shape a1 = new Shape();
Square a2 = new Square();
Circle a3 = new Circle();

Logically,
a1.f() will call the Shape´s f method; a2.f() the Square´s, etc

BUT ALSO if you do this....( and that´s the nice part)

 Shape a1 ;<br />
 Square a2 = new Square();<br />
 Circle a3 = new Circle();<br />
a1= a2;<br />
a1.f(); //Will call the Square´s f method<br />
a1= a3;<br />
a1.f(); //Will call te Circle´s f method


which is something similar to what you said and is the usual way in which these kind of things are accomplished

This happens because the fmethod is declared virtual in Shape class an is overriden in the child classes otherwise, if they weren´t virtual then the calls of a1.f() always would call the Shape´s f method

Notice this approach is specially useful in this case

Class TestShapes{

void DoSomethingWithShapes(Shape s)
{
s.f();
//it will call the proper f method regarding if s is in fact a Square or a Circle or a general Shape (if this class is conceived to have instances ,thing that generally shouldn´t be taht way)

}
}


Maybe it helps
QuestionHow to associate icons with ContextMenu ? Pin
nigs_krec4-Mar-04 19:25
nigs_krec4-Mar-04 19:25 
AnswerRe: How to associate icons with ContextMenu ? Pin
partyganger4-Mar-04 20:41
partyganger4-Mar-04 20:41 
QuestionHow to access parent form objects Pin
hongheo764-Mar-04 19:05
hongheo764-Mar-04 19:05 
AnswerRe: How to access parent form objects Pin
Roman Rodov4-Mar-04 19:13
Roman Rodov4-Mar-04 19:13 
GeneralIt doesn't work Pin
hongheo764-Mar-04 19:44
hongheo764-Mar-04 19:44 
GeneralRe: It doesn't work Pin
4-Mar-04 20:05
suss4-Mar-04 20:05 
GeneralRe: It doesn't work Pin
partyganger4-Mar-04 20:52
partyganger4-Mar-04 20:52 
GeneralIt 's working now Pin
hongheo765-Mar-04 6:56
hongheo765-Mar-04 6:56 
Questionhow to move bar like MSMessenger Pin
yu-yu4-Mar-04 17:48
yu-yu4-Mar-04 17:48 
AnswerRe: how to move bar like MSMessenger Pin
John Fisher5-Mar-04 8:48
John Fisher5-Mar-04 8:48 
GeneralRe: how to move bar like MSMessenger Pin
yu-yu5-Mar-04 11:59
yu-yu5-Mar-04 11:59 
Questiongetting disc space ?? Pin
azusakt4-Mar-04 15:13
azusakt4-Mar-04 15:13 
AnswerRe: getting disc space ?? Pin
Roman Rodov4-Mar-04 18:41
Roman Rodov4-Mar-04 18:41 
GeneralRe: getting disc space ?? Pin
azusakt4-Mar-04 19:32
azusakt4-Mar-04 19:32 
GeneralRe: getting disc space ?? Pin
Roman Rodov4-Mar-04 23:41
Roman Rodov4-Mar-04 23:41 
GeneralDataGrid Checkbox Event capture Pin
Ruchi Gupta4-Mar-04 14:27
Ruchi Gupta4-Mar-04 14:27 
GeneralRe: DataGrid Checkbox Event capture Pin
Heath Stewart5-Mar-04 3:32
protectorHeath Stewart5-Mar-04 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.