C#类的继承

把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。
要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整的程序。

提示:(1)可能用到的函数:math类中的函数
            (2)注意数据类型的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class CPoint
    {
        private double x;
        private double y;
        public CPoint() { }
        public CPoint(double x, double y) { this.x = x; this.y = y; }
        public static double Distance(CPoint n1, CPoint n2)
        {
            return Math.Abs(Math.Pow(n1.x - n2.x, 2) + Math.Pow(n1.y - n2.y, 2));
        }

    }
    class Cline : CPoint
    {
        private CPoint n1 ;
        private CPoint n2 ;
        public static double operator *(Cline a, Cline b)
        {
            return CPoint.Distance(a.n1, a.n2) * CPoint.Distance(a.n1, a.n2);

        }
        public static double operator +(Cline a, Cline b)
        {
            return CPoint.Distance(a.n1, a.n2) + CPoint.Distance(a.n1, a.n2);
        }
        public Cline() { }
        public Cline(CPoint n1, CPoint n2) { this.n1 = n1; this.n2 = n2; }

    }
    class CRect : Cline
    {
        Cline a;
        Cline b;
        public CRect() { }
        public CRect(Cline a, Cline b) { this.a = a; this.b = b; }
        public double Perimeter( )
        {
            return (a + b)*2;
        }
        public double Area( )
        {
            return a * b;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CPoint n1 = new CPoint(5, 6);
            CPoint n2 = new CPoint(9, 9);
            Console.WriteLine("两点之间的距离为:{0}", CPoint.Distance(n1, n2));
            CPoint n3 = new CPoint(5, 10);
            CPoint n4 = new CPoint(9, 14);
            Cline a = new Cline(n1, n2);
            Cline b = new Cline(n2, n4);
            CRect rect = new CRect(a, b);
            Console.WriteLine("矩形的长为:{0}", CPoint.Distance(n1, n2));
            Console.WriteLine("矩形的宽为:{0}", CPoint.Distance(n2, n4));
            Console.WriteLine("矩形的面积为:{0}",rect.Area() );
            Console.WriteLine("矩形的周长为:{0}", rect.Perimeter());
            Console.ReadKey();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值