Reputation:
I am trying to understand if the derived class has overloaded base class func, (base class has int parameter and der class has double parameter) when i created der class instance and call func name with correct parameters, only der func is called?? Can someone help me in this .. Is there a way at line 28 of code (dd.f(d1);) , I can get base class func call.
Here is the code.
using namespace std;
class base {
public :
virtual void f (int i);
};
class der : public base{
public:
void f ( double d);
};
void base::f(int i){
cout << "Base::f \n";
}
void der::f(double d){
cout <<"Der::f \n";
}
int main(){
der dd;
int i =99;
double d1 = 3232.232;
dd.f(i); // why this calls der class func???
dd.f(d1);
base *ptr = new der();
ptr->f(i);
dynamic_cast<der *>(ptr)->f(d1);
return 0;
}
Upvotes: 0
Views: 890
Reputation: 1794
the following is called overriding
class baseClass
{
public:
virtual void DoIt()
{//.........
}
}
class child : public baseClass
{
void DoIt()
{
baseClass::DoIt();
}
}
in microsoft visual C++ you can use _super instead of base class name _super::DoIt()
what you are doing is not overriding it is another method with different signature if there are problems just use in the public body of your class using base::confusingMethod; like the following.
class base
{
public:
base(){}
virtual void Job(int x)
{
cout<<"Base"<<endl;
}
};
class son : public base
{
public:
using base::Job;
son(){}
virtual void Job(float x)
{
cout<<"Son"<<endl;
}
};
Upvotes: 0
Reputation: 3002
why this calls der class func???
Because the compiler chooses functions by signatures only between those which are declared in the same class. For example if you had something like this:
class der : public base{
public:
void f ( double d);
void f (int i ){cout << "f(int)" <<endl;}
};
Calling your dd.f(i);
would bring you to the output f(int)
.
Since your der
class has a function f()
although the calling parameters don't match, the compiler doesn't have a need to go to base
class, it just converts your int
to double
In order to call explicitly f()
from base
class you can do it like this: dd.base::f(i);
Upvotes: 0
Reputation: 47658
When you declare function in derived class with same name as in base class, function in base class is hided.
`use base::f`
#include <iostream>
using namespace std;
class base {
public :
virtual void f (int i);
};
class der : public base{
public:
using base::f;
void f ( double d);
};
void base::f(int i){
cout << "Base::f \n";
}
void der::f(double d){
cout <<"Der::f \n";
}
int main(){
der dd;
int i =99;
dd.f(i); // Base::f
return 0;
}
class Base {
virtual void store(long l){
//both int and long could be processed
}
}
You create Derived class
class Derived {
virtual void store(long l) {
//store another way.
}
}
and call it like
Derived d;
d.store(1);
Then somebody think "I could store ints more quickly!" and create
Base::store(int)
and you fail if there's no hiding like this
Upvotes: 2
Reputation: 181
I'm not a C++ programmer, but I'm fairly certain that the language identifies the function/method being called by the parameters being passed to it, as well as the name. f(int) is different than f(double). Do some searching on polymorphism
Upvotes: 0