#include <iostream>
using namespace std;
// 如果类中至少有一个纯虚函数,则这个类就是抽象类
// 基类
class Shape
{
public:
// 纯虚函数
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// 派生类
class Rectangle :public Shape
{
public:
int getArea()
{
return (width*height);
}
};
class Triangle :public Shape
{
public:
int getArea()
{
return (width*height) / 2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(6);
Rect.setHeight(7);
cout << Rect.getArea() << endl;
Tri.setWidth(6);
Tri.setHeight(7);
cout << Tri.getArea() << endl;
system("pause");
return 0;
}
C++接口
猜你喜欢
转载自blog.csdn.net/qq2942713658/article/details/81784289
今日推荐
周排行