Abstract Classes And Pure Virtual Functions | C++ Tutorial

  Рет қаралды 15,298

Portfolio Courses

Portfolio Courses

Күн бұрын

How and why to use abstract classes and pure virtual functions in C++. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 46
@brandongill368
@brandongill368 Жыл бұрын
How has this only got 3.9k views! This is a fantastic explanation, thank you :D
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Brandon, I'm glad you enjoyed it! :-)
@M3t4lik
@M3t4lik Жыл бұрын
Clear and concise explanation of the relationship between abstract classes and virtual functions. thank you:)
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome, I’m glad that you found the explanation clear and concise! :-)
@user-rb8vv9pc5e
@user-rb8vv9pc5e 9 ай бұрын
I've watched many of your videos and the thoroughness of your explanations is wonderful. Awesome work!
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
I'm really happy that you're enjoying the content, thank you for the kind feedback! :-D
@mosslnnx
@mosslnnx Жыл бұрын
I really enjoyed watching your tutorials, concise and clear and good examples, Thanks a lot.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Miss, I’m glad you’re enjoying the tutorials! :-)
@ermangurses1278
@ermangurses1278 Жыл бұрын
Very clear explanation, thank you!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Erman! :-)
@laali_puppy
@laali_puppy 4 ай бұрын
Great explanation, thanks!
@clammyclams8968
@clammyclams8968 Жыл бұрын
Very Professional Explanation. Thank You So Much.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, and thank you for the kind words! :-)
@gabrieldias6430
@gabrieldias6430 Жыл бұрын
awesome video
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Gabriel! 🙂
@user-zy9ev1nz3y
@user-zy9ev1nz3y 11 ай бұрын
Nice video, very clear
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
I'm glad you found it clear! :-D
@omercandemirci6410
@omercandemirci6410 Жыл бұрын
Altough my english is bad. I understand purposes of abstract classes. In view of that situation I should say you are good teacher. Thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm really glad to hear that you enjoyed the video Ömer, and thank you for the kind words! :-) And by the way your written english seems pretty good to me!
@abrahamalejandroguzmancard6299
@abrahamalejandroguzmancard6299 Жыл бұрын
You are amazing!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the video! :-)
@mihaibozian
@mihaibozian Жыл бұрын
Thank you sir, I've been struggling why do we need to instantiate a pointer to an abstract class, and now I got it.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Bozian, I’m really glad to hear the video helped you out! :-)
@cndyflss
@cndyflss 9 ай бұрын
thank you sm dude🥳🥳🥳🥳
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
You’re very welcome, thank you for watching! :-)
@dzeno7370
@dzeno7370 2 жыл бұрын
What is the purpouse of using pointers to classes (a.k.a objects) in your example (and also couple of previous videos) if you are not using anything pointer-related stuff such as pointer arithmetics ? Could you do all those examples with something like Shape shapes [] = {Triangle(3,4), Rectangle(5,6)} ..... Can you also quickly elaborate what is the main adventage in general sense of dealing with object using pointers to instances, rather than object instances itself? One more thing: Does polymorphism work only with pointers so maybe you are using "Shape *shapes[]" on purpouse instead of "Shape shapes[]" ?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
These are great questions! :-) Objects that are instantiated using local variables of a function will exist in a portion of memory called 'the stack' that is managed for us automatically, they will have the lifetime of that function call (i.e. the memory that object uses on the stack will be freed when the function returns). But objects that are dynamically allocated (e.g. with 'new') exist on 'the heap', a separate portion of memory that we need to manually manage (e.g. by calling 'delete' to free the memory that object is using). We use pointers to refer to objects that have been dynamically allocated on the heap. Technically, we can use pointers to refer to objects on the stack as well. But using pointers allows to keep a "pointer" (i.e. memory address) to these objects on the heap, and that pointer can be passed around to different functions so they can access the same object too. Sometimes we'll have code where one function creates the object with dynamic memory allocation, and much later, a different function will free the memory associated object, using the pointer to the object. And I do not believe it is possible to implement runtime polymorphism without pointers, as the idea with runtime polymorphism is that the exact method to be called is determined at runtime when the member function of the object the pointer is pointing to is accessed. I don't know how that could really be possible without pointers, but sometimes in languages like C++ that are so large and have so many features, I'll find out there are ways of doing things that I'm totally unaware of. :-) This comment on stackoverflow also seems to suggest that it is not possible though: stackoverflow.com/a/7223724
@dzeno7370
@dzeno7370 2 жыл бұрын
@@PortfolioCourses great answer, thank you :)
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! :-)
@McDonaldIbekwe
@McDonaldIbekwe Ай бұрын
Please, is there a way of doing it with Smart Pointers?
@joelparker
@joelparker Жыл бұрын
great!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the video Joel! :-)
@McDonaldIbekwe
@McDonaldIbekwe Ай бұрын
I did some modification to your program. I even tried using Smart Pointers but it didn't work. #include #include #include #include class Shape { public: virtual double area() = 0; virtual const std::string name() const = 0; }; class Square : public Shape { public: Square(double side) : m_side(side) {} virtual double area() final { return pow(m_side, 2); } virtual const std::string name() const { return "square"; } ~Square() {} private: double m_side; }; class Triangle : public Shape { public: Triangle(double base, double height) : m_base(base), m_height(height) {} virtual double area() final { return 0.5 * m_base * m_height; } virtual const std::string name() const { return "triangle"; } ~Triangle() {} private: double m_base; double m_height; }; class Parallelogram : public Shape { public: Parallelogram(double side1, double side2) : m_side1(side1), m_side2(side2) {} virtual double area() final { return m_side1 * m_side2; } virtual const std::string name() const { return "parallelogram"; } ~Parallelogram() {} private: double m_side1; double m_side2; }; class Circle : public Shape { public: static double pi; Circle(double radius) : m_radius(radius) {} virtual double area() final { return pi * pow(m_radius, 2); } virtual const std::string name() const { return "circle"; } ~Circle() {} private: double m_radius; }; double Circle::pi = 3.1452; int main() { Shape* shapes[4] = { new Square(4), new Triangle(6, 9), new Parallelogram(11, 6), new Circle(7) }; for (int i{ 0 }; i < 4; ++i) { std::cout
@Bl0xxy
@Bl0xxy 7 ай бұрын
You used the new keyword. How do i safely do that without memory leaks?
@animeIsle
@animeIsle Жыл бұрын
Uhm why am I getting this error? I don't understand why I am getting this at all, btw this error is apparently in the for loop cout command: "no match for 'operator
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm not sure, it's hard to know without seeing your code. The original code for the video is found here: github.com/portfoliocourses/cplusplus-example-code/blob/main/abstract_class.cpp. Do you get the error if you use this code? Maybe there is a difference between this code and the code you're using? Apparently this error can happen if you use "end" instead of "endl" when outputting values, so maybe check for that, but the error can occur for other reasons as well.
@animeIsle
@animeIsle Жыл бұрын
@@PortfolioCourses uhm I found the solution to it myself. Apparently if you use a method that uses cout command in a cout statement, then that error shows up. So I fixed it by simply calling the method outside of the cout statement. Thanks for replying though.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@animeIsle That makes sense, I'm glad you figured it out! 🙂
@animeIsle
@animeIsle Жыл бұрын
@@PortfolioCourses Yea and oh boy, figuring it out yourself gives you a lot of satisfaction. You're surprisingly replying to the comments even if the video was uploaded quite a while ago. Hard to find people like you ngl. I respect you for that.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@animeIsle I totally agree that figuring it out yourself feels great, that's the "addiction" of programming. 🙂 And thanks for the kind words!
@mohannadrababah757
@mohannadrababah757 Жыл бұрын
Thank you so much
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Mohannad! :-)
@Victor-fl8ex
@Victor-fl8ex 2 жыл бұрын
Yo kevin, its me once again: its not a video-related question but, does template for switch case exist? for example i got this: case 1: std::cout
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
The short answer is "not really". Rather than use templating, something like a function would be a better approach to reducing code duplication in this case.
@Victor-fl8ex
@Victor-fl8ex 2 жыл бұрын
@@PortfolioCourses damn. Didn’t think about that. Thanks master!!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@Victor-fl8ex Haha you're welcome! :-D
Pure Virtual Destructors | C++ Tutorial
18:12
Portfolio Courses
Рет қаралды 3,1 М.
Introduction To Classes And Objects | C++ Tutorial
11:49
Portfolio Courses
Рет қаралды 50 М.
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 33 МЛН
Llegó al techo 😱
00:37
Juan De Dios Pantoja
Рет қаралды 59 МЛН
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 921 М.
C++ Abstract base classes and pure virtual functions [7]
7:09
Professor Hank Stalica
Рет қаралды 4 М.
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 МЛН
Two Ways To Do Dynamic Dispatch
19:54
Logan Smith
Рет қаралды 73 М.
Every single feature of C# in 10 minutes
9:50
Train To Code
Рет қаралды 111 М.
new & delete Operators For Dynamic Memory Allocation | C++ Tutorial
15:52
Portfolio Courses
Рет қаралды 27 М.
Function Templates | C++ Tutorial
10:54
Portfolio Courses
Рет қаралды 7 М.
C++ Polymorphism and Virtual Member Functions [6]
12:13
Professor Hank Stalica
Рет қаралды 10 М.