// 四邊形抽象類別 #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: "; 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 ); } /***** 抽象類別：Quadrilateral 四邊形 *****/ /* class Quadrilateral { friend ostream &operator<<(ostream&, Quadrilateral&); public: Quadrilateral(double = 0, double = 0, double = 0, double = 0, double = 0, double = 0, double = 0, double = 0); protected: Point p1; Point p2; Point p3; Point p4; void printPoints(void) const; // 列印四邊形的頂點 // ***** 寫出純虛擬成員函式 print() 的定義，列印四邊形的邊和面積 }; Quadrilateral::Quadrilateral(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) { } // 將平行四邊形、長方形、正方形的 operator<< 移除，將使用這個父類別函式 ostream &operator<<(ostream& out, Quadrilateral& q) { q.print(); return out; } void Quadrilateral::printPoints(void) const { cout << '(' << p1.getX() << ", " << p1.getY() << ") , (" << p2.getX() << ", " << p2.getY() << ") , (" << p3.getX() << ", " << p3.getY() << ") , (" << p4.getX() << ", " << p4.getY() << ")\n"; } */ /***** 實際類別：Parallelogram 平行四邊形 *****/ // 改成 Quadrilateral 的子類別 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); // ***** 移除子類別的共同資料成員 private: Point p1; Point p2; Point p3; Point p4; double bottom; double height; void printPoints(void) const; // 列印平行四邊形的頂點 void print(void); // 實際函式，列印平行四邊形的邊 double area(void) {return bottom * 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& out, Parallelogram& p) { p.print(); return out; } // ***** 改寫此函式，使用父類別函式 void Parallelogram::printPoints(void) const { cout << '(' << p1.getX() << ", " << p1.getY() << ") , (" << p2.getX() << ", " << p2.getY() << ") , (" << p3.getX() << ", " << p3.getY() << ") , (" << p4.getX() << ", " << p4.getY() << ")\n"; } // 實際函式，列印平行四邊形的邊和面積 void Parallelogram::print(void) { cout << "The coordinates of the Parallelogram are: "; printPoints(); if (verify()) cout << "Bottom: " << bottom << ", Height: " << height << ", Area: " << area() << endl << endl; else cout << "*** Not a parallelogram." << endl << endl; } bool Parallelogram::verify(void) { if (p1.getY()==p2.getY() && p3.getY()==p4.getY() && p1.getY()