fork download
  1. #include <iostream>
  2. #include <cstddef>
  3.  
  4. class Base
  5. {
  6. public:
  7. virtual void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  8. };
  9.  
  10. class Derived1 : public Base
  11. {
  12. private:
  13. virtual void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  14. };
  15.  
  16. class Derived2 : public Derived1
  17. {
  18. public:
  19. virtual void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  20. };
  21.  
  22. int main(int argc, char** argv)
  23. {
  24.  
  25. Base* derived1 = new Derived1();
  26. std::cout<< "Overriden methods from public inheritance cannot be forced to be private" << std::endl;
  27. derived1->foo();
  28.  
  29. Base* derived2 = new Derived2();
  30. std::cout<< "False attempt to make ::foo private in Derived1 doesn't work, Derived2 can still override it." << std::endl;
  31. derived2->foo();
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
Overriden methods from public inheritance cannot be forced to be private
virtual void Derived1::foo()
False attempt to make ::foo private in Derived1 doesn't work, Derived2 can still override it.
virtual void Derived2::foo()