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

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -