问题描述:
把点类CPoint作为基类,派生出描述一条直线的类CLine,再派生出矩形类CR。要求成员函数能求出两点间距离、矩形的周长和面积。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CPoint p1 = new CPoint(0, 0);
CPoint p2 = new CPoint(3, 4);
CRect r = new CRect(p1, p2);
CLine l2=new CLine();
Console.WriteLine("直线长度:{0}", CLine.Length(p1,p2));
Console.WriteLine("矩形周长:{0}", r.GetPerimeter());
Console.WriteLine("矩形面积:{0}", r.GetArea(p1,p2));
Console.ReadKey();
}
}
class CPoint
{
public double x;
public double y;
public CPoint() { }
public CPoint(double a, double b)
{
x = a; y = b;
}
}
class CLine : CPoint
{
public static double Length(CPoint p1, CPoint p2)
{
return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
}
class CRect : CPoint
{
CPoint p1=new CPoint();
CPoint p2=new CPoint();
public CRect(CPoint a, CPoint b)
{
p1.x=a.x;p1.y=a.y;
p2.x=b.x;p2.y=b.y;
}
public double GetPerimeter()
{
return (Math.Abs(p1.x-p2.x)*2+Math.Abs(p1.y-p2.y)*2);
}
public double GetPerimeter(CPoint p1, CPoint p2)
{
return (Math.Abs(p1.x - p2.x) * 2 + Math.Abs(p1.y - p2.y) * 2);
}
public double GetArea()
{
return (Math.Abs(p1.x - p2.x) * Math.Abs(p1.y - p2.y));
}
public double GetArea(CPoint p1, CPoint p2)
{
return (Math.Abs(p1.x - p2.x) * Math.Abs(p1.y - p2.y));
}
}
}