Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Shapes
- {
- abstract public class Shape
- {
- protected double area;
- protected string name;
- protected Shape(string name)
- {
- this.name = name;
- }
- public abstract double Area();
- }
- public class Square : Shape
- {
- public Square(int a, string name) : base(name)
- {
- area = a * a;
- }
- public override double Area()
- {
- return area;
- }
- }
- public class Circle : Shape
- {
- public Circle(int r, string name) : base(name)
- {
- this.area = Math.PI * r * r;
- }
- public override double Area()
- {
- return area;
- }
- }
- public class Rectangle : Shape
- {
- public Rectangle(int a, int b, string name) : base(name)
- {
- this.name = name;
- this.area = a * b;
- }
- public override double Area()
- {
- return area;
- }
- }
- public class ShapesCollection
- {
- Shape shape;
- string name;
- public ShapesCollection(string name, Shape shape)
- {
- this.shape = shape;
- this.name = name;
- }
- public override string ToString()
- {
- return name + "Area = " + shape.Area().ToString();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ShapesCollection[] shapes = new ShapesCollection[3]
- {
- new ShapesCollection("Square", new Square(5, "Square")),
- new ShapesCollection("Circle", new Circle(3, "Circle")),
- new ShapesCollection("Rectangle", new Rectangle(4, 5, "Rectangle"))
- };
- System.Console.WriteLine("Shapes Collection");
- foreach (ShapesCollection s in shapes)
- {
- System.Console.WriteLine(s);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement