Mega Code Archive

 
Categories / C# / Class Interface
 

Show name hiding in a derived class

/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Hide.cs -- Show name hiding in a derived class // //            Compile this program with the following command line: //                C:>csc Hide.cs // namespace nsHide {     using System;     using System.Reflection;          public class Hide     {         static public void Main ()         {             clsBase Base = new clsBase();             clsDerived Derived = new clsDerived ();             Base.x = 42;             Derived.x = 42;             Console.WriteLine ("For the base class:");             Console.WriteLine ("\tThe type stored in clsBase is " + Base.TypeOf());             Console.WriteLine ("\tMathOp () returns {0,0:F3} for {1}", Base.MathOp(42), 42);             Console.WriteLine ("\r\nFor the derived class:");             Console.WriteLine ("\tThe type stored in clsDerived is " + Derived.TypeOf());             Console.WriteLine ("\tMathOp () returns {0,0:F3} for {1}", Derived.MathOp(42), 42);         }     }     class clsBase     {         protected int m_x;         public int x         {            get {return (x);}            set {m_x = value;}         }         public double MathOp (int val)         {             return (Math.Sqrt ((double) val));         }         public string TypeOf ()         {             return ("integer");         }     }     class clsDerived : clsBase     {         new protected double m_x;         new public double x         {            get {return (x);}            set {m_x = value;}         }         new public double MathOp (int val)         {             return ((double) (val * val));         }         new public string TypeOf ()         {             return ("long");         }     } }