版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cd1202/article/details/51234613
把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整的程序。
提示:
(1) 可能用到的函数:Math类中的函数
(2) 注意数据类型的转换。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Cpoint
{
protected double x;
protected double y;
public void set(double a, double b)
{
x = a;
y = b;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
}
class Cline : Cpoint//直线类
{
public double ShowLength(Cpoint p1,Cpoint
p2)//求两点间距离
{
return Math.Sqrt((p1.getx() - p2.getx())
* (p1.getx() - p2.getx()) + (p1.gety() - p2.gety())
* (p1.gety() - p2.gety()));
}
}
class CRect : Cpoint//矩形类
{
public double showdecline(Cpoint p1, Cpoint
p2)//求矩形周长
{
return 2*(Math.Abs(p1.getx-p2.getx
())+Math.Abs(p1.gety()-p2.gety()));
}
public double showarea(Cpoint p1, Cpoint
p2)//求矩形面积
{
return (Math.Abs(p1.getx() - p2.getx())
* Math.Abs(p1.gety() - p2.gety()));
}
}
class Program
{
static void Main(string[] args)
{
int[] point = new int[4];
Console.WriteLine("输入两个点:");
for (int i = 0; i < 4; i++)
{
point[i] = int.Parse
(Console.ReadLine());
}
Cpoint c1 = new Cpoint();
c1.set(point[0], point[1]);
Cpoint c2 = new Cpoint();
c2.set(point[2], point[3]);
Cline c1 = new Cline();
double length = c1.ShowLength(c1,c2);
Console.WriteLine("该两点之间的长度为:
{0}",length);
CRect cr = new CRect();
Console.WriteLine("以该两点为对角线所组
成的矩形周长:{0},面积:{1}", cr.showarea(c1, c2),
cr.showlength(c1, c2));
Console.ReadKey();
}
}
}