I have two classes base and derived, base has a void foo(), derived classes doesn't override void foo() instead, I wrote an over loaded function which is void foo(int), If I create an instance of derived class, compiler doesn't recognize void foo(), it only sees void foo(int)
class base {
public:
int a;
base(){
a = 1;
}
virtualvoid foo(){
cout<<"this is base\n";
}
};
class derived : public base {
public:
int b;
derived(){
b = 2;
}
// overload without overriding
void foo(int x ){
cout<<"this is derived\n"<<x;
}
};
derived a;
// this is inside base class but it doesn't recognized by compiler
a.foo();
my Other question is;
in order to override a function, it must have same name return type variable... what if I change return type ? it doesn't count as either override or overload, than what is it ?
class derived : public base {
public:
int b;
using base::foo;
derived(){
b = 2;
}
// overload without overriding
void foo(int x ){
cout<<"this is derived\n"<<x;
}
};
I made these two classes to show the situation, however I would like to hear how I can make a better implementation ? by the way what is wrong with this ?