/*
几个小技巧:
1.ctrl + shift + c 对于全选的内容加上注释, +x 消除注释
2.关于直接在文件中读入和读到文件中:要先在存这个运行文件的地方建一个名为in的txt文件
#include<cstdio>
#define LOCAL
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
*/
/*第三次作业:类的组合问题
第一题:关于组合类的构造函数,在复合类的构造函数构建时,一定要采用初始化的方式
一定要建立构造函数。
第二题:关于构造函数和析构函数的调用次序
第三题:关于大类和小类的构造函数调用次序:先调用小类中的构造函数,在调用大类中的构造函数。 //用debug工具可以测试
第四题:大类也无法直接访问小类的私有成员,只能通过共有成员的接口来访问
第五题:对于文件中所输出内容的比较,用一系列的命令。嗯嗯。 cmd -> D: -> cd soft -> cd c++ -> cd study ->fc out.txt oout.txt
第六题:
*/
//数据成员就是一个类中直接定义的数据变量
#include<iostream>
using namespace std;
class Point
{
public:
Point()
{
x_ = 0;
y_ = 0;
cout << "Point : (" << x_ << ", " << y_ << ") is created." << endl;
}
Point(double x, double y)
{
x_ = x;
y_ = y;
cout << "Point : (" << x_ << ", " << y_ << ") is created." << endl;
}
Point(const Point& p)
{
x_ = p.x_;
y_ = p.y_;
cout << "Point : (" << x_ << ", " << y_ << ") is copied." << endl;
}
~Point()
{
cout << "Point : (" << x_ << ", " << y_ << ") is erased." << endl;
}
double x() const
{
return x_;
}
double y() const
{
return y_;
}
void showNoEndOfLine() const
{
cout << "Point : (" << x_ << ", " << y_ << ")";//Point : (1, -2)
}
void getX(double x)
{
x_ = x;
}
void getY(double y)
{
y_ = y;
}
void show()
{
cout << "Point : (" << x_ <<", " << y_ << ")" << endl;
}
private:
double x_, y_;
};
class Line
{
public:
Line():p_(),q_()
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ") is created." << endl;
}
/*Line(double x1, double y1, double x2, double y2):p_(x1, y1), q_(x2, y2)
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ") is created." << endl;
}
*/
//Line(Point &pp, Point &qq) :p_(pp.x(),pp.y()),q_(qq.x(),qq.y())
Line(Point &pp, Point &qq) :p_(pp),q_(qq)
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ") is created." << endl;
}
Line(const Line & ll):p_(ll.p_), q_(ll.q_)
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ") is copied." << endl;
}
~Line()
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ") is erased." << endl;
}
void show() const
{
cout << "Line : (" << p_.x() << ", " << p_.y() << ") to (" << q_.x() << ", " << q_.y() << ")" << endl;
}
/*void readLine()
{
double x1, y1, x2, y2;
char c;
cin>>x1>>c>>y1>>x2>>c>>y2;
p_.getX(x1);
p_.getY(y1);
q_.getX(x2);
q_.getY(y2);
}*/
Line& readLine()
{
double x1, y1, x2, y2;
char c;
cin>>x1>>c>>y1>>x2>>c>>y2;
p_.getX(x1);
p_.getY(y1);
q_.getX(x2);
q_.getY(y2);
return *this;
}
Line & setLine(Line & l)
{
p_ = l.p_;
q_ = l.q_;
return *this;
}
Line & setLine(Point &p, Point &q)
{
p_ = p;
q_ = q;
return *this;
}
Line & setLine(double x1, double y1, double x2, double y2)
{
p_.getX(x1);
p_.getY(y1);
q_.getX(x2);
q_.getY(y2);
return *this;
}
/*
Point start() const
{
return p_;
}
Point end() const
{
return q_;
}
*/
Point & start()
/*对于一个非常的类const 只加在后面不加在前面就会报错,
只加在前面不会报错,前后都加也不会报错,但是不能只加在后面,
所以我想必须要加在前面是因为const 类的定义,他的其中一个成员类必然也不可以改,
所以一定要返回常引用,因为它是Const类型的,所以一定要调用Const成员函数。*/
{
return p_;
}
//常量的地址只能赋给常指针,但是非常就没有这个限制了,引用是c++编译器偷偷帮我们用了指针。
//如果前面不加const修饰,就不能传给他,所以报错返回类型,但是如果不加在后面就会报错与const有关的错误。
//在C++中,只有被声明为const的成员函数才能被一个const类对象调用。
Point & end() const
{
return q_;
}
Line &setStart(const Point &pp)
{
p_ = pp;
return *this;
}
Line &setEnd(const Point &qq)
{
q_ = qq;
return *this;
}
private:
Point p_, q_;
};
void showLineCoordinate(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(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);
}
SDUST第三次作业
最新推荐文章于 2024-08-08 00:00:00 发布