Create Simple inheritance between Rectangle and block - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Description

Create Simple inheritance between Rectangle and block

Demo Code

using System;/*from  w ww .j av  a  2  s . com*/
using System.Text;
class Rectangle
{
   protected int length;
   protected int width;
   public Rectangle()
   {
      length = width = 1;
   }
   public Rectangle(int L, int W)
   {
      length = L;
      width = W;
   }
   public void display()
   {
      Console.WriteLine("From:Sams Teach Yourself C# in 21 Days");
      Console.WriteLine(" Width:  {0}", width);
      Console.WriteLine(" Length: {0}", length);
      Console.WriteLine(" Area:   {0}", getArea());
   }
   public int getArea()
   {
      return( length * width );
   }
}
class block : Rectangle
{
   private int depth;
   // ToDo: Add properties to access data members
   public block() : base()
   {
      depth = 1;
   }
   public block( int L, int W, int D ) : base( L, W )
   {
      depth = D;
   }
   public new void display()
   {
      Console.WriteLine(" Width:  {0}", width);
      Console.WriteLine(" Length: {0}", length);
      Console.WriteLine(" Depth:  {0}", depth);
      Console.WriteLine(" Area:   {0}", getArea());
      Console.WriteLine(" Volume: {0}", getVolume());
   }
   public new int getArea()
   {
      return(  (2*(length * width)) + (2*(width * depth)) + (2*(depth * length)) );
   }
   public int getVolume()
   {
      return( length * width * depth );
   }
}
class RectApp
{
   public static void Main()
   {
      block block1 = new block();
      block block2 = new block(4, 3, 2);
      block1.display();
      block2.display();
   }
}

Result


Related Tutorials