- 注意因为对象的传递而引起的不必要的输出,使用引用。
- 常对象调用常成员函数,返回值为常量。
- 需要将对象最为返回值时,返回对象的引用比较稳妥。
#include<iostream>
#include<iomanip>
using namespace std;
char c;
class Point{
public:
Point(double _xx ,double _yy) {
xx = _xx;
yy = _yy;
cout<<"Point : ("<<xx<<", "<<yy<<") is created."<<endl;
}
Point(double _x = 0){
xx = _x;
yy = _x;
cout<<"Point : ("<<xx<<", "<<yy<<") is created."<<endl;
}
Point(const Point &another){
xx = another.xx;
yy = another.yy;
cout<<"Point : ("<<xx<<", "<<yy<<") is copied."<<endl;
}
~Point(){
cout<<"Point : ("<<xx<<", "<<yy<<") is erased."<<endl;
}
void show(){
cout<<"Point : ("<<xx<<", "<<yy<<')'<<endl;
}
void _show()const{
cout<<"("<<xx<<", "<<yy<<')';
}
void set(double _xx,double _yy){
xx = _xx;
yy = _yy;
}
void set(const Point &ano){
xx = ano.xx;
yy = ano.yy;
}
double x()const{
return xx;
}
double y()const{
return yy;
}
void showNoEndOfLine()const{
cout<<"Point : ("<<xx<<", "<<yy<<')';
}
private:
double xx,yy;
};
class Line{
public:
Line(double x = 0,double y = 0,double _x = 0,double _y = 0){
p1.set(x,y);
p2.set(_x,_y);
cout<<"Line : ";
p1._show();
cout<<" to ";
p2._show();
cout<<" is created."<<endl;
}
Line(const Point &ano1,const Point &ano2):p1(ano1),p2(ano2){
cout<<"Line : ";
p1._show();
cout<<" to ";
p2._show();
cout<<" is created."<<endl;
}
Line(const Line &ano):p1(ano.p1),p2(ano.p2){
cout<<"Line : ";
p1._show();
cout<<" to ";
p2._show();
cout<<" is copied."<<endl;
}
void readLine(){
double x,y,_x,_y;
cin>>x>>c>>y>>_x>>c>>_y;
p1.set(x,y);
p2.set(_x,_y);
}
Line &setLine(double x,double y,double _x,double _y){
p1.set(x,y);
p2.set(_x,_y);
return *this;
}
Line &setLine(const Line &another){
p1.set(another.p1);
p2.set(another.p2);
return *this;
}
Line &setLine(const Point &ano1,const Point &ano2){
p1.set(ano1);
p2.set(ano2);
return *this;
}
void show()const{
cout<<"Line : ";
p1._show();
cout<<" to ";
p2._show();
cout<<endl;
}
~Line(){
cout<<"Line : ";
p1._show();
cout<<" to ";
p2._show();
cout<<" is erased."<<endl;
}
void setStart(const Point &ano){
p1.set(ano);
}
void setEnd(const Point &ano){
p2.set(ano);
}
const Point &start()const{
return p1;
}
const Point &end()const{
return p2;
}
private:
Point p1,p2;
};
void showLineCoordinate(const Line& line)
{
std::cout<<"Line : ";
std::cout<<"("<<line.start().x()<<", "<<line.start().y()<<")";
std::cout<<" to ";
std::cout<<"("<<line.end().x()<<", "<<line.end().y()<<")";
std::cout<<std::endl;
}
void showLinePoint(const Line& line)
{
std::cout<<"Line : ";
line.start().showNoEndOfLine();
std::cout<<" to ";
line.end().showNoEndOfLine();
std::cout<<std::endl;
}
void showLine(const Line& line)
{
line.show();
}
int main()
{
int num, i;
Point p(1, -2), q(2, -1), t;
t.show();
std::cin>>num;
Line line[num + 1];
for(i = 1; i <= num; i++)
{
line[i].readLine();
showLine(line[i]);
}
Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
showLineCoordinate(l1);
showLinePoint(l2);
showLinePoint(l3.setLine(l1));
showLineCoordinate(l4.setLine(t,q));
line[0].setStart(t);
line[0].setEnd(q);
}