+ 1
Need help in Virtual functions ; Can't figure out the error
#include <iostream> using namespace std; class health{ protected: int power; public: virtual void newhealth(int); }; class apple:public health{ public: void newhealth(int a){ power =a; cout<<"Eating an apple will increase your health by "<<power<<endl; } }; class banana:public health{ public: void newhealth(int a){ power =a; cout<<"Eating an banana will increase your health by "<<power<<endl; } }; int main() { apple a; banana b; health* fruit1=&a; health* fruit2=&b; fruit1->newhealth(25); fruit2->newhealth(10); return 0; }
1 Answer
+ 1
In class healt:
virtual void newhealth(int) = 0;
You need to tell compiler that function newhealth has no body ( = 0 )