c++虚拟成员函数

来源:互联网 发布:线程优化 编辑:程序博客网 时间:2024/05/20 06:06
// virtual members#include <iostream>using namespace std;class Polygon {  protected:    int width, height;  public:    void set_values (int a, int b)      { width=a; height=b; }    virtual int area ()      { return 0; }};class Rectangle: public Polygon {  public:    int area ()      { return width * height; }};class Triangle: public Polygon {  public:    int area ()      { return (width * height / 2); }};int main () {  Rectangle rect;  Triangle trgl;  Polygon poly;  Polygon * ppoly1 = &rect;  Polygon * ppoly2 = &trgl;  Polygon * ppoly3 = &poly;  ppoly1->set_values (4,5);  ppoly2->set_values (4,5);  ppoly3->set_values (4,5);  cout << ppoly1->area() << '\n';  cout << ppoly2->area() << '\n';  cout << ppoly3->area() << '\n';  return 0;}
原创粉丝点击