Skip to main content

C#: Program #40 (Oop5)

using System;


namespace OOP5
{
    class Program
    {
        static void Main(string[] args)
        {
            Shape[] shapes = {new Square(4),new Circle(4),new RECTANGLE(4,3)};
            foreach (Shape s in shapes)
            {
                s.Getinformation();
                Console.WriteLine("{0} has area of {1:f2}",s.Name,s.Area());
                Circle objcircle = s as Circle;
                if (objcircle == null)
                {
                    Console.WriteLine("This is not a circle");

                }
                if(s is Circle)
                {
                    Console.WriteLine("This is a circle");
                }

                object sq=new Square(5);
                Square sq1 = (Square)sq;
                Console.WriteLine("{0} has area of {1:f2}", sq1.Name, sq1.Area());
               

            }



            Console.ReadKey();
        }
    }

 abstract class Shape
    {
        public string Name { get; set;}
        public virtual void Getinformation(){
        Console.WriteLine("{0} Name is",Name);
        }
        public abstract double Area();

    }
class Square:Shape
    {
          public double Length { get; set;}
       
        public Square(double l)
        {
            Name = "Square";
            Length = l;
         
        }
        public override void Getinformation()
        {
           base.Getinformation();
           Console.WriteLine("The Length is {0}",Length);
        }
        public override double Area()
        {
            return Length*Length;
        }
    }
class Circle:Shape
    {
        public double Radius { get; set;}
        public Circle(double radius)
        {
            Name = "Circle";
            Radius = radius;

        }
        public override void Getinformation()
        {
            base.Getinformation();
            Console.WriteLine("The Radius is {0}", Radius);
        }
        public override double Area()
        {
            return 2*Math.PI*Math.Pow(Radius,2);
        }
    }
class RECTANGLE:Shape
    {

        public double Length { get; set;}
        public double Width { get; set;}
        public RECTANGLE(double l,double w)
        {
            Name = "Rectangle";
            Length = l;
            Width = w;
        }
        public override void Getinformation()
        {
           base.Getinformation();
           Console.WriteLine("The Length is {0} and width is {1}",Length,Width);
        }
        public override double Area()
        {
            return Length*Width;
        }

    }









}

Comments

Popular posts from this blog

C#: Program #38 (Oop4)

using System; namespace Oop_Inheritance_ {     class Program     {         static void Main(string[] args)         {             Animal obj=new Animal("Balto","Howl");             obj.setAnimalIdInfo(1,"Balto");             obj.getAnimalInfo();             Dog obj2 = new Dog("Hachico", "howl", "Grrrr");             obj2.Sound="Woof Woof";             obj2.setAnimalIdInfo(5, "Hachico");             obj2.getAnimalInfo();             Console.WriteLine(obj2.Name);             Console.WriteLine(obj2.Sound);             Animal sloth = new Dog("Simon", "bark", "woof");             sloth.M...