C#プログラミング基礎実験(4)



平面直角座標系上の一点を定義するクラスCPointをベースクラスとし,一直線を記述するクラスClineを派生させ,矩形クラスCRectを派生させる.
コードは次のとおりです.
using System;

namespace Program0
{
    class CPoint
    {
        public double x { get; set; }
        public double y { get; set; }
        public CPoint() { }
        public CPoint(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
     class CLine : CPoint
    {
    public CPoint EPoint{get;set;}
    public double x2{get{return EPoint.x;}set{EPoint.x=value;}}
    public double y2{get{return EPoint.y;}set{EPoint.y=value;}}
     
    public CLine()
    {
        EPoint=new CPoint();
    }
     
    public CLine(CPoint p1,CPoint p2)
    {
        base.x=p1.x;
        base.y=p1.y;
        EPoint=p2;
    }
     
    public double LineLength{
        get{
            return Math.Sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));
        }
    }
    }
    class CRect :CLine
    {
       
        public double Perimeter
        {
            get
            {
                return 2 * (Math.Abs(x2 - x) + Math.Abs(y2 - y));
            }
        }
        public double Area
        {
            get
            {
                return Math.Abs((x2 - x) * (y2 - y));
            }
        }
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            CRect rect = new CRect() {x = 1, y = 2, x2 = 0, y2 = 1 };
            Console.WriteLine(rect.LineLength);
            Console.WriteLine(rect.Perimeter);
            Console.WriteLine(rect.Area);
            
            Console.ReadKey();
        }
    }
    
}