Demonstrates the use of a simple interface

image_pdfimage_print

   

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

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// IntrFace.cs -- demonstrates the use of a simple interface
//
//                Compile this program with the following command line:
//                    C:>csc IntrFace.cs
using System;

namespace nsInterface
{
    interface IPlane
    {
        double Area
        {
            get;
        }
    }
    interface ISolid
    {
        double Volume
        {
            get;
        }
    }

    class clsCircle : IPlane
    {
        public clsCircle (double radius)
        {
            m_Radius = radius;
        }
        public double Area
        {
            get {return (3.14159 * m_Radius * m_Radius);}
        }
        private double m_Radius;


        public override string ToString ()
        {
            return ("Area = " + Area);
        }
    }
    class clsSphere : IPlane, ISolid
    {
        public clsSphere (double radius)
        {
            m_Radius = radius;
        }
        public double Area
        {
            get {return (4 * 3.14159 * m_Radius * m_Radius);}
        }
        public double Volume
        {
            get {return (4 * 3.14159 * m_Radius * m_Radius * m_Radius / 3);}
        }
        private double m_Radius;

        public override string ToString ()
        {
            return ("Area = " + Area + ", " + "Volume = " + Volume);
        }
    }

    public class IntrFace
    {
        static public void Main ()
        {
            clsCircle circle = new clsCircle (14.2);
            clsSphere sphere = new clsSphere (16.8);
            Console.WriteLine ("For the circle: " + circle);
            Console.WriteLine ("For the sphere: " + sphere);
        }
    }
}