C++ 接口(抽象类)
作者:互联网
C++ 接口(抽象类)
class Shape
{
public:
//纯虚函数
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width,height;
};
//派生类
class Rectangle : public Shape
{
public:
int getArea()
{
return (width * height);
}
};
//派生类
class Triangle : public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main()
{
Rectangle rec;
Triangle tri;
rec.setWidth(3);
rec.setHeight(4);
cout << "tri getAre 面积 =" << rec.getArea() << endl;
tri.setWidth(3);
tri.setHeight(4);
cout << "tri getAre 面积 =" << tri.getArea() << endl;
return 0;
}
标签:int,接口,height,width,C++,rec,抽象类,public,getArea 来源: https://blog.csdn.net/weixin_40828023/article/details/113825708