hi friends,
i have placed code because i want to explain my doubt clearly to
you ,and i have placed my doubt in Main of the program (static void
Main(string[] args)) by // before that
using System;
namespace AbstractClasses
{
abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
class Circle : Shape
{
public double Radius;
public override double GetArea()
{
return Math.PI * Radius * Radius;
}
public override double GetPerimeter()
{
return 2 * Math.PI * Radius;
}
}
class Rectangle : Shape
{
public double Width, Height;
public override double GetArea()
{
return Width * Height;
}
public override double GetPerimeter()
{
return 2 * (Width + Height);
}
}
class Class1
{
static void Main(string[] args)
{
Shape s = new Shape();
Shape [] shapes = new Shape[2];
Circle c = new Circle();
c.Radius=5;
Rectangle r = new Rectangle();
r.Width=8;
r.Height=6;
shapes[0]=c;//what we can achiev by assigning object c to
shapes reference ,is there any special use in
having abstract class like this [donot consider
this example alone]
shapes[1]=r;
double total = 0;
foreach (Shape s in shapes)
total += s.GetArea();
Console.WriteLine("The total area is {0}", total);
}
}
}