![]() |
#2
Sornets2013-06-09 18:08
![]() #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; void show() { 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; void show() { cout<<"Line : ("<<a.x<<", "<<a.y<<") to ("<<b.x<<", "<<b.y<<")"<<endl; } } void setLine(const Line &q) { a = q.a; b = q.b; void show() { 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; } }; 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(); } 为什么这样就通不过编译了?我在每个setline函数里都加了个show函数,把show函数注释掉就通过编译了,但是好像没有什么语法错误。。 |

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();
}
{
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();
}
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();
}