// 类模版派生普通类 #include #include #include using namespace std; template class Shape { protected: T iPerimeter; public: // virtual double area() = 0; virtual T perimeter() { return iPerimeter; } // 周长 }; class Rectangle : public Shape { private: int width, height; public: Rectangle(int w, int h) : width(w), height(h) { iPerimeter = 2 * (w + h); } }; class Triangle : public Shape { private: float a, b, c; public: Triangle(float a, float b, float c) : a(a), b(b), c(c) { iPerimeter = a + b + c; } }; int main() { Rectangle r1(10, 20); cout << "r1周长: " << r1.perimeter() << endl; Triangle t1(3.0f, 4.0f, 5.0f); cout << "t1周长: " << t1.perimeter() << endl; return 0; }