派生类求两点间的距离、矩形的周长和面积

问题及代码:

平面直角坐标系上的一点类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。

要求成员函数能求出两点间的距离、矩形的周长和面积等。

using System;
using System.Collections.Generic;
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)
        {
            Console.Write("x1=");
            int x1 = int.Parse(Console.ReadLine());
            Console.Write("y1=");
            int y1 = int.Parse(Console.ReadLine());
            Console.Write("x2=");
            int x2 = int.Parse(Console.ReadLine());
            Console.Write("y2=");
            int y2 = int.Parse(Console.ReadLine());
            CPoint n1 = new CPoint(x1, y1);
            CPoint n2 = new CPoint(x2, y2);
            Console.WriteLine("两点之间的距离为:{0}", CPoint.Distance(n1, n2));

            Console.Write("x1=");
            x1 = int.Parse(Console.ReadLine());
            Console.Write("y1=");
            y1 = int.Parse(Console.ReadLine());
            Console.Write("x2=");
            x2 = int.Parse(Console.ReadLine());
            Console.Write("y2=");
            y2 = int.Parse(Console.ReadLine());
            CPoint n3 = new CPoint(x1, y1);
            CPoint n4 = new CPoint(x2, y2);
            Cline a = new Cline(n1, n2);
            Cline b = new Cline(n2, n4);
            CRect rect = new CRect(a, b);
            Console.WriteLine("由点({0},{1})和点({2},{3})组成的", x1,y1,x2,y2);
            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();
        }
    }
}

运行结果:


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值