Show name hiding in a derived class : Class Definition « Class Interface « C# / C Sharp






Show name hiding in a derived class

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");
        }
    }
}


           
         
  








Related examples in the same category

1.simulate a bank account
2.Using Initializers
3.A simple inventory exampleA simple inventory example
4.Declaring and Defining Classes
5.Declaring Class Instances
6.Assign value to classAssign value to class
7.Declare class and use itDeclare class and use it
8.Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
9.Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
10.illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
11.Illustrates nested classesIllustrates nested classes
12.Multiple constructors in a class definitionMultiple constructors in a class definition
13.Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
14.Illustrates hidingIllustrates hiding
15.Variable in and out a classVariable in and out a class
16.Create classCreate class
17.A program that uses the Building classA program that uses the Building class
18.This program creates two Building objectsThis program creates two Building objects
19.Return an objectReturn an object
20.Uses a class from Example16_3a.cs
21.A Simple C# ClassA Simple C# Class
22.Persons Info class
23.Message class
24.Point class