Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 12-4 extracted from chapter Inheritance, polymorphism and abstraction


Listing 12-3<     > Listing 12-5


This listing can be compiled with the command line:
csc.exe /target:exe Example_12_4.cs
Errors: 1 Warnings: 0


Example_12_4.cs
class Point{
   public Point ( int x, int y ){ this.x = x; this.y = y; }
   int x; int y;
}
abstract class GeometricFigure{
   // As the Draw() method is abstract, it doesn't have a body.
   public abstract void Draw(); 
}
class Circle : GeometricFigure {
   private Point   m_Center;
   private double  m_Radius;
   public CirclePoint center, double radius ) {
      m_Center  = center;
      m_Radius  = radius;
   }
   public override void Draw (){
      // Draw the circle { m_Center ; m_Radius }.
   }
}
class Rectangle : GeometricFigure {
   private Point m_Vertice1, m_Vertice2, m_Vertice3;
   public RectanglePoint s1, Point s2, Point s3 ) {
      m_Vertice1 = s1; m_Vertice2 = s2; m_Vertice3 = s3;
   }
   public override void Draw (){
      // Draw the Rectangle { m_Vertice1; m_Vertice2; m_Vertice3 }.
   }
}
class Program {   
   static void Main() {
      GeometricFigure[] array = new GeometricFigure[3];
      array[0]=new Circlenew Point(0,0), 3.);
      array[1]=new Rectangle(new Point(0,0),new Point(0,2),new Point(1,2));
      array[2]=new Circlenew Point(1,1), 4.);
      // Thanks to polymorphism, 
      // proper versions of the Draw() method is called. 
      foreach(GeometricFigure f in array )
         f.Draw();
      // Compilation error!
      // We can't instantiate an abstract class !
      GeometricFigure figure = new GeometricFigure(); 
   }
}	
Copyright Patrick Smacchia 2006 2007