Code:
class Base
{
public:
void myFun(){ std::cout << "I m Base" };
};
class Derived : public Base
{
public:
void myFun(){ std::cout << "I m Derived"; };
};
int main()
{
Base b;
Derived d;
b.myFun();
d.myFun();
return 0;
}
The above code will work [I fixed the myFun so that it's actually doing something].
It would be nicer to use a virtual function, because that way you can use a pointer for the base-object, and get access to the version of the object that you actually used for the instance, e.g:
Code:
class Base
{
public:
virtual void myFun(){ std::cout << "I m Base" };
};
class Derived : public Base
{
public:
virtual void myFun(){ std::cout << "I m Derived"; };
};
int main()
{
Base *b1;
Base *b2;
Derived d;
b1 = new Base;
b2 = new Derived;
b1->myFun(); // Base::myFun()
b2->myFun(); // Derived::myFun()
d.myFun(); // Derived::myFun();
return 0;
}
--
Mats