// Class for rational numbers. #include #include #include using namespace std; using std::setprecision; using std::setiosflags; using std::resetiosflags; /***** 類別：Point 點 *****/ class Point { friend ostream &operator<<( ostream&, const Point& ); public: Point( double = 0, double = 0 ); void setPoint( double, double ); void print( void ) const; double getX( void ) const { return x; } double getY( void ) const { return y; } private: double x, y; }; Point::Point( double a, double b ) { setPoint( a, b ); } void Point::setPoint( double a, double b ) { x = a; y = b; } ostream &operator<<( ostream &output, const Point &p ) { output << "The point is: "; p.print(); return output; } void Point::print( void ) const { cout << setiosflags( ios::fixed | ios::showpoint ) << '[' << setprecision( 2 ) << getX() << ", " << setprecision( 2 ) << getY() << "]\n" << resetiosflags( ios::fixed | ios::showpoint ); } /***** 類別：Parallelogram 平行四邊形 *****/ class Parallelogram { friend ostream &operator<<( ostream&, Parallelogram& ); public: Parallelogram( double = 0, double = 0, double = 0, double = 0, double = 0, double = 0, double = 0, double = 0 ); double area(void) {return bottom * height;} // 計算平行四邊形的面積; protected: Point p1; Point p2; Point p3; Point p4; void print( void ) const; private: double bottom; double height; bool verify(void); }; Parallelogram::Parallelogram( double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4 ) : p1( x1, y1 ), p2( x2, y2 ), p3( x3, y3 ), p4( x4, y4 ) { } ostream &operator<<( ostream& output, Parallelogram& q ) { output << "The coordinates of the Parallelogram are: "; q.print(); if (q.verify()) output << "Bottom: " << q.bottom << ", Height: " << q.height << ", Area: " << q.area() << endl << endl; else output << "*** Not a parallelogram." << endl << endl; return output; } bool Parallelogram::verify( void ) { if (p1.getY()==p2.getY() && p3.getY()==p4.getY() && p1.getY()