Information Technology Reference
In-Depth Information
public class
Circle
:
Shape
{
private
PointF
center;
private float
radius;
public
Circle() :
this
(
PointF
.Empty,
0
)
{
}
public
Circle(
PointF
c,
float
r)
{
center = c;
radius = r;
}
public override void
Draw()
{
//...
}
static public implicit operator
Ellipse
(
Circle
c)
{
return new
Ellipse
(c.center, c.center,
c.radius, c.radius);
}
}
Now that you've got the implicit conversion operator, you can use a Circle
anywhere an Ellipse is expected. Furthermore, the conversion happens
automatically:
public static double
ComputeArea(
Ellipse
e)
{
// return the area of the ellipse.
return
e.R1 * e.R2 *
Math
.PI;
}
// call it:
Circle
c1 =
new
Circle
(
new
PointF
(
3.0f
,
0
),
5.0f
);
ComputeArea(c1);