redefine_inherited_default_parameter

C++

shaozkshaozk cpp/redefine_inherited_default_parameter.cpp · 22 行 · 更新于 2026-10-11

#include <iostream>

class Shape {
public:
  enum Color { Red, Green, Blue };
  virtual void draw(Color color = Red) const {
    std::cout << "shape color: " << color << std::endl;
  }
};

class Rectangle : public Shape {
public:
  virtual void draw(Color color = Green) const {
    std::cout << "rectangle color: " << color << std::endl;
  }
};

int main() {
  Shape *shape = new Rectangle();
  shape->draw();
  return 0;
}

评论