说实话这一系列题,困难在于Point的get函数的写法。还有就是show函数其实重点难点就在这。其他按步骤来就好了。
#include <iostream>
using namespace std;
class Point
{
private :
double x,y;
public:
Point()
{
x = 0;
y = 0;
}
Point(double x_,double y_):x(x_),y(y_) {}
void show()
{
cout<<"Point : ("<<x<<", "<<y<<")"<<endl;
}
Point(Point &p)
{
x = p.x;
y = p.y;
}
double getx()
{
return x;
}
double gety()
{
return y ;
}
};
class Line
{
private :
Point p,q;
public:
Line(double a,double b,double c,double d):p(a,b),q(c,d) {}
void show()
{
cout<<"Line : ("<<p.getx()<<", "<<p.gety()<<") to ("<<q.getx()<<", "<<q.gety()<<")"<<endl;
}
Line(Point a,Point b):p(a),q(b)
{}
};//来自Fivelish
#include <iostream>
using namespace std;
class Point
{
private :
double a,b;
public:
Point(double c=0,double d=0):a(c),b(d){}
double geta()
{
return a;
}
double getb()
{
return b;
}
void show()
{
cout<<"Point : ("<<a<<", "<<b<<")"<<endl;
}
};
class Line
{
private :
Point aa,bb;
public:
Line(double xx,double yy,double xxx,double yyy):aa(xx,yy),bb(xxx,yyy)
{
cout<<"Line : ("<<aa.geta()<<", "<<aa.getb()<<") to ("<<bb.geta()<<", "<<bb.getb()<<") is created."<<endl;
}
Line(Point &a,Point &b):aa(a),bb(b)
{
cout<<"Line : ("<<aa.geta()<<", "<<aa.getb()<<") to ("<<bb.geta()<<", "<<bb.getb()<<") is created."<<endl;
}
void show()
{
cout<<"Line : ("<<aa.geta()<<", "<<aa.getb()<<") to ("<<bb.geta()<<", "<<bb.getb()<<") "<<endl;
}
~Line()
{
cout<<"Line : ("<<aa.geta()<<", "<<aa.getb()<<") to ("<<bb.geta()<<", "<<bb.getb()<<") is erased."<<endl;
}
};
int main()
{
char c;
int num, i;
double x1, x2, y1, y2;
Point p(1, -2), q(2, -1), t;
t.show();
std::cin>>num;
for(i = 1; i <= num; i++)
{
std::cin>>x1>>c>>y1>>x2>>c>>y2;
Line line(x1, y1, x2, y2);
line.show();
}
Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
l1.show();
l2.show();
l3.show();
l4.show();
}//来自Fivelish
太麻烦了,如果不用friend的话就要写应该getx应该方便点。
#include<iostream>
using namespace std;
class Point
{
private:
double x,y;
public:
Point(double a=0,double b=0):x(a),y(b)
{
cout<<"Point : ("<<x<<", "<<y<<") is created."<<endl;
}
~Point()
{
cout<<"Point : ("<<x<<", "<<y<<") is erased."<<endl;
}
Point(const Point &q)
{
x=q.x;
y=q.y;
cout<<"Point : ("<<x<<", "<<y<<") is copied."<<endl;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
void show()
{
cout<<"Point : ("<<x<<", "<<y<<")"<<endl;
}
};
class Line
{
private:
Point xx,yy;
public:
Line(double a,double b,double c,double d):xx(a,b),yy(c,d)
{
cout<<"Line : ("<<xx.getX()<<", "<<xx.getY()<<") to ("<<yy.getX()<<", "<<yy.getY()<<") is created."<<endl;
}
Line(Point &a,Point &b):xx(a),yy(b)
{
cout<<"Line : ("<<xx.getX()<<", "<<xx.getY()<<") to ("<<yy.getX()<<", "<<yy.getY()<<") is created."<<endl;
}
void show()
{
cout<<"Line : ("<<xx.getX()<<", "<<xx.getY()<<") to ("<<yy.getX()<<", "<<yy.getY()<<")"<<endl;
}
~Line()
{
cout<<"Line : ("<<xx.getX()<<", "<<xx.getY()<<") to ("<<yy.getX()<<", "<<yy.getY()<<") is erased."<<endl;
}
};//来自Fivelish
#include <iostream>
using namespace std;
class Point
{
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y)
{
cout << "Point : ("<<x<<", "<<y<<") is created." << endl;
}
Point(const Point &another)
{
x = another.x, y = another.y;
cout << "Point : ("<<x<<", "<<y<<") is copied." << endl;
}
~Point()
{
cout << "Point : ("<<x<<", "<<y<<") is erased." << endl;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
void show()
{
cout << "Point : ("<<x<<", "<<y<<")" << endl;
}
void setX(double t)
{
x = t;
}
void setY(double t)
{
y = t;
}
};
class Line
{
private:
Point a, b;
public:
Line() : a(0, 0), b(0, 0)
{
cout << "Line : (0, 0) to (0, 0) is created." << endl;
}
Line(double x1, double y1, double x2, double y2) : a(x1, y1), b(x2, y2)
{
cout << "Line : ("<<x1<<", "<<y1<<") to ("<<x2<<", "<<y2<<") is created." << endl;
}
Line(Point &a, Point &b) : a(a), b(b)
{
cout << "Line : ("<<a.getX()<<", "<<a.getY()<<") to ("<<b.getX()<<", "<<b.getY()<<") is created." << endl;
}
~Line()
{
cout << "Line : ("<<a.getX()<<", "<<a.getY()<<") to ("<<b.getX()<<", "<<b.getY()<<") is erased." << endl;
}
void show()
{
cout << "Line : ("<<a.getX()<<", "<<a.getY()<<") to ("<<b.getX()<<", "<<b.getY()<<")" << endl;
}
void SetLine(double x1, double y1, double x2, double y2)
{
a.setX(x1), a.setY(y1);
b.setX(x2), b.setY(y2);
}
};
int main()
{
char c;
int num, i;
double x1, x2, y1, y2;
Point p(1, -2), q(2, -1), t;
t.show();
std::cin>>num;
Line line[num];
for(i = 0; i < num; i++)
{
std::cout<<"=========================\n";
std::cin>>x1>>c>>y1>>x2>>c>>y2;
line[i].SetLine(x1, y1, x2, y2);
line[i].show();
}
std::cout<<"=========================\n";
Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
l1.show();
l2.show();
l3.show();
l4.show();
}//来自Fivelish
其实这里用的思路之一getX的思想前面已经用到过了,在这里我没最重要的是去写拷贝构造函数。写出来拷贝构造函数显得尤为重要。
这里科普一下拷贝构造函数在哪几种情况下使用。
(上课没仔细听的我,下课百度问同学,所以上课要认真听讲。)
(1)用类的一个对象去初始化另一个对象时
(2)当函数的形参是类的对象时(也就是值传递时),如果是引用传递则不会调用,(注意传递形参和传递地址的区别)
(3)当函数的返回值是类的对象或引用时
其实拷贝构造函数和析构函数以及构造函数是一样的,如果未定义都是有默认函数的,定义了的话就会按照定义地走。
终于到五了,耶耶耶。
还有几个就写完了,真心快乐。
#include <iostream>
using namespace std;
class Point
{
private:
double x,y;
public:
Point()
{
x = 0;
y = 0;
cout << "Point : (" << x << ", " << y <<") is created." << endl;
}
Point(double xx,double yy )
{
x = xx;
y = yy;
cout << "Point : (" << x << ", " << y <<") is created." << endl;
}
Point (const Point &p)
{
x = p.x;
y = p.y;
cout << "Point : (" << x << ", " << y <<") is copied." << endl;
}
double getX() const
{
return x;
}
double getY() const
{
return y;
}
void setXY(double x1,double y2)
{
x = x1,y = y2;
}
void show()
{
cout << "Point : (" << x << ", "<< y << ")" <<endl;
}
~Point ()
{
cout << "Point : (" << x << ", " << y <<") is erased." << endl;
}
};
class Line
{
private :
Point st,ed;
public:
Line():st(0,0),ed(0,0)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is created." << endl;
}
Line( Point &sst, Point &eed):st(sst),ed(eed)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is created." << endl;
}
Line(Line & line):st(line.st),ed(line.ed)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is copied." << endl;
}
Line & setLine(const Line &pt)
{
st = pt.st;
ed = pt.ed;
return *this;
}
Line & setLine( Point &stt, Point &edd)
{
st = stt;
ed = edd;
return *this;//返回指针,这里即新知识点
}
void readLine()
{
double x1,y1,x2,y2;
char ch ;
cin >> x1 >>ch >>y1>> x2 >> ch >>y2;
st.setXY(x1,y1);
ed.setXY(x2,y2);
}
void show() const
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" <<endl;
}
~Line()
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is erased." << 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();
}
这个地方主要是this函数的使用它的使用是一个新的知识点。在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象。友元函数没有 this 指针,因为友元不是类的成员。只有成员函数才有 this 指针。
#include <iostream>
using namespace std;
class Point
{
private:
double x_,y_;
public:
Point()
{
x_ = 0;
y_ = 0;
cout << "Point : (" << x_ << ", " << y_ <<") is created." << endl;
}
Point(double xx,double yy)
{
x_ = xx;
y_ = yy;
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 getX() const
{
return x_;
}
double getY() const
{
return y_;
}
void show() const
{
cout << "Point : (" << x_ << ", "<< y_ << ")" <<endl;
}
double x()const
{
return x_;
}
double y()const
{
return y_;
}
void setXY(double xx,double yy)
{
x_= xx;
y_ = yy;
}
void showNoEndOfLine() const
{
cout << "Point : (" << x_ << ", "<< y_ << ")" ;
}
};
class Line
{
private:
Point st,ed;
public:
Line():st(0,0),ed(0,0)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is created." << endl;
}
Line(const Point &sst, const Point &eed):st(sst),ed(eed)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is created." << endl;
}
Line(const Line & line):st(line.st),ed(line.ed)
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is copied." << endl;
}
void show() const
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" <<endl;
}
Line &setLine(double x1,double y1,double x2,double y2)
{
st.setXY(x1,y1);
ed.setXY(x2,y2);
return *this;
}
Line & setLine(const Line &pt)
{
st = pt.st;
ed = pt.ed;
return *this;
}
Line & setLine(const Point &sst,const Point &eed)
{
st = sst;
ed = eed;
return *this;
}
void readLine()
{
double x1,y1,x2,y2;
char ch ;
cin >> x1 >>ch >>y1>> x2 >> ch >>y2;
st.setXY(x1,y1);
ed.setXY(x2,y2);
}
const Point &start()const//和下面一样
{
return st;
}
const Point &end()const//const不可去
{
return ed;
}
void setStart(const Point & pt)
{
st = pt;
}
void setEnd(const Point & pt)
{
ed = pt;
}
~Line()
{
cout << "Line : (" << st.getX() << ", " << st.getY()<< ") to (" <<ed.getX() <<", " << ed.getY() << ")" << " is erased." << endl;
}
};
void showLineCoordinate(const Line& line)
{
cout<<"Line : "<<"("<<line.start().x()<<", "<<line.start().y()<<")"<<" to "<<"("<<line.end().x()<<", "<<line.end().y()<<")"<<endl;
}
void showLinePoint(const Line& line)
{
cout<<"Line : ";
line.start().showNoEndOfLine();
cout<<" to ";
line.end().showNoEndOfLine();
cout<<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);
}
终于到最后一个了,快乐吗!太快乐了!!!!
#include <iostream>
#include <iomanip>
using namespace std;
class Point
{
private:
double x_,y_;
friend class Line;
static int sta1;
static int sta2;
public:
Point(double x,double y)
{
x_ = x;
y_ = y;
sta1++;
sta2++;
}
Point(double d)
{
x_ = d;
y_ = d;
sta1++;
sta2++;
}
Point()
{
x_ = 0;
y_ = 0;
sta1++;
sta2++;
}
void setvalue(double xx,double yy)
{
x_ = xx;
y_ = yy;
}
void setx(int xx)
{
x_ = xx;
}
void sety(int yy)
{
y_ = yy;
}
void show()
{
cout<<"Point : ("<<x_<<", "<<y_<<")"<<endl;
}
int x()
{
return x_;
}
int y()
{
return y_;
}
Point(const Point &p)
{
sta1++;
sta2++;
x_ = p.x_;
y_ = p.y_;
}
~Point()
{
sta1 = sta1 - 1;
}
static void showCounter()
{
cout<<"Current : "<<sta1<<" points."<<endl;
}
static void showSum()
{
cout<<"In total : "<<sta2<<" points."<<endl;
}
};
int Point::sta1(0);
int Point::sta2(0);
class Line
{
friend class Point;
private:
Point x1_,y1_;
double x1,y1,x2,y2;
public:
static int sta3;
static int sta4;
Line(double xx1,double yy1,double xx2,double yy2):x1_(xx1,yy1),y1_(xx2,yy2)
{
sta3++;
sta4++;
}
Line(Point &q1,Point &q2):x1_(q1),y1_(q2)
{
sta3++;
sta4++;
}
Line():x1_(),y1_()
{
sta3++;
sta4++;
}
Line setLine(double xx3,double yy3,double xx4,double yy4)
{
sta3++;
sta4++;
x1_.x_ = xx3;
x1_.y_ = yy3;
y1_.x_ = xx4;
y1_.y_ = yy4;
return *this;
}
void show()
{
cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<")"<<endl;
}
~Line()
{
sta3 = sta3 - 1;
}
Line &setLine(const Point &p1,const Point &p2)
{
x1_ = p1;
y1_ = p2;
return *this;
}
Line &setLine(const Line& q)
{
x1_ = q.x1_;
y1_ = q.y1_;
return *this;
}
void readLine()
{
double x1,y1,x2,y2;
char c;
cin>>x1>>c>>y1>>x2>>c>>y2;
x1_.x_ = x1;
x1_.y_ = y1;
y1_.x_ = x2;
y1_.y_ = y2;
}
Line(const Line &b):x1_(b.x1_),y1_(b.y1_)
{
sta3++;
sta4++;
}
static void showCounter()
{
cout<<"Current : "<<sta3<<" lines."<<endl;
}
static void showSum()
{
cout<<"In total : "<<sta4<<" lines."<<endl;
}
};
int Line::sta3 = 0;
int Line::sta4(0);
int main()
{
int num, i;
Point p(1, -2), q(2, -1), t;
t.showCounter();
t.showSum();
std::cin>>num;
Line line[num + 1];
for(i = 1; i <= num; i++)
{
Line *l1, l2;
l1->showCounter();
l1->showSum();
l1 = new Line(p, q);
line[i].readLine();
p.showCounter();
p.showSum();
delete l1;
l2.showCounter();
l2.showSum();
q.showCounter();
q.showSum();
}
Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
Line::showCounter();
Line::showSum();
Point::showCounter();
Point::showSum();
Line *l = new Line[num];
l4.showCounter();
l4.showSum();
delete[] l;
t.showCounter();
t.showSum();
}
这个题最主要的是static的用法,即静态成员的使用方法。static即快乐,即正义。
完结