![]() |
#2
m21wo2010-09-26 22:37
这是我写的!希望对你有用!
![]() #include <iostream> #include <cmath> using namespace std; class point { public: double x; double y; point(double x1=0,double y1=0):x(x1),y(y1) {} void draw() { cout<<"point的x坐标值是:"<<x<<" point的y坐标值是:"<<y<<endl; } }; class line:public point { private: point p1; point p2; public: line(point xp1,point xp2):p1(xp1.x,xp1.y),p2(xp2.x,xp2.y) {} void draw() { cout<<"line的起点是point的x坐标值是:"<<p1.x<<" point的y坐标值是:"<<p1.y<<endl; cout<<"line的终点为point的x坐标值是:"<<p2.x<<" point的y坐标值是:"<<p2.y<<endl; } double Distance() { double m=(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y); return sqrt(m); } }; class rect :public point { private: point u1; point u2; public: rect(point ru1,point ru2):u1(ru1.x,ru1.y),u2(ru2.x,ru2.y) {} void draw() { cout<<"rect的起点为point的x坐标值是:"<<u1.x<<" point的y坐标值是:"<<u1.y<<endl; cout<<"rect的终点为point的x坐标值是:"<<u2.x<<" point的y坐标值是:"<<u2.y<<endl; } double Area() { return fabs((u2.x-u1.x)*(u2.y-u1.y)); } }; int main() { point p(2,3); point p11(3,8); point p21(7,5); line l(p11,p21); rect r(p11,p21); p.draw(); l.draw(); r.draw(); cout<<"线的长度为:"<<l.Distance()<<endl; cout<<"矩形的面积为:"<<r.Area()<<endl; } |
自己定义三个类即点、线和矩形,通过带参数的构造函数完成对对象的初始化赋值,三个类中都要实现一个显示的操作,要求该操作能够完成显示点和线(两个端点)的坐标数值。线的类中除了显示的操作外还要有一个显示线段长度的操作。矩形的类中除了显示的操作外还要有一个显示矩形面积的操作。
类是这样的:
point : Private:x,y public:void draw()
line:Private:point p1,p2 public:void draw() Distance()
rect:Private:point u1,u2 public:void draw() Area()
主函数是这样的:
#include <iostream>
using namespace std;
void main()
{
point p(2,3);
point p11(3,8),
p21(7,5);
line l(p11,p21);
rect r(p11,p21);
p.draw();
l.draw();
r.draw();
cout<<"线的长度为:"<<l.Distance()<<endl;
cout<<"矩形的面积为:"<<r.Area()<<endl;
}
输出之后是这样的:
point的x坐标值是:2 point的y坐标值是:3
line的起点是point的x坐标值是:3 point的y坐标只是:8
line的终点为point的x坐标值是:7 point的y坐标值是:5
rect的起点为point的x坐标值是:3 point的y坐标值是:8
rect的重点为point的x坐标值是:7 point的y坐标值是:5
线的长度为:5
矩形的面积为:12
请大家帮帮忙,把这个程序补充完整,谢谢,今晚就要