Advertisement
NAK

Polymorphism (C#)

NAK
Dec 5th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Create some objects
  13.             DrawingObject[] dObj = new DrawingObject[2];
  14.  
  15.             dObj[0] = new Circle(50);
  16.             dObj[1] = new Square(10,10);
  17.  
  18.             foreach (DrawingObject drawObj in dObj)
  19.             {
  20.                 switch (drawObj.GetType().Name)
  21.                 {
  22.                     case "Circle":
  23.                         Circle circle = (Circle)drawObj;
  24.                         Console.WriteLine (string.Format("I'm a {0} with area of {1} derived from {2}", circle.Draw(), circle.Area(), circle.DrawBase()) );
  25.                         break;
  26.                     case "Square":
  27.                         Square square = (Square)drawObj;
  28.                         Console.WriteLine (string.Format("I'm a {0} with area of {1} derived from {2}", square.Draw(), square.Area(), square.DrawBase()) );
  29.                         break;
  30.                     default:
  31.                         break;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     abstract class DrawingObject
  38.     {
  39.     // Area() function must be over ridden in derived classes to provide specific functionality
  40.         public abstract double Area();
  41.     // DrawBase will be common to all derived classes
  42.         public  string DrawBase()
  43.         {
  44.            return "DrawingObject";
  45.         }
  46.     }
  47.  
  48.     class Square : DrawingObject
  49.     {
  50.         public int Width { get; set; }
  51.         public int Length { get; set; }
  52.  
  53.         public Square(int width, int length)
  54.         {
  55.             Width = width;
  56.             Length = length;
  57.         }
  58.  
  59.         public  string Draw()
  60.         {
  61.             return "Square";
  62.         }
  63.  
  64.         public override double Area ()
  65.         {
  66.             return (Width * Length);
  67.         }
  68.     }
  69.  
  70.     class Circle : DrawingObject
  71.     {
  72.         public int Radius { get; set; }
  73.  
  74.         public Circle(int radius)
  75.         {
  76.             this.Radius = radius;
  77.         }
  78.  
  79.         public  string Draw()
  80.         {
  81.             return "Circle";
  82.         }
  83.  
  84.       public override double Area ()
  85.       {
  86.          return (Math.Pow(Radius,2)*Math.PI);
  87.       }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement