猿问

关于return 与 析构 和 拷贝 问题 输入4后输入8个点坐标 1,2 ...


#include<iostream>

using namespace std;

class Point{

public:

    Point():x_(0),y_(0){cout<<"Point : (0, 0) is created."<<endl;}

    Point(double x, double y):x_(x),y_(y){cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;}


    Point(const Point &p);

    ~Point();

    const double x()const{return x_;}

    const double y()const{return y_;}


    void show()const{

    cout << "Point : ("<<x_<<", "<<y_<<")"<<endl;

    }

   const void showNoEndOfLine()const{cout << "Point : ("<<x_<<", "<<y_<<")" ;}


    double x_, y_;


};

Point::Point(const Point &p):x_(p.x_),y_(p.y_){cout<<"Point : ("<<x_<<", "<<y_<<") is copied."<<endl;}

Point::~Point(){cout<<"Point : ("<<x_<<", "<<y_<<") is erased."<<endl;}


istream &operator>>(istream &is, Point &p){

    char z;

    is >> p.x_ >>z>> p.y_;

    return is;

}


class Line{

public:

    Line():sta_(0,0),ed_(0,0){cout<<"Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<") is created."<<endl;}

    Line(double x1,double y1,double x2,double y2 ):sta_(x1,y1),ed_(x2,y2){cout<<"Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<") is created."<<endl;}

    Line(Point &sta, Point &ed):sta_(sta),ed_(ed){cout<<"Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<") is created."<<endl;}

    Line(const Line &z);

    ~Line();



    Line& setLine(double x1,double y1,double x2,double y2){

    sta_.x_=x1;

    ed_.x_=x2;

    sta_.y_=y1;

    ed_.y_=y2;

    return *this;

    }

    Line& setLine(const Point &g, const Point &h){

    sta_.x_=g.x_;ed_.x_=h.x_;

    sta_.y_=g.y_;ed_.y_=h.y_;

    return *this;

    }

    Line& setLine(const Line &k){

    sta_.x_=k.sta_.x_;ed_.x_=k.ed_.x_;

    sta_.y_=k.sta_.y_;ed_.y_=k.ed_.y_;

    return *this;

    }

    void readLine(){cin>>sta_>>ed_;}


    void show()const{

    cout << "Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<")"<<endl;

    }


    Point setStart(Point &p){sta_.x_ = p.x_; sta_.y_ = p.y_;}

    Point setEnd(Point &q){ed_.x_ = q.x_; ed_.y_ = q.y_;}


//调用析构

//    Point& l = sta_;

//    Point& o = ed_;

//    Point& start()const{return l;}

//    Point& end()const{return o;}


    const Point& start()const{return sta_;}//调用拷贝

    const Point& end()const{return ed_;}


private:

    Point sta_, ed_;


};



Line::Line(const Line &z):sta_(z.sta_),ed_(z.ed_){//wo

        cout<<"Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<") is copied."<<endl;}

Line::~Line(){cout<<"Line : ("<<sta_.x_<<", "<<sta_.y_<<") to ("<<ed_.x_<<", "<<ed_.y_<<") is erased."<<endl;}




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);

}


ReZero
浏览 1876回答 1
1回答

Popper0912

代码写规范点,再来吧。。。
随时随地看视频慕课网APP
我要回答