Dynamic Binding (Polymorphism) With The Virtual Keyword | C++ Tutorial

  Рет қаралды 8,468

Portfolio Courses

Portfolio Courses

2 жыл бұрын

How to implement dynamic binding (i.e. dynamic polymorphism) with virtual member functions in C++. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 17
@dwivedys
@dwivedys Ай бұрын
This is the best video on static and dynamic polymorphism I have seen among here on YT
@FluxSentor
@FluxSentor Жыл бұрын
This was such a boon to find. I've been lurking for a while since starting school for computer science. You've really been a help, thanks for explaining so many topics!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, I'm really glad to hear the videos have been helpful! :-)
@MohitRaj-1712
@MohitRaj-1712 2 жыл бұрын
Your videos on OOP topics help me a lot.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm very glad to hear that they help you Mohit, thank you for sharing that! :-D
@CrabRango
@CrabRango 3 ай бұрын
Would you only need to apply the virtual keyword to the base class member function? Or are there cases in which you would need to apply it to derived class member functions?
@ValliNayagamChokkalingam
@ValliNayagamChokkalingam Жыл бұрын
Thanks! I thought that polymorphism just meant that the derived class could be used to access member functions and variables of the parent class. So, in our array with student object pointers, when we create space for medical student object instances, is space for all the member variables of medical student allocated or just the name variable? Thanks! Also, if a function returns a reference to something like "&int add()", just returning an int in the actual function's code is enough right? The compiler will convert the returned int to an int reference, right?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Space for all the member variables will be allocated. If you try returning a constant int like '5' from a function "int& add()" then it should produce an error.
@akashislam9127
@akashislam9127 Жыл бұрын
if I have a chain of drive class like a semi-diamond and the very bottom class has the base class 3 times do I have to use virtual in all the drived class?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
My understanding is that in the derived classes, the member functions will be virtual whether we use virtual or not because they are virtual in the base class: stackoverflow.com/questions/58042421/c-multi-level-inheritance-with-virtual-functions.
@akashislam9127
@akashislam9127 Жыл бұрын
@@PortfolioCourses I was not taking about that , I meant ambiguity problem. #include using namespace std; template class sim_calculator { public: T x, y; void get() { cout > x >> y; } void calculate() { cout
@akashislam9127
@akashislam9127 Жыл бұрын
why polymorphism does not work if I make the array on the stack instead of on the heap?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a really good question Akash but the answer may be a bit long for a commnet. Some of these answers here explain why polymorphism works with pointers/references: stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references. :-)
@akashislam9127
@akashislam9127 Жыл бұрын
@@PortfolioCourses I'm still not fully clear, I would be grateful if you made a video to clarify this topic
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@akashislam9127 Maybe one day I can do that! 🙂
@germankoga8640
@germankoga8640 Жыл бұрын
So basically what the virtual keyword does is override the default behavior of a base class function so when polymorphism is in place child clases use their own versions?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes, that's basically the idea. The virtual keyword allows derived classes to override the behaviour of the virtual base class method to achieve dynamic aka "runtime" polymorphism. :-)
Virtual Destructors | C++ Tutorial
10:07
Portfolio Courses
Рет қаралды 2,8 М.
C++ Polymorphism and Virtual Member Functions [6]
12:13
Professor Hank Stalica
Рет қаралды 10 М.
1❤️
00:17
Nonomen ノノメン
Рет қаралды 12 МЛН
C++ Programming Tutorial 99 - Polymorphism
7:21
Caleb Curry
Рет қаралды 31 М.
Class Member Functions (aka Methods) | C++ Tutorial
7:18
Portfolio Courses
Рет қаралды 25 М.
C++ Tutorial 11 : Polymorphism
18:48
Derek Banas
Рет қаралды 75 М.
Object-oriented Programming in 7 minutes | Mosh
7:34
Programming with Mosh
Рет қаралды 3,8 МЛН
Pure Virtual Destructors | C++ Tutorial
18:12
Portfolio Courses
Рет қаралды 3 М.
What is polymorphism in programming
2:27
iAmDev
Рет қаралды 143 М.