C# Abstract class and overriding example

Program

using System;

namespace AbstractClass
{
    abstract class Shape
    {
        //protected string _name; will be implemented automatically
        protected string name { get; set; }
        public Shape(string name)
        {
            this.name = name;
        }

        public abstract float CalculateArea();
    }

    class Circle: Shape
    {
        //int _radius; will be implemented automatically
        public int radius { get; set; }
        public Circle(string name, int radius):base(name)
        {
            this.radius = radius;
        }
        public override float CalculateArea()
        {
            return 3.14f * (radius * radius);
        }
    }

    class Square: Shape
    {
        //int _side; will be implemented automatically
        public int side { get; set; }
        public Square(string name, int side): base(name)
        {
            this.side = side;
        }
        public override float CalculateArea()
        {
            return side * side;
        }
    }

    class Rectangle: Shape
    {
        //int _length,_breadth; will be implemented automatically
        public int length { get; set; }
        public int breadth { get; set; }
        public Rectangle(string name, int length, int breadth): base(name)
        {
            this.length = length;
            this.breadth = breadth;
        }
        public override float CalculateArea()
        {
            return length * breadth;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Circle\nSquare\nRectangle\n
                                       Enter the shape name");
            string ShapeName = Console.ReadLine().ToLower();
            switch (ShapeName)
            {
                case "circle":
                    Console.WriteLine("Enter the radius");
                    int radius = int.Parse(Console.ReadLine());

                    Circle refCir = new Circle("Circle", radius);
                    Console.WriteLine(refCir.CalculateArea()
                                                .ToString(".00"));
                     break;

                case "square":
                    Console.WriteLine("Enter the side");
                    int side = int.Parse(Console.ReadLine());

                    Square refSqr = new Square("Square", side);
                    Console.WriteLine(refSqr.CalculateArea()
                                                 .ToString(".00"));
                    break;

                case "rectangle":
                    Console.WriteLine("Enter the length");
                    int len = int.Parse(Console.ReadLine());
                    Console.WriteLine("Enter the breadth");
                    int bth = int.Parse(Console.ReadLine());

                    Rectangle refRect = new Rectangle("Rectangle", len,                                                                         bth);
                    Console.WriteLine(refRect.CalculateArea()
                                                .ToString(".00"));
                    break;

                default:
                    Console.WriteLine("Invalid input please check                                                                    spelling");
                    break;
            }
            Console.ReadKey();
        }
    }
}


Output 1

Circle
Square
Rectangle
Enter the shape name
circle
Enter the radius
25
1962.50

Output 2
Circle
Square
Rectangle
Enter the shape name
square
Enter the side
23
529.00

Output 3
Circle
Square
Rectangle
Enter the shape name
rectangle
Enter the length
45
Enter the breadth
60
2700.00

No comments:

Post a Comment