Question : class

why is the line   derv.func(); fails with compiler error but it likes the line
  derv.func_1(); I thought method overriding should work

class A
{
  public:
        void func() { printf ("\n func base class \n" );}
         void func_1() { printf ("\n func_1 \n" );}
};

class B : public A
{
public:
      void func(int a) {  }
 
};

int main (int argc, char *argv[])
{
  B derv;
  derv.func_1();
  derv.func(); // compiler error says requires 1 argument.

  return 0;
}

Answer : class

It's not just an override - it's a combination of an override and an overload that you do.

You've overridden the func function, including all of its overloads. You cannot call any of A's func methods from a B instance, since they're all hidden by overriding the func method.

This would work though :
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
class A
{
  public:
        void func(int a) { printf ("\n func base class \n" );}
         void func_1() { printf ("\n func_1 \n" );}
};

class B : public A
{
public:
      void func(int a) {  }
 
};



// or this :

class A
{
  public:
        void func() { printf ("\n func base class \n" );}
         void func_1() { printf ("\n func_1 \n" );}
};

class B : public A
{
public:
      void func() { A::func(); }
      void func(int a) {  }
 
};
Random Solutions  
 
programming4us programming4us