Initializing Base Class - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Description

Initializing Base Class

Demo Code

using System;//from   ww w  .  j a  va2  s.co  m
class Rectangle {
   //member variables
   protected double length;
   protected double width;
   public Rectangle(double l, double w) {
      length = l;
      width = w;
   }
   public double GetArea() {
      return length * width;
   }
   public void Display() {
      Console.WriteLine("Length: {0}", length);
      Console.WriteLine("Width: {0}", width);
      Console.WriteLine("Area: {0}", GetArea());
   }
}
class Tabletop : Rectangle {
   private double cost;
   public Tabletop(double l, double w) : base(l, w) { }
   public double GetCost() {
      double cost;
      cost = GetArea() * 70;
      return cost;
   }
   public void Display() {
      base.Display();
      Console.WriteLine("Cost: {0}", GetCost());
   }
}
class MainClass{
   static void Main(string[] args) {
      Tabletop t = new Tabletop(4.5, 7.5);
      t.Display();
      Console.ReadLine();
   }
}

Related Tutorials