5 Replies - 572 Views - Last Post: 15 March 2012 - 04:13 PM Rate Topic: -----

#1 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Overloading Virtual function

Posted 14 March 2012 - 11:24 PM

Hi,

#include<iostream>
using namespace std;
class A
{
  public:
   A() {
   }  
virtual void m()
{
  cout << "BASE Default" << endl;
}
virtual void m(int i)
{
  cout << "BASE 1 Arg" << endl;
}
virtual void m(int i,char c)
{
  cout << "BASE 2 Arg" << endl;
}
};
class B : public A
{
public:
B(){}
void m()
{
  cout << "Der Default" << endl;
}
void m(int i,char c)
{
  cout << "BASE 2 Arg" << endl;
}
};
int main()
{
  B b;
  b.m(25);
  return 0;
} 


The output of above code is:
sample.cpp: In function ‘int main()’:
sample.cpp:37: error: no matching function for call to ‘B::m(int)’
sample.cpp:25: note: candidates are: virtual void B::m()
sample.cpp:29: note: virtual void B::m(int, char)

I was expecting output as "BASE 1 Arg" since if we don't override any base virtual function then derived class will have the definition defined in base class for that function.so can anybody explain the reason for this behavior.

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading Virtual function

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Overloading Virtual function

Posted 15 March 2012 - 01:06 AM

Quote

Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class, as in the above example.
~ cplusplus.com (polymorphism tutorial)

int main()
{
        B b;
        A* a = &b;
        a->m(10);
        return 0;
} 


Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,232
  • Joined: 31-December 10

Re: Overloading Virtual function

Posted 15 March 2012 - 07:10 AM

You only overrode one of the virtual functions from the base class: virtual void m(int i,char c). You then defined a function in the derived class called "m" that doesn't take any arguments. When overriding virtual functions, the arguments need to match exactly. So you want the "m" function with no arguments to look like virtual void m(int i). If you make those changes, your program SHOULD work.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Overloading Virtual function

Posted 15 March 2012 - 02:32 PM

Quote

I was expecting output as "BASE 1 Arg" since if we don't override any base virtual function then derived class will have the definition defined in base class for that function.so can anybody explain the reason for this behavior.


And this is exactly what my solution does. Prints "BASE 1 Arg" to the console. vividexstance: I don't why you down-voted my post. It's correct.

Quote

So you want the "m" function with no arguments to look like virtual void m(int i). If you make those changes, your program SHOULD work.


But that wasn't the OP intentions. He wanted to call the base case class function, not override it.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Overloading Virtual function

Posted 15 March 2012 - 03:56 PM

Your code was correct but I think you misunderstood the thrust of the OP's question. But so did your "down-voter", and you certainly didn't deserve to be down-voted for misunderstanding the question.

The OP was asking, since they didn't override the base class m(int) method, why wasn't that base class method visible in the derived class.

The reason is that name hiding works differently than inheritance. If a derived class defines any function with the same name as a base class function (even if it has a different argument list), the base class function is hidden -- not visible in the derived class. Therefore, defining either m() or m(int,int) in the derived class will prevent the base class m(int) function from being visible in the derived class.

If that function had a different name than anything defined in the derived class, it would be visible in the derived class whether or not it was virtual in the base class. For example:
#include<iostream>
using namespace std;
class A
{
public:
    A() {
    }
    virtual void m()
    {
        cout << "BASE Default" << endl;
    }
    virtual void mm(int i)
    {
        cout << "BASE 1 Arg" << endl;
    }
    virtual void m(int i,char c)
    {
        cout << "BASE 2 Arg" << endl;
    }
};
class B : public A
{
public:
    B() {}
    virtual void m()
    {
        cout << "Der Default" << endl;
    }
    virtual void m(int i,char c)
    {
        cout << "BASE 2 Arg" << endl;
    }
};
int main()
{
    B b;
    b.mm(25);
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Overloading Virtual function

Posted 15 March 2012 - 04:13 PM

I see stiltskin. I was going to try and declare m(int) to be virtual, thinking that would work, but then I saw the comment about using a dynamic type pointer, so I just went with that. I figured that was the only way to access the base class function, but I see now that it's not.

This post has been edited by blackcompe: 15 March 2012 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1