#include <iostream> using namespace std; class Shape { protected: int width; int height; string name; public: // pure virtial function for interface virtual int getArea() = 0; virtual string getName() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } void setName(string name_) { name = name_; } }; class Rectangle : public Shape { public: int getArea() { return width * height; } string getName() { return "retangle"; } }; class Triangle : public Shape { public: int getArea() { return (width * height) / 2; } string getName() { return "triangle"; } }; int main() { Rectangle rectangle; Triangle triangle; rectangle.setWidth(5); rectangle.setHeight(7); triangle.setWidth(5); triangle.setHeight(7); cout << rectangle.getName() << " area is:" << rectangle.getArea() << endl; cout << triangle.getName() << " area is:" << triangle.getArea() << endl; return 0; }标签:triangle,string,int,c++,接口,height,width,纯虚,public From: https://www.cnblogs.com/johnnyzhao/p/17034809.html