求解这个main函数的倒数第一和倒数第三行
程序代码:
int main() { int num, i; Point p(1, -2), q(2, -1), t; t.show(); std::cin>>num; Line line[num]; for(i = 0; i < num; i++) { line[i].readLine(); line[i].show(); } Line l1(p, q), l2(p,t), l3(q,t), l4(l1); l1.show(); l2.setLine(l1).show(); l3.show(); l4.setLine(t,q).show(); }
这是我们oj上的一个题,大概就是构造类,输入输出点和线的。
但是最后X.f.f这里不明白要怎么实现了。
是要在函数里构造类吗?
PS:下面是我写的代码,还没有通过编译
程序代码:
#include <iostream> using namespace std; class Point { public: double x,y; Point (double xx = 0,double yy = 0) { x = xx;y = yy; cout<<"Point : ("<<x<<", "<<y<<") is created."<<endl; } void show() { cout<<"Point : ("<<x<<", "<<y<<')'<<endl; } }; class Line { //double x1,x2,y1,y2; Point a,b; public: Line () { cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<") is created."<<endl; } Line (const Point &aa,const Point &bb) { Point aaa = aa,bbb = bb; a = aaa;b = bbb; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<") is created."<<endl; } Line (const Line &q) { a = q.a; b = q.b; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<") is created."<<endl; } void setLine(double x1, double y1, double x2, double y2) { a.x = x1; a.y = y1; b.x = x2 ;b.y = y2; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } void setLine(const Point &p, const Point &q) { a.x = p.x; b.x = q.x; a.y = p.y; b.y = q.y; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } void setLine(const Line &q) { a = q.a; b = q.b; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } void readLine() { double x,y,xx,yy; cin>>x>>y>>xx>>yy; a.x = x; a.y = y; b.x = xx; b.y = yy; cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } void show() { cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } }; int main() { int num, i; Point p(1, -2), q(2, -1), t; t.show(); std::cin>>num; Line line[num]; for(i = 0; i < num; i++) { line[i].readLine(); line[i].show(); } Line l1(p, q), l2(p,t), l3(q,t), l4(l1); l1.show(); l2.setLine(l1).show(); l3.show(); l4.setLine(t,q).show(); }