Lesson11-Inheritance-Abstract-Classes The GeometricObject case
GeometricObject class public abstract class GeometricObject private string color = "White"; private DateTime datecreated = new DateTime(2017, 12, 31, 23, 59, 59); private bool filled = false; // Properties (classic style) public string Color get return color; set string temp = value.toupper(); char firstletter = temp[0]; color = firstletter + temp.substring(1).tolower(); public DateTime DateCreated get return datecreated; private set datecreated = value; public bool Filled get return filled; set filled = value; //constructors public GeometricObject() Color = "White"; Filled = true; DateCreated = DateTime.Now; public GeometricObject(string colorvalue, bool filledvalue) Color = colorvalue; Filled = filledvalue; DateCreated = DateTime.Now; //user-defined methods public override string ToString() return string.format($"geometricobject [Color: Color, " + $"Filled:Filled, " + $"DateCreated:DateCreated.ToString("yyy-MM-dd")]"); //Observe that teh following two methods have no body! //Descendants of the GeometricObject class MUST implement them. public abstract double GetArea(); public abstract double GetArea(int numdecimals); public abstract double GetPerimeter(); public virtual int CalculateCost() //an arbitrary way to assess figure's cost int cost = 0; if (Color == "Gold" Color=="Silver") cost += 300;
if (filled) cost += 200; return cost; Circle class class Circle : GeometricObject //data member(s) private double radius = 0; //properties public double Radius get return radius; set radius = Math.Abs(value); //this is an alternative way of defining method GetArea() //public double Area // // get return Math.PI * Math.Pow(Radius, 2); // //constructor(s) public Circle() : base() Radius = 0; public Circle(string colorvalue, bool filledvalue, double radiusvalue) :base(colorvalue, filledvalue) Radius = radiusvalue; //user-defined methods public override string ToString() //showing two possible versions of the ToString() method int option = 1; switch (option) case 1: //OPTION1. mixed message child + ancestor string ancestormsg = base.tostring(); string childmsg1 = string.format($"circle [Radius: Radius ] "); return childmsg1 + " child of " + ancestormsg; case 2: //OPTION2. only child's msg string childmsg2 = string.format($"circle [Radius:Radius, " + $"Color:Color, Filled:Filled, " +
return childmsg2; $"Created:DateCreated ]"); return "Circle"; //The following three methods override the empty definition imposed by ancestor class. //The base class demanded that its ABSTRACT METHODS must be implemented by its children. public override double GetArea() //NOTE: this method could be implemented instead as a read-only property //(see above defintion of property Area) return Math.PI * Math.Pow(Radius, 2); public override double GetArea(int numdecimals) //display area using a given number of decimal positions double area = Math.PI * Math.Pow(Radius, 2); string numformat = "0:N" + numdecimals + ""; string strarea = string.format(numformat, area); return Convert.ToDouble(strArea); public override double GetPerimeter() return 2 * Math.PI * Radius; //Circle //namespace Rectangle class class Rectangle : GeometricObject //data members private double height = 0; private double width = 0; //properties public double Width get return width; set width = value; public double Height
get return height; set height = value; //constructors public Rectangle() : base() Height = 0; Width = 0; public Rectangle(double widthvalue, double heightvalue, string colorvalue, bool filledvalue) :base(colorvalue, filledvalue) Width = widthvalue; Height = heightvalue; //user defined methods public override string ToString() return string.format($"rectangle [Height:Height:N2, Width:Width:N2, " + $"Color:Color, Filled:Filled, " + $"Created:DateCreated ]"); //The following three methods override the empty definition imposed by ancestor class. //The base class demanded that its ABSTRACT METHODS must be implemented by its children. public override double GetArea() return Height * Width; public override double GetArea(int numdecimals) //display area using a given number of decimal positions double area = Height * Width; string numformat = "0:N" + numdecimals + ""; string strarea = string.format(numformat, area); return Convert.ToDouble(strArea); public override double GetPerimeter() return 2 * (Height + Width); Program class class Program static void Main(string[] args)
//this declaration is invalid when GeometricObject is abstract //GeometricObject g1 = new GeometricObject("Blue", true); //Console.WriteLine(g1); Circle c1 = new Circle("Red", true, 2); Console.WriteLine(c1); Circle c2 = new Circle(); Console.WriteLine(c2); Rectangle r1 = new Rectangle(); Console.WriteLine(r1); Rectangle r2 = new Rectangle(4.33, 2.1789, "Gold", true); Console.WriteLine(r2); Console.WriteLine("r2 area 0", r2.getarea()); Console.WriteLine("r2 area 0", r2.getarea(2)); Console.WriteLine("r2 perimeter 0", r2.getperimeter()); Console.WriteLine("r2 cost 0", r2.calculatecost()); Console.Read(); Console