C++ Tutorial: Inheritance, Polymorphism, Virtual Functions, Abstract Base Class (Updated!)

  Рет қаралды 19,017

Professor Hank Stalica

Professor Hank Stalica

Күн бұрын

In this video, I write a short program that utilizes the object-oriented programming principles of polymorphism and inheritance.
Included are examples of inheritance, polymorphism, virtual functions, abstract base classes, and a UML diagram for the example program.
// Learn More //
...by watching my Classes playlist:
• Playlist
...by watching my 80+ videos C++ playlist:
• Playlist
// Consider supporting this channel in multiple ways //
ko-fi.com/professorhank
paypal.me/hankstalica1
/ @professorhankstalica
Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP

Пікірлер: 83
@ladoctoradebug9788
@ladoctoradebug9788 Жыл бұрын
Pretty much everything you need to know about C++ classes. Thanks Prof!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Hi, Linda. Really happy to hear that you enjoyed the video and got some use out of it! Thank you so much for the super thanks and that will go into the upgrades fund for improving the channel and videos for you guys and the students. Thank you very, very much.
@liboyan7010
@liboyan7010 Жыл бұрын
The best OOP application tutorial in C++ I ever watched!!!!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Thank you for the kind words and comment.
@bern1223
@bern1223 3 жыл бұрын
This is how computer science SHOULD be taught! My school just used powerpoint and we never ever coded in class so I would never know how to actually implement these concepts. Thank you for your help!
@BidoofGames
@BidoofGames 3 жыл бұрын
yee
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you found it useful!
@petlover9580
@petlover9580 Жыл бұрын
@@BidoofGames in g ty TG and a TV g ggg gg h gf gf is ggg good ty tvg
@mihirpatel3010
@mihirpatel3010 Жыл бұрын
Thank you sir. You're one of the best when learning C++ 🙏
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Thank you for the kind words and I'm really happy you getting value from the videos.
@jhc641
@jhc641 3 жыл бұрын
This about 30 minutes video was way crystal clear than my 2-hour CS lecture. Thank you for your quality video.
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
You're very welcome!
@johnpaulrosario5538
@johnpaulrosario5538 3 жыл бұрын
The best OOP application tutorial in C++ I ever watched. Simple and easy to understand explanation.
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Wow, thanks!
@alexandruteodor3585
@alexandruteodor3585 Жыл бұрын
Thank you! I'm finally starting to understand what is polymorphism used for.
@nikopapiashvili1627
@nikopapiashvili1627 2 жыл бұрын
this was better than 4 hours of lecture, thak you very much.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you liked it and thanks for the comment!
@nolans7997
@nolans7997 2 жыл бұрын
Amazing video, thanks for all the help. Needs more teachers like this
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you found it helpful and thanks for the comment and support.
@maxwen6447
@maxwen6447 Жыл бұрын
I was struggling to create a vector of derived classes and for some reason it's difficult to find good examples but this explanation was outstanding. I'm going to watch all your C++ videos now because of your excellent teaching style. Thank you for this resource.
@shaheerhasan4568
@shaheerhasan4568 2 жыл бұрын
Thank you professor hank this helped me a lot I struggled all through cs 115 and finally I understand how inheritance and polymorphism works. and this got you a new sub!!!
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad it helped!
@Spectrej10
@Spectrej10 2 жыл бұрын
Thank you so much for making this! I've been feeling some disconnect between the content of my CS class and my understanding and seeing this helped me understand what we've been talking about so much more.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad it was helpful!
@jesusgarciagutierrez7301
@jesusgarciagutierrez7301 2 жыл бұрын
The best video on the internet I have ever seen about inheritance, thank you so much!!!
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Wow, thank you!
@truthspeaks651
@truthspeaks651 3 жыл бұрын
Excellent tutorial. You nailed it. Thank you very much, this was a well designed and well presented video, only someone insane could give it a thumbs down. I can't wait for more tutorials from you. Keep up the good work.
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Glad you liked it!
@gepic8329
@gepic8329 3 жыл бұрын
Very nice and simple way to explain an actually very complicated topic! That was exactly what I needed for my current project! Thank you!
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Glad you found it useful!
@chakt2772
@chakt2772 3 жыл бұрын
This is an awesome video explaining difficult to understand C++ OOP concept in a simple and straightforward way. Very well thought of samples from inheritance to virtual function and to polymorphism. Sincere Thank You!
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Glad it was helpful!
@shankarghimire4492
@shankarghimire4492 2 жыл бұрын
Wow! Very well and clear explanation and demonstration! Great Job Hank! :)
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you found it helpful and thanks for the comment and support.
@zengceng
@zengceng 3 жыл бұрын
was lovely to listen to this while doing my stressful assignment
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
*whispers* glad you like it....
@kidbuu1328
@kidbuu1328 3 жыл бұрын
This is so well paced and explained. Thank you very much for your effort professor!
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Glad it was helpful!
@extensivemoney6797
@extensivemoney6797 3 жыл бұрын
This be so dope Professor Hank!
@omarmohamed3473
@omarmohamed3473 Жыл бұрын
Fantastic lecture!
@donovanhodges5713
@donovanhodges5713 Жыл бұрын
Thank you for the video. I enjoy your explanation and method of explaining concepts.
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Glad it was helpful!
@mahdishirazi5785
@mahdishirazi5785 2 жыл бұрын
thanks very much. it is really worth a thumbs up professor Stallica, very very excellent video with great example to demonstrate the concepts, although it would have been better to inherit two abstract classes from the Shape class, being named TwoDShape and ThreeDShape, then assigning a virtual function getVolume to the ThreeDShape class and then inheriting the Circle and Rectangle classes from TwoDShape and Solid class from ThreeDShape; so that the getVolume method will do whatever the getArea method did for Solid in your code (returning length times width times depth). the getArea method you defined, is later used in Solid class but it doesn't return the area, it returns the volume, and that is not wrong in a way to generate errors, but is kind of weird for a getArea method to return the volume and not the area itself.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks for pointing that out! Will have to adjust that in a future remake of this video!
@robcambr
@robcambr 9 ай бұрын
thanks for the lesson professor!
@ProfessorHankStalica
@ProfessorHankStalica 9 ай бұрын
Glad you liked it. I do have an updated version of this video if you'd like to see that as well. Good luck with your studies!
@Diamond_Hanz
@Diamond_Hanz 3 жыл бұрын
Thanks prof hanky!
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
You bet!
@saif5151
@saif5151 2 жыл бұрын
thank you
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
You're welcome!
@cerealnomilk9657
@cerealnomilk9657 3 жыл бұрын
thank you !
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
You're welcome!
@rfdave3980
@rfdave3980 4 жыл бұрын
Thank Prof Hank. Great explanation.
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Glad it was helpful!
@garyvillar8992
@garyvillar8992 Жыл бұрын
Great video! Quick question do the objects need to be dynamically allocated in order for the dynamic binding to work?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
It does require pointers.
@animalzblc8960
@animalzblc8960 2 жыл бұрын
18:00
@maxim25o2
@maxim25o2 Жыл бұрын
You forget to mention that when You are deleting objects in vector, or another storage, vector also must be emptied. Like " shapes.clear() ", otherwise in side of code, when vector is working in loop, and it will be overridden, then program will crash, because, the old appended objects as pointers are not existing anymore, and it will point to garbage. After deleting objects, always remember to clean vector. I have that situation, when vetor was holding pointers to non existing objects.
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
That is absolutely correct, however, since the program was terminating immediately after deleting the objects, it doesn't really matter. As a matter of best practice though, if I had more code following the for loop deleting objects, you are 100% right I should have cleared the vector.
@mandeepkhadka388
@mandeepkhadka388 2 жыл бұрын
Thank you, this was both very clear and concise, I just have one question though, what's the exact purpose of using the 'const' keyword after almost every method name? ( e.g. double getRadius() const { return radius; } ) I have not used 'const' this way with function names and everything seems to work fine without using it as well. Thank you in advance.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
It is a way to explicitly mark the method as an accessor and prevents any statements within the method from changing the contents of any of the class attributes. For example, class Foo { int x; public: void bar() const { x = 5; } }; Will not compile.
@mandeepkhadka388
@mandeepkhadka388 Жыл бұрын
@@ProfessorHankStalica thank you once again :) for the comprehensive reply.
@imrank340
@imrank340 10 ай бұрын
Please increase MS code dev studio Font size much appreciated. Thanks for explaining "dynamic Bindings".
@ProfessorHankStalica
@ProfessorHankStalica 10 ай бұрын
I hear you and thanks for the comment. This is a really old video and in recent releases I did make that adjustment. Here is updated versions of this content: kzbin.info/aero/PLaatXkJEXKyKlx6H44MbV2d63AmudD5sd
@marcinlesniewski4571
@marcinlesniewski4571 3 жыл бұрын
27:18 Line 55 Can we use Ractangle::getArea() instead of getLength() * getWidth() ? , except that this is the formula for Volume, not Area;) Should be 2 * Ractangle::getArea()+2*getLength()*depth+2*getWidth*depth
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks!
@thanasiskotsompolis7821
@thanasiskotsompolis7821 3 жыл бұрын
You create a vector of Shape pointers, but each pointer points to something with more memmory than it should have(Circle,Rectangle,Solid) . How is that allowed??
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Because pointers aren't objects.
@PickleADC
@PickleADC 3 жыл бұрын
pointer is just a variable that contain address of another variable/object so yeah it's not related to the object itself
@TpojkaVEVO
@TpojkaVEVO 3 жыл бұрын
solid.getArea() { return 2 * ((length * widht) + (length * depth) + (widht * depth)); };
@maxim25o2
@maxim25o2 3 жыл бұрын
You don't need use constructor in Circle, I was playing with similar code and I found out that calling to fincion in Shape I was able also to achive this same. using namespace std; class SHAPE{ protected: double height; double width; public: void s(double length){ height = length; width = length; } void s(double h, double w){ height = h; width = w; } virtual double area(){ return height * width; } }; class CIRCLE : public SHAPE{ public: void c(double w) { s(w); //************ this is call to function I didn't use constructor at all. } double area(){ return 3.14159 * pow((width / 2), 2); } }; //******************** void showarea(SHAPE& shape){ cout
@patrickorone1149
@patrickorone1149 3 жыл бұрын
Great bro
@lesstor99
@lesstor99 3 жыл бұрын
if you don't use a constructor at all, the compiler writes one for you, remember this: RAII (resource acquisition is initialization) Initializing the member variables is not all the constructor is doing... Also, you might want to think about design choices...in a way, classes and objects are supposed to mimic real world "objects"...likewise for inheritance...its supposed to make sense
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Great answer here. This person object-oriented programs.
@maxim25o2
@maxim25o2 3 жыл бұрын
@@lesstor99 I know what You mean. Creating obiect in side of another obiect. But this code will create obiect by inheritance anyway. I don't see sense to play manually with constructors. Because it will create that class always. But there is a reason why sometimes can be used constructor and destructor. If Your class must live longer then Your parent or outside of function where was created. Or You use if statement to create obiect only if there is a reason, like with low memory embedded systems. Or if Your obiect need live shorter time in side of function because You have big loop statement.
@brandonarnold7430
@brandonarnold7430 2 жыл бұрын
You can't tell me that dislike isn't himself. Who but the creator would dislike this video :/
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks for the compliment...I think? Took me a sec to understand what you were saying...I guess? LOL. Glad it was helpful!
@ivans7
@ivans7 2 жыл бұрын
i don't wunderstand why ppl think making things complicated is cool !! That's really unwelcoming !! I mean inheritance could be taught with simple variables and a single function...You genius make things tough and there indians took the advantages😑😑
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
I don't understand it either. Maybe some people think it makes them look smarter than they are or something? Any fool can make something overly complicated. It takes genius to make something simple. I'm not saying that I'm a genius or that I'm better than anyone else, just that it's my goal and philosophy to make things as simple as possible. Sometimes, I do a great job. Sometimes I have to try a few times to get it right. Teaching takes practice just like learning takes practice.
@ivans7
@ivans7 2 жыл бұрын
@@ProfessorHankStalica It'd be better if you can use simple terms in your videos
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks for the feedback and I can understand that. I try to keep it as simple as I can, but there are some terms you just can't simplify. An object is an object, for example. There are terms that my students are expected to learn and use so that's usually why you'll hear the jargon that you do.
@temercastle7193
@temercastle7193 3 жыл бұрын
16:15 starts talking about Virtual Functions
@melmthombeni9761
@melmthombeni9761 Жыл бұрын
Its not that much visible otherwise thanks
C++ Polymorphism and Virtual Member Functions [6]
12:13
Professor Hank Stalica
Рет қаралды 10 М.
Каха и суп
00:39
К-Media
Рет қаралды 6 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 24 МЛН
Cool Items! New Gadgets, Smart Appliances 🌟 By 123 GO! House
00:18
123 GO! HOUSE
Рет қаралды 17 МЛН
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 78 МЛН
Rule of Three Implementation in C++
44:27
ReelLearning
Рет қаралды 69 М.
Practical Polymorphism C++
41:44
javidx9
Рет қаралды 124 М.
C++ Abstract base classes and pure virtual functions [7]
7:09
Professor Hank Stalica
Рет қаралды 3,9 М.
C++ Header Files & Inheritance Review
15:15
Logic Lambda
Рет қаралды 3 М.
C++ Tutorial 10 : Object Oriented Programming
40:25
Derek Banas
Рет қаралды 240 М.
C++ OOP - What is polymorphism in programming? (simple example)
15:20
Dynamic Binding (Polymorphism) With The Virtual Keyword | C++ Tutorial
9:57
Object Oriented Programming (OOP) in C++ Course
1:30:26
freeCodeCamp.org
Рет қаралды 2,5 МЛН
Inheritance in C++
8:00
The Cherno
Рет қаралды 381 М.
Каха и суп
00:39
К-Media
Рет қаралды 6 МЛН