0
Virtual function in c++ find the error and also correct the error
#include<iostream> using namespace std; class base { public: virtual void vfunc() { cout<<"\n This is base's vfunc() \n"; } }; class Derived1: public base { public: void vfunc() { cout<<" \n This is derived 1's v func() \n"; } }; class Derived2: public base { public: void vfunc() { cout<<"\n This is derived2's vfunc() \n "; } }; int main() { base *p,b; Derived1 d1; Derived2 d2; p=&b; p-vfunc (); p=&d1; p-vfunc(); p=&d2; p-vfunc(); return 0; }
1 Antwort
+ 4
Please add the language (C++) in post tags for searchability
Function call through pointer needs to use member-access operator `->`
You did
p - vfunc();
Where you should do
p -> vfunc();
https://code.sololearn.com/W3uiji9X28C1/?ref=app