First, we design the representation of Vector and Point, and then perfect the constructors in the two classes in turn, copy the constructors and so on.
The vector is represented by two points. When the operation is performed, the coordinate of the starting point is (0, 0).
Line 14: since Vector needs to use x,y in Point, it is more convenient to design it as friend.
Line 13: because the ostream and istream classes cannot be modified, overload "<" > "as a global function or declare it as a friend; return os can realize the continuous use of" < ">";
Line 49: design the second constructor to implement the return value when the operator in Vector is overloaded;
Line 59: prevent self replication;
1 #include<iostream> 2 using namespace std; 3 4 class Point // Point class 5 { 6 private : 7 double x,y; 8 public : 9 Point(double x1=0,double y1=0):x(x1),y(y1) {} // Point Constructor 10 Point(const Point &rhs); // Point copy constructor 11 Point operator + (const Point & a); // Point + 12 Point operator - (const Point & a); // Point - 13 friend ostream & operator << (ostream & os , const Point & a); // Point Output overload 14 friend class Vector; // Friends 15 }; 16 17 Point::Point(const Point & rhs) // Point copy constructor 18 { 19 if(this != &rhs) 20 { 21 x = rhs.x; 22 y = rhs.y; 23 } 24 } 25 26 Point Point::operator + (const Point & a) // Point + 27 { 28 return Point(x + a.x , y + a.y); 29 } 30 31 Point Point::operator - (const Point & a) // Point - 32 { 33 return Point(x - a.x , y - a.y); 34 } 35 36 ostream & operator << (ostream & os , const Point & a) // Point << 37 { 38 os <<a.x<<","<<a.y; 39 return os; 40 } 41 42 class Vector // Vector class 43 { 44 private : 45 Point beg,end,_00; 46 public : 47 Vector(double x1=0,double y1=0,double x2=0,double y2=0) // Constructor 1 48 :beg(x1,y1),end(x2,y2) {} 49 Vector(Point beg1,Point end1):beg(beg1),end(end1) {} // Constructor 2 (overload) 50 Vector(const Vector & b); // copy constructor 51 Vector operator + (const Vector & b); // Vector + 52 Vector operator - (const Vector & b); // Vector - 53 friend ostream & operator << (ostream & os , const Vector & b); // Vector Output overload << 54 // void print(); // Output function 55 }; 56 57 Vector::Vector(const Vector & rhs1) // copy constructor 58 { 59 if(this != &rhs1) 60 { 61 beg = rhs1.beg; 62 end = rhs1.end; 63 } 64 } 65 66 Vector Vector::operator + (const Vector & b) // Vector + 67 { 68 return Vector(_00 , end -beg + b.end - b.beg); // Construct with constructor overload 69 } 70 71 Vector Vector::operator - (const Vector & b) // Vector - 72 { 73 return Vector(_00 , end - beg - b.end + b.beg); 74 } 75 /* 76 void Vector::print() // Output function 77 { 78 cout <<"("<<beg.x<<","<<beg.y<<")"<<"\t"; 79 cout <<"("<<end.x<<","<<end.y<<")"<<endl<<endl; 80 } 81 */ 82 83 ostream & operator << (ostream & os , const Vector & b) 84 { 85 os <<"("<<b.beg<<")"<<"\t"; 86 os <<"("<<b.end<<")"<<endl<<endl; 87 return os; 88 } 89 90 int main() 91 { 92 Vector ww(0,0,10,10),mm(1,1,5,5); 93 // ww.print(); 94 // mm.print(); 95 // (ww+mm).print(); 96 // (ww-mm).print(); 97 cout<<ww<<mm<<ww-mm<<mm+ww; 98 return 0; 99 }
2019-10-17