Advertisement
Avele

Untitled

Apr 20th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shapes
  4. {
  5.     abstract public class Shape
  6.     {
  7.         protected double area;
  8.         protected string name;
  9.         protected Shape(string name)
  10.         {
  11.             this.name = name;
  12.         }
  13.         public abstract double Area();
  14.     }
  15.     public class Square : Shape
  16.     {
  17.         public Square(int a, string name) : base(name)
  18.         {
  19.             area = a * a;
  20.         }
  21.         public override double Area()
  22.         {
  23.             return area;
  24.         }
  25.     }
  26.     public class Circle : Shape
  27.     {
  28.         public Circle(int r, string name) : base(name)
  29.         {
  30.             this.area = Math.PI * r * r;
  31.         }
  32.         public override double Area()
  33.         {
  34.             return area;
  35.         }
  36.     }
  37.     public class Rectangle : Shape
  38.     {
  39.         public Rectangle(int a, int b, string name) : base(name)
  40.         {
  41.             this.name = name;
  42.             this.area = a * b;
  43.         }
  44.         public override double Area()
  45.         {
  46.             return area;
  47.         }
  48.     }
  49.  
  50.     public class ShapesCollection
  51.     {
  52.         Shape shape;
  53.         string name;
  54.         public ShapesCollection(string name, Shape shape)
  55.         {
  56.             this.shape = shape;
  57.             this.name = name;
  58.         }
  59.         public override string ToString()
  60.         {
  61.             return name + "Area = " + shape.Area().ToString();
  62.         }
  63.     }
  64.     class Program
  65.     {
  66.         static void Main(string[] args)
  67.         {
  68.             ShapesCollection[] shapes = new ShapesCollection[3]
  69.             {
  70.                 new ShapesCollection("Square", new Square(5, "Square")),
  71.                 new ShapesCollection("Circle", new Circle(3, "Circle")),
  72.                 new ShapesCollection("Rectangle", new Rectangle(4, 5, "Rectangle"))
  73.             };
  74.             System.Console.WriteLine("Shapes Collection");
  75.             foreach (ShapesCollection s in shapes)
  76.             {
  77.                 System.Console.WriteLine(s);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement