+ 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; }

7th Dec 2016, 10:53 AM
Kunal Sharma
Kunal Sharma - avatar
1 Answer
+ 1
In class healt: virtual void newhealth(int) = 0; You need to tell compiler that function newhealth has no body ( = 0 )
7th Dec 2016, 2:05 PM
Maciej Falbogowski
Maciej Falbogowski - avatar