Problem E: 平面上的点和线——Point类、Line类 (VI)

  • 注意因为对象的传递而引起的不必要的输出,使用引用。
  • 常对象调用常成员函数,返回值为常量。
  • 需要将对象最为返回值时,返回对象的引用比较稳妥。
#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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值