C++ Class and virtual method -


i having 2 problem virtual methods.

first:

class parent { public:     virtual void show(int x = 5) { cout << "parent " << x << endl; } };  class child : public parent { public:     virtual void show(int y = 10) { cout << "child " << y << endl; } };  int main() {     child y;      parent* p = &y;     p->show();      getch();       return 0;      } 

i think tt should child 10 result child 5

and another:

class parent { public:     virtual void show() { cout << "parent" << endl; } };  class child : public parent { private:     virtual void show() { cout << "child" << endl; } };  int main() {     child y;     parent* p = &y;     p->show();      getch();       return 0;      } 

it'll show child on screen. don't know how private method called outside?

thank you. i'm learning english so.. :)

1) c++ standard says

a virtual function call (10.3) uses default arguments in declaration of virtual function determined static type of pointer or reference denoting object. overriding function in derived class not acquire default arguments function overrides.

§8.3.6/10

2) overriding public virtual functions private functions in c++


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -