Circle area: 12.5663706143592
Circle circumference: 12.5663706143592
Sphear top: 10
Sphear Left: 0
Sphear Front: 15
Sphear Volume: 3141.59265358979
Sphear surface area: 1256.63706143592
Sphear circumference: 62.8318530717959



Notes: Pretty simple example. The lesson this example is trying to teach is inheritance, and abstracts.
  1. When accessing your base class, you must(should) reference the base class with: base. Personally I find it easier to read when that keyword is used.
  2. Noticed how I used "new" when using the Center method in the sphear class. This is to hide that method in the derived class..
  3. Circle is based off of the abstract class Shape. Override keywords are needed when overriding members of the abstract class





Front end code

    8             <asp:Literal ID="litOutput" runat="server" />



Source Code.

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 namespace BicNet.Projects.mcp.chapter3

   12 {

   13     public partial class CircleClick : System.Web.UI.Page

   14     {

   15         protected void Page_Load(object sender, EventArgs e)

   16         {

   17             System.Text.StringBuilder sbOutput = new System.Text.StringBuilder();

   18             Circle myCircle = new Circle();

   19             myCircle.Radius = 2;

   20             myCircle.Center(10, 2);

   21             sbOutput.Append("Circle area: " + myCircle.Area() + "<br />");

   22             sbOutput.Append("Circle circumference: " + myCircle.Perimeter() + "<br />");

   23             Sphear mySphear = new Sphear();

   24             mySphear.Radius = 10;

   25             mySphear.Center(10,20,25);

   26             sbOutput.Append("Sphear top: " + mySphear.Top + "<br/>");

   27             sbOutput.Append("Sphear Left: " + mySphear.Left + "<br/>");

   28             sbOutput.Append("Sphear Front: " + mySphear.Front + "<br/>");

   29             sbOutput.Append("Sphear Volume: " + mySphear.Volume() + "<br/>");

   30             sbOutput.Append("Sphear surface area: " + mySphear.Area() + "<br/>");

   31             sbOutput.Append("Sphear circumference: " + mySphear.Perimeter() + "<br/>");

   32 

   33             litOutput.Text = sbOutput.ToString();

   34         }

   35     }

   36     public class Sphear : Circle

   37     {

   38         double fzCenter = 0;

   39 

   40         public Sphear()

   41         {

   42             fzCenter = 0;

   43         }

   44 

   45         public override double Area()

   46         {

   47             return (double)(4 * System.Math.PI * Math.Pow((double)base.Radius, 2));

   48         }

   49 

   50         public new void Center(double x, double y)

   51         {

   52             this.Center(x, y, 0);

   53         }

   54 

   55         public void Center(double x, double y, double z)

   56         {

   57             base.Center(x, y);

   58             fzCenter = z;

   59         }

   60 

   61         public double Volume()

   62         {

   63             return (double)((4 / 3) * System.Math.PI * Math.Pow((double)base.Radius, 3));

   64         }

   65 

   66         public double Front

   67         {

   68             get { return fzCenter - base.Radius; }

   69             set { fzCenter = value + base.Radius; }

   70         }

   71     }

   72     public class Circle : Shape

   73     {

   74         double fxCenter = 0, fyCenter = 0, fRadius = 0;

   75 

   76         public Circle()

   77         {

   78             fxCenter = 0;

   79             fyCenter = 0;

   80             fRadius = 0;

   81         }

   82 

   83 

   84         public override double Top

   85         {

   86             get { return fyCenter - fRadius; }

   87             set { fyCenter = value + fRadius; }

   88         }

   89 

   90         public override double Left

   91         {

   92             get { return fxCenter - fRadius; }

   93             set { fxCenter = value + fRadius; }

   94         }

   95 

   96         public double Radius

   97         {

   98             get { return fRadius; }

   99             set { fRadius = value; }

  100         }

  101 

  102         public virtual void Center(double x, double y)

  103         {

  104             fxCenter = x;

  105             fyCenter = y;

  106         }

  107 

  108         public override double Area()

  109         {

  110             return (double)(System.Math.PI * Math.Pow((double)fRadius, 2));

  111         }

  112 

  113         public override double Perimeter()

  114         {

  115             return 2 * fRadius * (double)System.Math.PI;

  116         }

  117     }

  118 

  119     public abstract class Shape

  120     {

  121         public Shape()

  122         { }

  123 

  124         public abstract double Top

  125         {

  126             get;

  127             set;

  128         }

  129 

  130         public abstract double Left

  131         {

  132             get;

  133             set;

  134         }

  135 

  136         public abstract double Area();

  137 

  138         public abstract double Perimeter();

  139     }

  140 

  141 }