Let's say I've a class fruit and several class like apple,mango etc,Now i want to create a single function that would accept fruit and it's all derived classes object's as argument,How can i do so?
I have not tried anything yet!
CodePudding user response:
You can make the function parameter to be a reference or a pointer to the base class as shown below.
void eat(const Fruit* f)
{
//code here
}
Or
void eat(const Fruit& f)
{
//code here
}
You can remove the low-level const if you want to be able to make changes through f
.