#include "iostream"
using namespace std;
class CPolygon{
protected:
double height, width;
public:
void setValue(double h, double w){
height = h;
width = w;
}
};
class CRectangle : public CPolygon{
public:
double getArea(){
return height * width;
}
};
class CTriangle : public CPolygon{
public:
double getArea(){
return height * width / 2;
}
};
int main(){
CRectangle rec;
CTriangle tri;
rec.setValue(5,10);
tri.setValue(5,10);
cout << rec.getArea() << endl;
cout << tri.getArea() << endl;
return 0;
}output:50
25
本文详细介绍了C++中类的继承与多态概念,通过实现矩形和三角形类来计算其面积,展示了如何使用基类指针和虚函数来处理不同形状的计算问题。

被折叠的 条评论
为什么被折叠?



