tags:

views:

93

answers:

3

I have defined a class A and derived a new class B from A . I have overloaded SetData() funtion in class B. When i tried to access SetData funtion of class B using object of B ,compiler doesn't permit it. why is it so ?

class A{

 public :
       void SetData();    
};

class B : public A {

 public:
       void SetData(int);   
};

B b;
b.SetData() ; // error
+1  A: 
b.SetData(2) ; // error

I don't see any problem in calling SetData(2).

Where as if you call b.SetData() ; then there will be an error. When you define a method with same name in in derived class the base class methods with same name will be hidden in derived class.

You can make them visible in derived class by using keyword.

class A{

 public :
     void SetData(){}   
};

class B : public A {

 public:

     using A::SetData;
     void SetData(int){}   
};



int main() 
{ 
B b;
b.SetData() ; // error
}
aJ
+4  A: 

OMG, no error message. -1 for you.

But let us use telepathy and guess your error message. You're getting something like "symbol not found" because you try to call a function B::SetData() which doesn't have a body. And it must have a body even if it does nothing and even if it's declared in parent class! Try adding it into the body of your class

class B : public A {
 public:
       void SetData(int)
       {  /* add body here */ };   
};

or outside of it

class B : public A {

 public:
       void SetData(int);   
};
void B::SetData(int)
{
  //write it here
}
Pavel Shved
+1 for the good analysis.
Johannes Schaub - litb
A: 

What i guess you might be facing

error: unresolved external symbol

write code as suggested by Pavel Shved

sat