using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { // an abstract class with (undefined) methods abstract class Shape { abstract public double Area (); } // the Suqare class derives from shape, but overrides Area class Square : Shape { public static int no = 0; public static double area = 0.0; public double Width { get ; set ; } public override double Area() { return this.Width * this.Width; } public Square(double x) { this.Width = x ; Square.no++; Square.area += this.Area(); } } // the Circle class derives from shape, but overrides Area // Hint: use Math.PI class Circle : Shape { public static int no = 0; public static double area = 0.0; public double Radius { get ; set ; } public override double Area() { return Math.PI * this.Radius * this.Radius; } public Circle(double r) { this.Radius = r ; Circle.no++; Circle.area += this.Area(); } } // the Rectangle class derives from shape, but overrides Area class Rectangle : Shape { public static int no = 0; public static double area = 0.0; public double Width { get ; set ; } public double Height { get ; set ; } public override double Area() { return this.Width * this.Height ; } public Rectangle(double w, double h) { this.Width = w ; this.Height = h ; Rectangle.no++; Rectangle.area += this.Area(); } } // the main class class Program { private static double TotalArea(Shape[] arr, int n) { double res = 0.0; for (int i = 0; i < n; i++) { Console.WriteLine("Current element: " + arr[i].ToString()); res += arr[i].Area(); } return res; } static void Main(string[] args) { // Just one object is used like this (doesn't need overloading): Circle c = new Circle(3); Console.Write("This is a cricle of radius {0} covering an area of {1}", c.Radius, c.Area()); // this will NOT work: you can't instantiate an abstract class // Shape q = new Shape(); // this container can be used with different geometric objects Shape[] all = new Shape[80]; int i = 0; all[i++] = c; Console.Write("Enter shape (c for circle, s for square, r for rectangle, q to quit):"); string str = Console.ReadLine(); while (str.CompareTo("q") != 0) { switch (str) { case "c": { Console.Write("Enter radius of circle: "); string str1 = Console.ReadLine(); int x = Convert.ToInt32(str1); all[i++] = new Circle(x); break; } case "s": { Console.Write("Enter width of square: "); string str1 = Console.ReadLine(); int x = Convert.ToInt32(str1); all[i++] = new Square(x); break; } case "r": { Console.Write("Enter width of rectangle: "); string str1 = Console.ReadLine(); int w = Convert.ToInt32(str1); Console.Write("Enter height of rectangle: "); str1 = Console.ReadLine(); int h = Convert.ToInt32(str1); all[i++] = new Rectangle(w, h); break; } default: Console.WriteLine("Unknown input {0}", str); break; } Console.Write("Enter shape (c for circle, s for square, r for rectangle, q to quit):"); str = Console.ReadLine(); } Console.WriteLine("Created {0} circles, {1} squares and {2} rectangles", Circle.no, Square.no, Rectangle.no); Console.WriteLine("Area of all circles: {0}; area of all squares: {1}; area of all rectangles {2}", Circle.area, Square.area, Rectangle.area); double total = Circle.area + Square.area + Rectangle.area; Console.WriteLine("Total area is {0}", total); Console.WriteLine("Total area computed by TotalArea method is {0}", TotalArea(all,i)); } } class ProgramFixed { static void MainFixed(string[] args) { Square sq1 = new Square(5); Square sq2 = new Square(2); Rectangle rec1 = new Rectangle(4, 5); Console.WriteLine("Created {0} squares and {1} rectangles", Square.no, Rectangle.no); Console.WriteLine("Area of all squares: {0}; area of all rectangles {1}", Square.area, Rectangle.area); double w = sq1.Area() + sq2.Area() + rec1.Area(); double w0 = Square.area + Rectangle.area; if (w == w0) { Console.WriteLine("Total area is {0}", w); } else { Console.WriteLine("Something went wrong computing the total area"); } } } }