c#实验5.1:类的继承和使用

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

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            double x1,y1,x2,y2,x3,y3;
            string s1, s2, s3, s4, s5, s6;
            Console.WriteLine("请输入两点的坐标:");
            s1 = Console.ReadLine();
            s2 = Console.ReadLine();
            s3 = Console.ReadLine();
            s4 = Console.ReadLine();
            x1 = int.Parse(s1);
            y1 = int.Parse(s2);
            x2 = int.Parse(s3);
            y2 = int.Parse(s4);
            CPoint c1 = new CPoint(x1, y1);
            CPoint c2 = new CPoint(x2, y2);
            double sum;
            Cline l = new Cline();
            sum=l.getdis(c1, c2);
            Console.WriteLine("两点间的距离为:" + sum);
            Console.WriteLine("请输入第三个点的坐标:");
            s5 = Console.ReadLine();
            s6 = Console.ReadLine();
            x3 = int.Parse(s5);
            y3 = int.Parse(s6);
            CPoint c3=new CPoint(x3,y3);
            CRect cr = new CRect();
            double zc = cr.getzc(c1, c2, c3);
            Console.WriteLine("矩形的周长为:"+zc);
            double area = cr.getarea(c1, c2, c3);
            Console.WriteLine("矩形的面积为:" + area);
            Console.ReadKey();
        }
    }
    class CPoint
    {
        public double x, y;
        public CPoint()
        {
        }
        public CPoint(double xx, double yy)
        {
            x = xx;
            y = yy;
        }
    }
    class Cline : CPoint
    {
        public Cline()
        {
        }
        public Cline(CPoint c1, CPoint c2)
        {

        }
        public double getdis(CPoint c1, CPoint c2)
        {
            double sum;
            return sum = Math.Sqrt((c1.x - c2.x)*(c1.x-c2.x) + (c1.y -c2.y)*(c1.y-c2.y));
        }
    }
    class CRect : Cline
    {
        public CRect()
        {
        }
        public double getzc(CPoint c1,CPoint c2,CPoint c3)
        {
            return 2 * (getdis(c1, c2) + getdis(c2,c3));
        }
        public double getarea(CPoint c1, CPoint c2, CPoint c3)
        {
            return getdis(c1, c2) * getdis(c2, c3);
        }
    }
}


 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值