0

C++

in this code, I am trying to make override method do ı have to use virtual key ? or is it already override method? #include<iostream> class gun{ public: void fire(){ std::cout<<"pi:ugt"<<std::endl; } }; class ak47 : public gun{ public: void fire(){ std::cout<<"tı:u:gh"<<std::endl; } }; int main(){ ak47 k1; k1.fire(); return 0; }

25th Aug 2022, 8:36 PM
kullanici 8
kullanici 8 - avatar
1 Answer
0
For this specific code, you don't have to do that. But , if you start refering ak47 as gun using gun pointer, then it won't work as expected. Have a look at this : https://code.sololearn.com/cSU57dvK840H/?ref=app To fix that you have to use virtual and override : https://code.sololearn.com/cDouJe5hiTW3/?ref=app It would be beneficial in the scenario like this, where we have multiple guns besides ak47 like this: https://code.sololearn.com/cK4L4ta5P7E6/?ref=app
26th Aug 2022, 4:51 AM
Vinit Sonawane
Vinit Sonawane - avatar