How would I implement an IInterface class with this code that calculates area of circle, rectangle, triangle?

1.8k views Asked by At

I need to further my knowledge of IInterface classes used with abstract classes. Whether this stuff is ever used have work requirements to learn a little more about it.

What I think an interface is (this works and calculates area for 3 items, Rectangle, Triangle, Circle using values provided):

static void Main(string[] args)
{
    Rectangle r = new Rectangle(4, 8, "Rectangle");

    Triangle t = new Triangle(4, 4, "Triangle");

    Circle c = new Circle(3, "Circle");

    r.DisplayArea();
    Console.WriteLine();
    t.DisplayArea();
    Console.WriteLine();
    c.DisplayArea();
    Console.ReadKey();
}

Here is my abstract class. It looks like it deals with the name of what is being measured:

namespace DrawShapes
{
    abstract class Shape
    {
        public string name;

        public abstract void DisplayArea();

        public Shape(string name)
        {
            this.name = name;
        }
    }

I'm not positively sure but wouldn't these be in the IInterface class and get set or called somehow? Definitely not an expert at this object oriented stuff.

public int Radius { get; set; }
public int Height { get; set; }
public int Width { get; set; }

Instead, I am hardcoding the parameters into the "function call":

static void Main(string[] args)
{
    Rectangle r = new Rectangle(4, 8, "Rectangle");

    Triangle t = new Triangle(4, 4, "Triangle");

    Circle c = new Circle(3, "Circle");
}

I personally can't think of why or how I would use IInterface.

Just to be complete and maybe help someone who is looking for stuff like this, here are my classes to calculate areas.There are other SO answers to calculate areas, this question is about IInterface and abstract classes:

namespace DrawShapes
{
    class Rectangle : Shape
    {
        public int length;
        public int width;

        public Rectangle(int length, int width, string name) : base(name)
        {
            //this.angles = angles;
            this.length = length;
            this.width = width;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write(length * width);
        }
    }

    class Triangle : Shape
    {
        // public int angles;
        public int width;
        public int height;

        public Triangle(int width, int height, string name) : base(name)
        {
            //this.angles = angles;
            this.width = width;
            this.height = height;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write((width * height) / 2);
        }
    }

    class Circle : Shape
    {
        public int radius;

        public Circle(int radius, string name) : base(name)
        {
            this.radius = radius;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write(3.22 * radius * radius);
        }
    }
}
0

There are 0 answers