Virtual function in c++ find the error and also correct the error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

31st Jan 2023, 9:49 AM
Nikita Mondal
Nikita Mondal - avatar
1 Answer
+ 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
31st Jan 2023, 9:59 AM
Ipang