Hello, welcome to the 63rd tutorial in the c++ programming series. And in this problem, we're looking at virtual methods. Virtual methods are functions that can be redefined in a derived class, the derived method needs to have the same return type as the base method. They have it really that's all there is to the drum Fs LS and the tutorial right here as jokin necessarily show you how to implement a virtual method. So first of all, we're going to create a class called vehicle. And then we'll do public comment to create a virtual method use a keyword virtual.
Then literally afterwards, you just put on your method of void print function. Void vehicle, print function. As you can see, you don't actually put the virtual Key word when you're actually implemented. If you were to implement it in line like this, then you would still leave the word key word there. But when implementing the outside of class, you don't put semicolon there. So in here, we're going to do STD c out.
I'm going to print base, STD and the line. Another nice thing for us to do is create another class called BMW make this equal to public vehicle so it inherits from the vehicle. And now all we're going to do is public void. print function, analysis, implement this, filling the void, BMW convolution print function and it here Do STD c out and then what we're gonna do for this is print quotation marks, print BMW, STD and line. Now what we're going to do is just simply create two objects are going to do vehicle v one, do v one dot print function do BMW B one B one dot print function on every run it we get print base and print BM that'd be morphing. Yeah, it just looks like a regular functions.
One thing you got to note before actually, is to show you something if you were to move the keyword virtual ironic, you get build succeeded as well, and that works evolve but these are Technically to individual methods, but if we put the keyword virtual back in and we change the return type to an int, for example, and we put the channel one and run it, the build failed saying the return type hasn't been corrected there but let's let's ignore that for a second virtual function print each other different return takedown, the function it overrides. So this basically makes you have the same sort of structure. It just allows you to override the functionality itself. Cuz you do the play of it really for this tutorial. Just make sure the return type is the same. If you have any questions feel free to message us or support us on our system correct a email will be in the description.
You can comment on this video with us directly message me just via YouTube. The recording source source code will also be in the description and in the next part of this series will be Looking at the abstract base class, if you're me, already asked if you have any questions for me and as usual, thanks for watching and I hope you have a nice day.