定义直角坐标系上的一个点的类作为基类

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:00:36
定义直角坐标系上的一个点的类作为基类,派生出描述一条直线的类(两点坐标确定一直线),再派生出三角形类(三点坐标确定一个三角形),要求成员函数能求出两点间的距离、三角形的周长和面积等。设计一个测试程序,并构成完整的程序。

sing System;
public class CPoint
{
private double x;
private double y;
public CPoint() { }
public CPoint(double x, double y)
{
this.x = x;
this.y = y;
}
public double getx()
{ return x; }
public double gety()
{ return y; }
public void print()
{ Console.Write("(" + x + "," + y + ")"); }
}
public class CLine : CPoint
{
CPoint p1, p2;
public CLine() { }
public CLine(CPoint p1, CPoint p2)
{
this.p1 = p1;
this.p2 = p2;
}
public double distance()
{ return System.Math.Sqrt(System.Math.Pow(p1.getx() - p2.getx(), 2) + System.Math.Pow(p1.gety() - p2.gety(), 2)); }
}
public class CRect : CLine
{
CLine l1, l2;
public CRect(CLine l1, CLine l2)
{
this.l1 = l1;