Class Hierarchy with two children class

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;

class Window
{
// constructor takes two integers to
// fix location on the console
public Window(int top, int left)
{
this.top = top;
this.left = left;
}

// simulates drawing the window
public virtual void DrawWindow()
{
Console.WriteLine(“Window: drawing Window at {0}, {1}”,
top, left);
}

// these members are protected and thus visible
// to derived class methods. We'll examine this
// later in the chapter
protected int top;
protected int left;

}

// ListBox derives from Window
class ListBox : Window
{
// constructor adds a parameter
public ListBox(
int top,
int left,
string contents):
base(top, left) // call base constructor
{

listBoxContents = contents;
}

// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow()
{
base.DrawWindow(); // invoke the base method
Console.WriteLine (“Writing string to the listbox: {0}”,
listBoxContents);
}

private string listBoxContents; // new member variable
}

class Button : Window
{
public Button(
int top,
int left):
base(top, left)
{
}

// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow()
{
Console.WriteLine(“Drawing a button at {0}, {1}
“,
top, left);
}
}

public class TesterClassArray1
{
static void Main()
{
Window win = new Window(1,2);
ListBox lb = new ListBox(3,4,”Stand alone list box”);
Button b = new Button(5,6);
win.DrawWindow();
lb.DrawWindow();
b.DrawWindow();

Window[] winArray = new Window[3];
winArray[0] = new Window(1,2);
winArray[1] = new ListBox(3,4,”List box in array”);
winArray[2] = new Button(5,6);

for (int i = 0;i < 3; i++) { winArray[i].DrawWindow(); } } } [/csharp]

Class Hierarchy test


   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;

 class Window
 {
     // constructor takes two integers to
     // fix location on the console
     public Window(int top, int left)
     {
         this.top = top;
         this.left = left;
     }

     // simulates drawing the window
     public void DrawWindow()
     {
         Console.WriteLine("Drawing Window at {0}, {1}",
             top, left);
     }

     // these members are private and thus invisible
     // to derived class methods; we&#039;ll examine this
     // later in the chapter
     private int top;
     private int left;
 }

 // ListBox derives from Window
 class ListBox : Window
 {
     // constructor adds a parameter
     public ListBox(
         int top,
         int left,
         string theContents):
         base(top, left)  // call base constructor
     {
         mListBoxContents = theContents;
     }

     // a new version (note keyword) because in the
     // derived method we change the behavior
     public new void DrawWindow()
     {
         base.DrawWindow();  // invoke the base method
         Console.WriteLine ("Writing string to the listbox: {0}",
             mListBoxContents);
     }
     private string mListBoxContents;  // new member variable
 }

 public class HierarchyTester
 {
     public static void Main()
     {
         // create a base instance
         Window w = new Window(5,10);
         w.DrawWindow();

         // create a derived instance
         ListBox lb = new ListBox(20,30,"Hello world");
         lb.DrawWindow();
     }
 }

           
          


Illustrates versioning


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example7_5.cs illustrates versioning
*/

using System;


// declare the MotorVehicle class
class MotorVehicle
{

  // declare the fields
  public string make;
  public string model;

  // define a constructor
  public MotorVehicle(string make, string model)
  {
    this.make = make;
    this.model = model;
  }

  // define the Accelerate() method
  public virtual void Accelerate()
  {
    Console.WriteLine("In MotorVehicle Accelerate() method");
    Console.WriteLine(model + " accelerating");
  }

}


// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{

  // define a constructor
  public Car(string make, string model) :
  base(make, model)
  {
    // do nothing
  }

  // define the Accelerate() method (uses the new keyword to
  // tell the compiler a new method is to be defined)
  public new void Accelerate()
  {
    Console.WriteLine("In Car Accelerate() method");
    Console.WriteLine(model + " accelerating");
  }

}


public class Example7_5
{

  public static void Main()
  {

    // create a Car object
    Console.WriteLine("Creating a Car object");
    Car myCar = new Car("Toyota", "MR2");

    // call the Car object&#039;s Accelerate() method
    Console.WriteLine("Calling myCar.Accelerate()");
    myCar.Accelerate();

  }

}


           
          


Private field and public Property in inheritance


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsInherit
{
    using System;
    
    public class clsMainInherit
    {
        static public void Main ()
        {
            clsDerived derived = new clsDerived();
            derived.Property = 42;
            derived.ShowField();
        }
    }
//
// Define a base class with a private field and a public Property
    class clsBase
    {
        private int m_Field;
        public int Property
        {
            get {return (m_Field);}
            set {m_Field = value;}
        }
        public void ShowField ()
        {
            Console.WriteLine ("The value of m_Field is " + m_Field);
        }
    }
//
// Define a derived class that inherits from the clsBase
    class clsDerived : clsBase
    {
// For now, the derived class needs no members
    }
}


           
          


illustrates inheritance


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example7_1.cs illustrates inheritance
*/

using System;


// declare the MotorVehicle class (the base class)
class MotorVehicle
{

  // declare the fields
  public string make;
  public string model;

  // define a constructor
  public MotorVehicle(string make, string model)
  {
    this.make = make;
    this.model = model;
  }

  // define a method
  public void Start()
  {
    Console.WriteLine(model + " started");
  }

}


// declare the Car class (derived from the MotorVehicle base class)
class Car : MotorVehicle
{

  // declare an additional field
  public bool convertible;

  // define a constructor
  public Car(string make, string model, bool convertible) :
  base(make, model)  // calls the base class constructor
  {
    this.convertible = convertible;
  }

}


// declare the Motorcycle class (derived from the MotorVehicle base class)
class Motorcycle : MotorVehicle
{

  // declare an additional field
  public bool sidecar;

  // define a constructor
  public Motorcycle(string make, string model, bool sidecar) :
  base(make, model)  // calls the base class constructor
  {
    this.sidecar = sidecar;
  }

  // define an additional method 
  public void PullWheelie()
  {
    Console.WriteLine(model + " pulling a wheelie!");
  }

}


public class Example7_1
{

  public static void Main()
  {

    // declare a Car object, display the object&#039;s fields, and call the
    // Start() method
    Car myCar = new Car("Toyota", "MR2", true);
    Console.WriteLine("myCar.make = " + myCar.make);
    Console.WriteLine("myCar.model = " + myCar.model);
    Console.WriteLine("myCar.convertible = " + myCar.convertible);
    myCar.Start();

    // declare a Motorcycle object, display the object&#039;s fields, and call the
    // Start() method
    Motorcycle myMotorcycle = new Motorcycle("Harley-Davidson", "V-Rod", false);
    Console.WriteLine("myMotorcycle.make = " + myMotorcycle.make);
    Console.WriteLine("myMotorcycle.model = " + myMotorcycle.model);
    Console.WriteLine("myMotorcycle.sidecar = " + myMotorcycle.sidecar);
    myMotorcycle.Start();
    myMotorcycle.PullWheelie();

  }

}

           
          


Demonstrate when constructors are called


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Demonstrate when constructors are called. 
 
using System; 
 
// Create a base class. 
class A { 
  public A() {  
    Console.WriteLine("Constructing A."); 
  } 
} 
 
// Create a class derived from A. 
class B : A { 
  public B() { 
    Console.WriteLine("Constructing B."); 
  } 
} 
 
// Create a class derived from B. 
class C : B { 
  public C() { 
    Console.WriteLine("Constructing C."); 
  } 
} 
 
public class OrderOfConstruction { 
  public static void Main() {
    C c = new C(); 
  } 
}


           
          


A multilevel hierarchy 1


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A multilevel hierarchy. 
 
using System; 
 
class TwoDShape { 
  double pri_width;  // private 
  double pri_height; // private  
 
  // Default constructor. 
  public TwoDShape() { 
    width = height = 0.0; 
  } 
 
  // Constructor for TwoDShape. 
  public TwoDShape(double w, double h) { 
    width = w; 
    height = h; 
  } 
 
  // Construct object with equal width and height. 
  public TwoDShape(double x) { 
    width = height = x; 
  } 
 
  // Properties for width and height. 
  public double width { 
     get { return pri_width; } 
     set { pri_width = value; } 
  } 
 
  public double height { 
     get { return pri_height; } 
     set { pri_height = value; } 
  } 
 
  public void showDim() { 
    Console.WriteLine("Width and height are " + 
                       width + " and " + height); 
  } 
} 
 
// A derived class of TwoDShape for triangles. 
class Triangle : TwoDShape { 
  string style; // private 
   
  /* A default constructor. This invokes the default 
     constructor of TwoDShape. */ 
  public Triangle() { 
    style = "null"; 
  } 
 
  // Constructor 
  public Triangle(string s, double w, double h) : base(w, h) { 
    style = s;  
  } 
 
  // Construct an isosceles triangle. 
  public Triangle(double x) : base(x) { 
    style = "isosceles";  
  } 
 
  // Return area of triangle. 
  public double area() { 
    return width * height / 2; 
  } 
 
  // Display a triangle&#039;s style. 
  public void showStyle() { 
    Console.WriteLine("Triangle is " + style); 
  } 
} 
 
// Extend Triangle. 
class ColorTriangle : Triangle { 
  string color; 
 
  public ColorTriangle(string c, string s, 
                       double w, double h) : base(s, w, h) { 
    color = c; 
  } 
 
  // Display the color. 
  public void showColor() { 
    Console.WriteLine("Color is " + color); 
  } 
} 
 
public class Shapes6 { 
  public static void Main() { 
    ColorTriangle t1 =  
         new ColorTriangle("Blue", "right", 8.0, 12.0); 
    ColorTriangle t2 =  
         new ColorTriangle("Red", "isosceles", 2.0, 2.0); 
 
    Console.WriteLine("Info for t1: "); 
    t1.showStyle(); 
    t1.showDim(); 
    t1.showColor(); 
    Console.WriteLine("Area is " + t1.area()); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("Info for t2: "); 
    t2.showStyle(); 
    t2.showDim(); 
    t2.showColor(); 
    Console.WriteLine("Area is " + t2.area()); 
  } 
}