Advertisement
ChazPIerre

Polymorphism

Nov 16th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     public abstract class Shape
  7.     {
  8.         // Shape properties
  9.         public double X { get; private set; }
  10.         public double Y { get; private set; }
  11.  
  12.         // Virtual method
  13.         public virtual void Draw()
  14.         {
  15.             Console.WriteLine("Shape: Draw()");
  16.         }
  17.     }
  18.     class Circle : Shape
  19.     {
  20.         public double radius { get; private set; }
  21.  
  22.         public Circle(double radius)
  23.         {
  24.             this.radius = radius;
  25.         }
  26.  
  27.         public override void Draw()
  28.         {
  29.             Console.WriteLine("Circle: Draw() - Circumference: " + Circumference());
  30.             base.Draw();
  31.         }
  32.  
  33.         public double Circumference()
  34.         {
  35.             return (2 * this.radius) * Math.PI;
  36.         }
  37.     }
  38.     class Rectangle : Shape
  39.     {
  40.         public double height { get; private set; }
  41.         public double width { get; private set; }
  42.  
  43.         public Rectangle(double height, double width)
  44.         {
  45.             this.height = height;
  46.             this.width = width;
  47.         }
  48.  
  49.         public override void Draw()
  50.         {
  51.             Console.WriteLine("Rectangle: Draw() - Perimiter: " + Perimeter());
  52.             base.Draw();
  53.         }
  54.  
  55.         public double Perimeter()
  56.         {
  57.             return 2 * (this.height + this.width);
  58.         }
  59.     }
  60.     class Triangle : Shape
  61.     {
  62.         public double a { get; private set; }
  63.         public double b { get; private set; }
  64.         public double c { get; private set; }
  65.  
  66.         public Triangle(double a, double b, double c)
  67.         {
  68.             this.a = a;
  69.             this.b = b;
  70.             this.c = c;
  71.         }
  72.  
  73.         public override void Draw()
  74.         {
  75.             Console.WriteLine("Triangle: Draw() - Perimiter: " + Perimeter());
  76.             base.Draw();
  77.         }
  78.  
  79.         public double Perimeter()
  80.         {
  81.             return this.a + this.b + this.c;
  82.         }
  83.     }
  84.  
  85.     class Program
  86.     {
  87.         static void Main(string[] args)
  88.         {
  89.             // Universal Polymorphism at work #1: a Rectangle, Triangle and Circle
  90.             // can all be used whereever a Shape is expected. No cast is
  91.             // required because an implicit conversion exists from a derived
  92.             // class to its base class.
  93.             var shapes = new List<Shape>
  94.         {
  95.             new Rectangle(34.5, 62.0),
  96.             new Triangle(22.1, 36.9, 84.6),
  97.             new Circle(57)
  98.         };
  99.  
  100.             // Universal Polymorphism at work #2 (Overriding): the virtual method Draw is
  101.             // invoked on each of the derived classes, not the base class.
  102.             foreach (Shape shape in shapes)
  103.             {
  104.                 shape.Draw();
  105.                 Console.WriteLine();
  106.             }
  107.  
  108.             // Universal Polymorphism at work #3 (Parametric): the class ParametricPolymorphism defines
  109.             // a generic type that allows the programmer to instantiate an object with whatever generic type
  110.             // he/she chooses. The function ParametricPolymorphism.Add has been written so that it may account
  111.             // for any type that was passed to the generic.
  112.             ParametricPolymorphism<Shape> container = new ParametricPolymorphism<Shape>();
  113.  
  114.             foreach (Shape shape in shapes)
  115.             {
  116.                 container.Add(shape);
  117.             }
  118.  
  119.             // Ad-Hoc Polymorphism at work #1 (Overloading): we are able to pass in
  120.             // different types and amounts of arguments in our function calls because we have
  121.             // overloaded the function signature "AddNumbers" in our definition file. The One Definition Rule
  122.             // must still not be violated, so the compiler will rename the function to a unique name.
  123.             short f = 4;
  124.  
  125.             Console.WriteLine(AdHocPolymorphism.AddNumbers(3.5, 3.5));
  126.             Console.WriteLine(AdHocPolymorphism.AddNumbers(1, 2, 3.5));
  127.             Console.WriteLine(AdHocPolymorphism.AddNumbers(1.5F, 2, 3.5));
  128.  
  129.             // Ad-Hoc Polymorphism at work #2 (Coercion): Despite the arguments types
  130.             // do not correspond with the function parameter types, the program will still compile successfully
  131.             // because the compiler will insert neccessary code to make sure the conversion is successful.
  132.             Console.WriteLine(AdHocPolymorphism.AddNumbers(1L, 1F));
  133.             Console.WriteLine(AdHocPolymorphism.AddNumbers(f, f, 1U));
  134.  
  135.             Console.WriteLine("Press any key to exit.");
  136.             Console.ReadKey();
  137.         }
  138.  
  139.     }
  140. }
  141.  
  142. using System.Collections.Generic;
  143.  
  144. namespace ConsoleApp1
  145. {
  146.     class AdHocPolymorphism
  147.     {
  148.         public static double AddNumbers(double x, double y) { return x + y; }
  149.         public static double AddNumbers(int a, int b, double c) { return a + b + c; }
  150.         public static double AddNumbers(float a, int b, double c) { return a + b + c; }
  151.  
  152.     }
  153.  
  154.     class ParametricPolymorphism<T>
  155.     {
  156.         List<T> container = new List<T>();
  157.         public void Add(T x)
  158.         {
  159.             this.container.Add(x);
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement