#include<stdafx.h> #include<iostream> #include<stdio.h> #include<math.h> using namespace std; class CShape{ public: virtual double Area() = 0;//pure virtual fun virtual void PrintInfo()= 0; }; class CRectangle :public CShape//public mean?? { public: int w, h; virtual double Area();//virtual mean?? virtual void PrintInfo(); double CRectangle::Area() { return w*h; } double CRectangle::PrintInfo() { cout << "CRectangle:" << Area() << endl; } }; class CCircle :public CShape{ public: int r; virtual double Area();//virtual mean?? virtual void PrintInfo(); double CCircle::Area() { return 3.14*r*r; } double CCircle::PrintInfo() { cout << "CCircle:" << Area() << endl; } }; class CTriangle :public CShape{ public: int a, b, c; virtual double Area();//virtual mean?? virtual void PrintInfo(); double CTriangle::Area() { double p = (a + b + c) / 2.0; return sqrt(p*(p - a)*(p - b)*(p - c)); } double CTriangle::PrintInfo() { cout << "CTriangle:" << Area() << endl; } }; CShape *pShapes[100]; //基类指针,使用多态 int MyCompare(const void *s1, const void *s2); int main() { int n; CRectangle *pr; CCircle *pc; CTriangle *pt; cin >> n; for (int i; i<n; i++) { char c; cin >> c; switch (c) { case'R': pr = new CRectangle(); cin >> pr->>w >> pr->h; pShapes[i] = pr; break; case'C': pr = new CCircle(); cin >> pr->>r; pShapes[i] = pc; break; case'T': pr = new CTriangle(); cin >> pr->>a >> pr->b >> pr->c; pShapes[i] = pt; break; } } qsort(pShapes, n, sizeof(CShape*), Mycompare); for (i = 0; i<n; i++) { pShapes[i]->PrintInfo(); } return 0; }