Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create some objects
- DrawingObject[] dObj = new DrawingObject[2];
- dObj[0] = new Circle(50);
- dObj[1] = new Square(10,10);
- foreach (DrawingObject drawObj in dObj)
- {
- switch (drawObj.GetType().Name)
- {
- case "Circle":
- Circle circle = (Circle)drawObj;
- Console.WriteLine (string.Format("I'm a {0} with area of {1} derived from {2}", circle.Draw(), circle.Area(), circle.DrawBase()) );
- break;
- case "Square":
- Square square = (Square)drawObj;
- Console.WriteLine (string.Format("I'm a {0} with area of {1} derived from {2}", square.Draw(), square.Area(), square.DrawBase()) );
- break;
- default:
- break;
- }
- }
- }
- }
- abstract class DrawingObject
- {
- // Area() function must be over ridden in derived classes to provide specific functionality
- public abstract double Area();
- // DrawBase will be common to all derived classes
- public string DrawBase()
- {
- return "DrawingObject";
- }
- }
- class Square : DrawingObject
- {
- public int Width { get; set; }
- public int Length { get; set; }
- public Square(int width, int length)
- {
- Width = width;
- Length = length;
- }
- public string Draw()
- {
- return "Square";
- }
- public override double Area ()
- {
- return (Width * Length);
- }
- }
- class Circle : DrawingObject
- {
- public int Radius { get; set; }
- public Circle(int radius)
- {
- this.Radius = radius;
- }
- public string Draw()
- {
- return "Circle";
- }
- public override double Area ()
- {
- return (Math.Pow(Radius,2)*Math.PI);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement