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'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()); 
  } 
}


           
          


Call a hidden method


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// Call a hidden method. 
 
using System; 
 
class A { 
  public int i = 0; 
 
  // show() in A 
  public void show() { 
    Console.WriteLine("i in base class: " + i); 
  } 
} 
 
// Create a derived class. 
class B : A { 
  new int i; // this i hides the i in A 
 
  public B(int a, int b) { 
    base.i = a; // this uncovers the i in A 
    i = b; // i in B 
  } 

  // This hides show() in A. Notice the use of new. 
  new public void show() { 
    base.show(); // this calls show() in A 
 
    // this displays the i in B 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
public class UncoverName123 { 
  public static void Main() { 
    B ob = new B(1, 2); 
 
    ob.show(); 
  } 
}


           
          


Using base to overcome name hiding


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// Using base to overcome name hiding. 
 
using System; 
 
class A { 
  public int i = 0; 
} 
 
// Create a derived class. 
class B : A { 
  new int i; // this i hides the i in A 
 
  public B(int a, int b) { 
    base.i = a; // this uncovers the i in A 
    i = b; // i in B 
  } 
 
  public void show() { 
    // this displays the i in A. 
    Console.WriteLine("i in base class: " + base.i); 
 
    // this displays the i in B 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
public class UncoverName1231 { 
  public static void Main() { 
    B ob = new B(1, 2); 
 
    ob.show(); 
  } 
}


           
          


An example of inheritance-related name hiding


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// An example of inheritance-related name hiding. 
 
using System; 
 
class A { 
  public int i = 0; 
} 
 
// Create a derived class. 
class B : A { 
  new int i; // this i hides the i in A 
 
  public B(int b) { 
    i = b; // i in B 
  } 
 
  public void show() { 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
public class NameHiding { 
  public static void Main() { 
    B ob = new B(2); 
 
    ob.show(); 
  } 
}


           
          


Four layers of class hierarchy


   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class MyMainClass3
  {
    static void Main(string[] args)
    {
      //The function called is based
      //upon the type called by new.
      A MyA = new D();
      B MyB = new C();

      MyA.Display();    //Calls D Display  
      MyB.Display();    //Calls C Display
      // followed by B's Display //via the base keyword
    }
  }
  class A
  {
    public virtual void Display()
    {
      Console.WriteLine("Class A's Display Method");
    }
  }
  class B: A
  {
    public override void Display()
    {
      Console.WriteLine("Class B's Display Method");
    }
  }
  class C: B
  {
    public override void Display()
    {
      Console.WriteLine("Class C's Display Method");
      base.Display();
    }
  }
  class D: C
  {
    public override void Display()
    {
      Console.WriteLine("Class D's Display Method");
    }
  }

}

           
          


Inheritance 3


   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class InheritanceChapter_5___Building_Your_Own_Classes
  {
    static void Main(string[] args)
    {
      B MyB = new D();
      D MyD = new D();

      //Both result in in D's instance of Display being //called
      MyB.Display();
      MyD.Display();
    }
  }
  public class B
  {
    public virtual void Display()
    {
      Console.WriteLine("Class B's Display Method");
    }
  }
  public class C: B
  {
    public override void Display()
    {
      Console.WriteLine("Class C's Display Method");
    }
  }
  public class ContainedClass
  {
    int MyInt = 0;
  }
  public class D: C
  {
    public ContainedClass MyClass = new ContainedClass();
    public override void Display()
    {
      Console.WriteLine("Class D's Display Method");
    }
  }

}