Some time ago, when I used g++, I could type something like this: Square *s = (class Square*)&r; I think this will probably still work and maybe it explains things better.
It's the difference between using static binding and dynamic binding. If you want C++ to decide which method to use at runtime, you use dynamic binding (virtual), otherwise you use static binding. Sometimes C++ has to decide which destructor to use at run time. Basically, if you expect your class will ever be inherited from, you should make the destructor virtual.
It means that the print() function can not contain any code that modifies class member variables. If print were to try, the compiler will give you an error at compile time. For example, class Foo { int x; void print() const { x = 0; cout
No, because you have to delete each of the objects whose memory addresses are stored in the array. It's an array of pointers that was statically allocated. delete [ ] squares; would attempt to delete the array which is nonsensical because it wasn't dynamically allocated in the first place. It's the Square, Rectangle, and Foo that were dynamically allocated, not squares. Remember that for each new that executes, a corresponding delete has to execute. 3 new execution, 3 delete. new Square, new Rectangle, new Foo So the loop repeats three times, calling delete each time, and just delete. We didn't create 3 arrays dynamically, so we don't use delete [ ]
In case the class with them gets extended in the future to ensure the correct destructor gets invoked. It's good to make them virtual as well. It's a best practice kind of thing to ensure extensibility
Sir , I did not understand something , which is ; Why we put virtual for all destructors meanwhile put virtual for only base class's print function . Thank you for considering :)
You use virtual on methods that you expect will be overridden in child classes. If there is a chance your class gets inherited from, then it's a good practice to make destructors virtual. That way, if the classes get used in a polymorphic context, you ensure the correct destructor is executed for each object.
Thank you for the video. Very informative. Sorry to be that guy but the relationship between square and rectangle threw me off. Every square is a rectangle. Every rectangle is NOT a square. so wouldn't it make sense to make rectangle the base class?
The best explanation I've found on this subject, thank you very much !
You explained too well. Tomorrow is my exam and I understood the topic completely.
Thank you.
Glad it helped!
Absolutely wonderful!
Some time ago, when I used g++, I could type something like this: Square *s = (class Square*)&r; I think this will probably still work and maybe it explains things better.
Mr.Hank please what is the difference betwen ~square(){}; and virtuel ~square(){}; .Thanks for your explanation
It's the difference between using static binding and dynamic binding. If you want C++ to decide which method to use at runtime, you use dynamic binding (virtual), otherwise you use static binding.
Sometimes C++ has to decide which destructor to use at run time. Basically, if you expect your class will ever be inherited from, you should make the destructor virtual.
class Foo {
void print() const { cout
It means that the print() function can not contain any code that modifies class member variables. If print were to try, the compiler will give you an error at compile time.
For example,
class Foo {
int x;
void print() const { x = 0; cout
@@ProfessorHankStalica thank you for explaining. new subscriber 🤝
so why did we make all the destrcutors virtual should not that be just for the base class or we did it for readbilty of the code ?
Best practice to make them virtual in case the class is derived from in the future.
Prof. Hank can't we use delete[] squares instead of the for loop to delete the pointers?
Thank you
No, because you have to delete each of the objects whose memory addresses are stored in the array. It's an array of pointers that was statically allocated.
delete [ ] squares;
would attempt to delete the array which is nonsensical because it wasn't dynamically allocated in the first place.
It's the Square, Rectangle, and Foo that were dynamically allocated, not squares.
Remember that for each new that executes, a corresponding delete has to execute.
3 new execution, 3 delete.
new Square, new Rectangle, new Foo
So the loop repeats three times, calling delete each time, and just delete.
We didn't create 3 arrays dynamically, so we don't use delete [ ]
@@ProfessorHankStalica thanks a lot!
why do we have to include destructors in the classes , derived classes per se , since we don't have pointers inside them??
In case the class with them gets extended in the future to ensure the correct destructor gets invoked. It's good to make them virtual as well. It's a best practice kind of thing to ensure extensibility
what if we did r.print()
amazing keep going
Thank you, I will
Sir , I did not understand something , which is ; Why we put virtual for all destructors meanwhile put virtual for only base class's print function .
Thank you for considering :)
You use virtual on methods that you expect will be overridden in child classes. If there is a chance your class gets inherited from, then it's a good practice to make destructors virtual.
That way, if the classes get used in a polymorphic context, you ensure the correct destructor is executed for each object.
@@ProfessorHankStalica thank you so much.. :)
Thank you for the video. Very informative. Sorry to be that guy but the relationship between square and rectangle threw me off. Every square is a rectangle. Every rectangle is NOT a square. so wouldn't it make sense to make rectangle the base class?
Could be... I might have gotten it backwards in the heat of the moment...my brain doesn't work like it used to.. :-)
@@ProfessorHankStalica Clearly it works very well. The tutorial was not about geometry and it is a fantastic tutorial. Thanks you very much.
im first watching 🤓first like 👍🏻
I like you use a geometric example, people like me can visualize the proposed problem, meaning and scope!
yea but why the hell would you ever need to use all this shyt, keep it simple and readable.