Destructors in Programming: Practical Demonstration

Поділитися
Вставка

КОМЕНТАРІ • 128

  • @CodeBeauty
    @CodeBeauty  Рік тому +22

    📚 Learn how to solve problems and build projects with these Free E-Books ⬇
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    Experience the power of practical learning, gain career-ready skills, and start building real applications!
    This is a step-by-step course designed to take you from beginner to expert in no time!
    💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
    Use it quickly, because it will be available for a limited time.
    ****CODE FROM THE VIDEO IS BELOW****
    #include
    #include
    using namespace std;
    class Book {
    public:
    string Title;
    string Author;
    int* Rates;
    int RatesCounter;
    int* Year;
    Book(string title, string author) {
    Title = title;
    Author = author;
    RatesCounter = 2;
    Rates = new int[2];
    Rates[0] = 4;
    Rates[1] = 5;
    Year = new int;
    *Year = 10;
    cout

    • @johnniewalkerjohnniewalker2459
      @johnniewalkerjohnniewalker2459 Рік тому +2

      We need a copy constructor for the book3 object.book3 object is declared and also initialized simultaneously.Firstly book3 will be destroyed but also will be freed the dynamic memory from the year pointer.When book1 goes to get destructed it will not find memory to free so the delete year will not work because the pointer year pointa nowhere.

  • @milkamilkica1935
    @milkamilkica1935 Рік тому +10

    i'm so happy that i fund your channel. you saved my career

  • @nv9usb381
    @nv9usb381 Рік тому +7

    the best programming teacher ever

  • @DeadlyRollick
    @DeadlyRollick Рік тому +13

    Funny that these are always something that I use when workin with cpp and know what they do but feel it is nice to get an indepth example of what they are actually doing.

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      Glad that I managed to provide some insight for someone who already has experience 😊😊

  • @naderhumood
    @naderhumood Рік тому +10

    You outdid yourself today....Great professor......Thank you very much indeed.

    • @CodeBeauty
      @CodeBeauty  Рік тому +3

      you're making me feel shy 🤭

    • @naderhumood
      @naderhumood Рік тому +1

      Lovely channel. Lovely lady.....I've learnt a lot. Thank you so much.

  • @jaijaijaijai123
    @jaijaijaijai123 10 днів тому

    @16:21 because we are simply copying the contents of object book 2 into book 3 and pointer is also getting copied. So, when book 3 rates pointer memory is freed (after destructor of book3 called first), book2's rates memory is also freed(since point to same location), hence when book2's destructor is called, it tries to free unallocated memory and throws error.

  • @威廷-n5o
    @威廷-n5o 9 місяців тому +1

    You could use member initialize list so that the compiler won't construct twice.
    (By the way, thank you for your tutorials, they're excellent.)

  • @Ghisisan_
    @Ghisisan_ Рік тому +7

    Another excellent video! Cheers!🤗

  • @gollygobih6527
    @gollygobih6527 Рік тому +8

    Best explanation ever. I understand your videos perfectly. Thanks

  • @attaullahbrohi
    @attaullahbrohi Рік тому +1

    I prefer watching your videos than scrolling on social media in my free time, keep making amazing videos❤

  • @ertemeren
    @ertemeren Рік тому +1

    Thank you for teaching debugger useage at the same time as well.

  • @dundyd3612
    @dundyd3612 Рік тому +5

    thank you for making these videos

  • @adreezy6576
    @adreezy6576 Рік тому +1

    i like how you recommended the millionaire fastlane. its one of my favorites 😊

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      Mine too 😃 I've read that book 3 times so far, in different stages of my life, and every time I get something new and useful from it 🚀🚀

  • @fatimakamran9996
    @fatimakamran9996 Рік тому +6

    Mam please create a separate video on constructors

    • @SZMarcos
      @SZMarcos Рік тому +6

      The previous video is about that.

    • @davidmorris2848
      @davidmorris2848 Рік тому

      ua-cam.com/video/hBq6EGX5II4/v-deo.html

    • @CodeBeauty
      @CodeBeauty  Рік тому

      There you go, thank you Marcos

  • @vickymar3836
    @vickymar3836 Рік тому +6

    Hi, Thanks for the great video. Can you please clarify one doubt.
    In 14:25 you have said that using delete instead of delete[] will just deallocate the memory used by Rates (ie Left side of the excel). Logically, using delete should instead deallocate only the first memory cell of the contiguous memory block (ie first element of right side of excel ) right ? Memory leaks will still be there because apart from the first cell of the contiguous memory, other cells have not been deallocated.
    Thanks!!

    • @jhon3991
      @jhon3991 6 місяців тому

      @CodeBeauty I can't agree more with @vickymar. How do you think about it?

  • @marym9003
    @marym9003 Рік тому +4

    great video, you really shared important info that i didn't know before

  • @sujalthakkar2118
    @sujalthakkar2118 8 місяців тому

    Love from India , i have purchased a course of DSA and this topic was hard for me but now it's crystal-clear. Thanks!

    • @CodeBeauty
      @CodeBeauty  8 місяців тому

      I'm happy to hear that. You should definitely enroll in my Practical Programming Course to learn how to build real apps and to get skills required for a job. Fell free to use the discount code from the description of this video for a special discount. 😊 Love for India 🇮🇳 🤎

    • @milanrai1556
      @milanrai1556 6 місяців тому

      @@CodeBeauty how to enroll ma'am?

  • @geosimplem
    @geosimplem Рік тому +4

    very good way you cover the topics, as for the bug, we should do a deep copy

    • @CodeBeauty
      @CodeBeauty  Рік тому

      Excelent, that's the reason, and to fix it we should implement copy constructor which I will cover in one of my next videos.

  • @ccsiri
    @ccsiri Рік тому +8

    can you upload video about graph data structure. It is great help for me.❤

    • @CodeBeauty
      @CodeBeauty  Рік тому

      As always, if there are enough requests, I will be more than happy to cover that topic too. In the meantime, I'm trying to focus on topics with higher demand. Stay in touch. ❤

  • @swastikbhandari2644
    @swastikbhandari2644 7 місяців тому +1

    best tutor for c++ , very detailed explanation

  • @l.p.1967
    @l.p.1967 Рік тому +5

    amazing, thank you

  • @kassavagt7393
    @kassavagt7393 Рік тому +1

    This is probably the third video of yours that I have watched today, and surely the teacher I am looking for!
    You explain things with details and a lot of "What if's" that keeps things interesting, but the best part is having your human figure in the video looking to the camera, For me, I feel like you are talking to me directly as a teacher, this keeps me a lot more focused in class. Thank you for your lessons, sincerely!

    • @georgiosdoumas2446
      @georgiosdoumas2446 Рік тому

      4:00 I think it is not a good practice to do these assignments in the body of the constructor in the body, better do them in the initialization list. Viewers of this video BEWARE , you have to realize that what you see is not high quality code. DO not trust what you see here. Go to some good books.

  • @danny12345
    @danny12345 Рік тому +4

    Thank you

  • @seersl123
    @seersl123 Рік тому +3

    Correction! Actually "delete Rates" will delete the first element (in this case first int variable in the array). "delete [] Rates" will delete all the elements.

    • @sohankaushik7
      @sohankaushik7 Рік тому

      Thankx

    • @georgiosdoumas2446
      @georgiosdoumas2446 Рік тому

      There are many other errors in this video, check my comment and the comment from
      @billjohnes9380

  • @user-8863
    @user-8863 Місяць тому

    Beautiful explanation thank you ur a miracle worker

  • @jamalbello9077
    @jamalbello9077 Рік тому +7

    Do you do java please 🙏 in need of help

    • @CodeBeauty
      @CodeBeauty  Рік тому

      Currently I'm focusing on my C# practical programming course and I'm telling you this because it will be focused mostly on programming concepts and developing logic, which is transferable on every programming language, and it will certainly help you in learning Java too, for sure. Here you can subscribe for discount, and I will notify you when the course is out, so don't miss it: bit.ly/SimplifyingCoding

  • @qcnck2776
    @qcnck2776 Рік тому +7

    At the end: book3 is assigned book2 as a shallow copy. When book2 is deleted, book3 now has no access to the actual members of book2?
    Great video, BTW 👍

    • @CodeBeauty
      @CodeBeauty  Рік тому +3

      Yess, correct answer! ✋️

    • @juniorlucival
      @juniorlucival Рік тому +2

      but, what's the problem? where is the leak memory? The book3 lost reference but not leak memory in my option. Please help me.

    • @qcnck2776
      @qcnck2776 Рік тому

      @@juniorlucival I think the problem is that book3 still exists, and when it tries to access its members, it may get garbage values or an error. Secondly, if the object has pointers, if book3 is deleted first, the pointer is the only thing being deleted, and the allocated memory cannot be accessed by book2 or any other object, and is lost.
      Perhaps someone more knowledgeable than me can clarify.

    • @juniorlucival
      @juniorlucival Рік тому

      @@qcnck2776 i know that, i do this way. In some cases it can be useful. When a pointer loses its reference, its not a problem if was part of your strategy, and i dont see any memory leaks or issues, even when deleting book2.

    • @billjohnes9380
      @billjohnes9380 Рік тому

      When book2 is deleted, book3 has already been deleted, so it cannot have or not have any access because it does not exist at the time of book2 deletion.

  • @AravindhSK
    @AravindhSK Рік тому

    Thank you. I learned a lot from your C++ video tutorials. Stay blessed ❤

  • @georgiosdoumas2446
    @georgiosdoumas2446 Рік тому +2

    4:00 I think it is not a good practice to do these assignments in the body of the constructor in the body, better do them in the initialization list. Viewers of this video BEWARE , you have to realize that what you see is not high quality code.

  • @MuhammadAbdullah-cl2hc
    @MuhammadAbdullah-cl2hc Рік тому +1

    Please make a video on Aggregation and composition in C++

  • @kaiwalyakshirsagar
    @kaiwalyakshirsagar Рік тому

    Destructor 7 RULES [ IMP ]
    1) Destructor always starts with "~".
    2) Destructor and constructor do not have any return type.
    3) Name of the constructor and destructor is the same as the class name.
    4) Destructor does not require any input parameter.
    5) Destructor is a member function of the class.
    6) Destructor needs to be placed is always a public member.
    7) Multiple constructors are possible in one class, but Destructor should be only one.

    • @billjohnes9380
      @billjohnes9380 Рік тому

      The 6th one is wrong.
      If you need to write a singleton class you should place both the constructor and destructor to the private section.
      Otherwise, someone else in the program will be able to construct one more instance of the singleton or destruct the existing one.

  • @adityasinghrajput7815
    @adityasinghrajput7815 Рік тому +1

    love from india ma'am your way of explaination is best among all youtubers i love you ma'am❤❤

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      Thank You very much. In my days of college, I've learned to value someone who has the skill of transferring knowledge more than just having knowledge so it's always nice to get feedback from someone who appreciates your work. Much love for India

    • @adityasinghrajput7815
      @adityasinghrajput7815 Рік тому

      @@CodeBeautyI am in college fourth semester and currently learning dsa with you just i have finished the linked list video and it was ♾️❤️ 🙌 .

    • @adityasinghrajput7815
      @adityasinghrajput7815 Рік тому

      @@CodeBeauty i can see the effort you made to create videos surely appreciated ❤️

    • @adityasinghrajput7815
      @adityasinghrajput7815 Рік тому

      And i am sure being your student i can get job in a big MNC.

  • @proexpertsolution242
    @proexpertsolution242 Рік тому +1

    I am watching from Bangladesh. And mam i am a great fan of you🥰.

  • @chinthaka7027
    @chinthaka7027 10 місяців тому +1

    Thank you

  • @bousriabderraouf6696
    @bousriabderraouf6696 Рік тому

    Can I know if you have an estimation about when you will start the programming course? Thanks in advance

  • @shamaldesilva9533
    @shamaldesilva9533 Рік тому

    Love your teaching style ❤🎉 , could you do a tutorial series on RUST 😊

  • @فيصلالبابا
    @فيصلالبابا Рік тому +2

    thank you for your videos
    but please you didn't explain the copy constructor
    I really like your videos
    this is one of the most perfect explanations that I've ever seen 🙂

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      Copy c-tor video will be out as soon as I find time to edit it, it's already filmed ✌️😁❤️

    • @فيصلالبابا
      @فيصلالبابا Рік тому

      @@CodeBeauty thank you very much

  • @juniorlucival
    @juniorlucival Рік тому +2

    I make the same code in my visual studio 2012 and here didn't crash code. Everything fine.

  • @QuizzzME
    @QuizzzME 6 місяців тому

    Please can you answer is your course is c++ or c# before I enroll I wanna know thanks 🙏

  • @zoro-i
    @zoro-i Рік тому

    you helped me a lot thanks

  • @orhan10
    @orhan10 7 місяців тому

    Because of book3 holds the address of book1 and book3 gets deleted book 1 will be deleted too program will find that and you can't delete something that is already deleted there shall be an error, am I right ma'am?

  • @atulkrjha
    @atulkrjha Рік тому +2

    How that pointer is holding the address of an array but the type of the pointer is int not integer array?

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      It is possible because pointer is pointing to the first element of the array and that element is of type int, and in addition that FIRST element has the same address as the array itself. Also, please keep in mind, when you are initializing dynamic arrays like this, you also need another variable that holds the size of the array, which is in our case RatesCounter. Neat right? 😊😊

    • @atulkrjha
      @atulkrjha Рік тому

      @@CodeBeauty Awesome explanation Thanks!

    • @georgiosdoumas2446
      @georgiosdoumas2446 Рік тому

      The memory address is represented as a big integer , as an int32_t or a int64_t for bigger systems. So the value of the pointer is just an integer.

  • @abdullahbilal9393
    @abdullahbilal9393 8 місяців тому

    you are actually helping me a lot to learn oops♥

  • @pedrovinicius5128
    @pedrovinicius5128 Рік тому

    Sorry about my ignorance, but where can I find the link of the complete course you mentioned?

  • @marwasolh9449
    @marwasolh9449 Рік тому

    Hello from Lebanon 🇱🇧♥️ I wish you make data structure and algorithms tutorial 😢 Thank you❤

  • @AlphaRocketLeo
    @AlphaRocketLeo Рік тому +1

    Is there any way to handle multiple classes destructor handle in a single class

    • @CodeBeauty
      @CodeBeauty  Рік тому

      Each class should have its own destructor to handle memory allocation and releasing own resources. I hope that answers your questions. Cheers

    • @AlphaRocketLeo
      @AlphaRocketLeo Рік тому

      @@CodeBeauty Yes i understand it 🤗

  • @v-sig2389
    @v-sig2389 Рік тому

    Hello Salina ! I know it is impolite to change the subject, but ... what do you think about the new language that can be used in Unreal instead of C++ ?

    • @pratham30403
      @pratham30403 Рік тому

      Unreal engine added support for a new language?

    • @v-sig2389
      @v-sig2389 Рік тому

      @@pratham30403 yes, they added Verse, a simpler language that still offers the power of a language while not needong to recompile the whole editor at each code update.

  • @bebagcreativity3149
    @bebagcreativity3149 Рік тому

    Hi saldina I understand your videos very well ....and can U please make a video about UNIONS....Thank U for helping us 🥺🙏

  • @mehulshah9027
    @mehulshah9027 Рік тому

    very good...

  • @tkj12.hendrik98
    @tkj12.hendrik98 Рік тому +1

    Good videos

  • @thesoftwareproject5798
    @thesoftwareproject5798 Рік тому +1

    hey code beauty, nice video. so one question (for the advanced programmers): how do you prevent memory leaks if an exception occurs in the constructor? for example at line 19?

    • @CodeBeauty
      @CodeBeauty  Рік тому

      Good point! So, to prevent scenario where we allocate memory, and exception happens which prevents deallocating memory in destructor, we can use Try-Catch block. We could wrap all memory allocation inside constructor with Try clause, and in Catch we could release it as we do now in destructor.
      Something like this:
      try {
      Rates = new int[2];
      Rates[0] = 4;
      Rates[1] = 5;
      Year = new int;
      *Year = 10;
      }
      catch (const std::exception&) {
      delete[] Rates;
      Rates = nullptr;
      delete Year;
      Year = nullptr;
      throw;
      }
      Hope that answers your question. 😊

    • @thesoftwareproject5798
      @thesoftwareproject5798 Рік тому

      @@CodeBeauty yes, that answers my question. that would work. however, using RAII-compliant smart pointers would provide a better, less cluttered solution?

    • @thesoftwareproject5798
      @thesoftwareproject5798 Рік тому

      @@CodeBeauty of course, this is off topic. your video is already complete.

  • @bilalysf
    @bilalysf Рік тому +1

    Would like if you manage to upload lectures on python language

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      There is definitely plan to do it in future but in the meantime, I'm working on creating course for practical programming in C#, with full application as you would see it in real world and all requirements that such application might have. Since this course will focus on developing your logic and your thinking as programmer, I can assure you that the programming concepts and skills you'll learn in this course are applicable to any language (Python as well), and I'm confident that you'll find it to be a valuable learning experience. I encourage you to give it a try. Use this link bit.ly/SimplifyingCoding to earn special discount and get notified once I publish it.

  • @AlphaRocketLeo
    @AlphaRocketLeo Рік тому +1

    and one thing more destructor is necessary to used

    • @CodeBeauty
      @CodeBeauty  Рік тому

      not strictly but it's always a good practice. ✌

  • @billjohnes9380
    @billjohnes9380 Рік тому +3

    You explain wrong things.
    Non-array version of delete expression does not delete the address in the pointer itself: it still deletes the allocated memory at that address.
    In the case you mentioned and on the platform/compiler you use, there is no difference between the two.
    Yes, applying a non-array delete expression to the address returned by an array new expression is UB.
    But in this case of the platform/compiler, the UB looks like is expected by a programmer behavior: nothing unusual occurs.
    C++ is the language of languages, and no one on Earth knows it completely.
    Your level is at least 3-5 times lower than it is necessary to explain simple things in C++.
    You pass string objects to the constructor by value.
    You does not use initialization in the constructor like that: Book(string const &title, string const &author): Title{title}, Author{author} {cout

    • @georgiosdoumas2446
      @georgiosdoumas2446 Рік тому +1

      Damn , just a few minutes ago, I also made a comment about her not using constructor initialization list! And then I started scrolling through the comments. I guess we do not have to feel sorry for the poor people that watch her videos and have the fake impression that they learn something, they must be total beginners, students of computer science that are struggling with C++ and just want to pass the class. I hope those people are not going to work with C or C++ , and they will go to work with other languages. So tell me what modern books do you recommend? I have read the "Thinking in C++" from the free html version, from 2004, and also an old book from 1998 "Navigating Object Oriented design in C++" , the 1999 book of "C++ FAQs" is another book that I have started reading but not yet finished, along with the 2005 "More Effective C++" (I search in the z_l1brary and found the 11th printing that has errors corrected) , I have also read half of Lipmans "C++ primer" 2012 edition. Not yet the "C++ programming language" from 2014 edition. But I have found a book with good reviews on amazon , " Deciphering Object-Oriented Programming with C++" from 2022, and it seems good. Another one that seems good is the "Professional C++" now on its 5th edition from 2021, I hope to read it next year (because for now I started reading some books on Qt5, for my next developers role, it will by my 2nd position, my 1st position that I started in Oct 2022 is about micro-controllers, using C++98 mainly )
      When I started watching the video, I was wondering "will she mention when the destructor should be virtual?" , but the level is too simple , and as you said even at that simple level she presents some things wrong. I am surprised that your comment is not deleted actually!

    • @billjohnes9380
      @billjohnes9380 Рік тому

      ​@@georgiosdoumas2446 I read books very-very rarely, so I cannot recommend you modern books.
      However, who impressed me most of all is Scott Meyers, but in December 2015, Meyers announced his retirement from the world of C++.
      So, all his books and videos are constantly obsoleting every year.
      The comment is not deleted probably because, from one side, she may have no time to monitor comments for all her videos.
      From the other side, there are not so many comments for the video, but comments are important to promote the video, so she may feel sorry to delete them.

  • @waheedkhan3508
    @waheedkhan3508 Рік тому +6

    listning you from pakistan i love you

    • @ImSecular
      @ImSecular Рік тому +3

      Jalsa nahi ho raha hain jo sun rahe ho

    • @CodeBeauty
      @CodeBeauty  Рік тому

      I'm very well, thank you. Love for Pakistan

  • @xamsemahdi3861
    @xamsemahdi3861 Рік тому +6

  • @waheedkhan3508
    @waheedkhan3508 Рік тому +6

    i am listning you from pakistan love you

  • @PabloSanDev
    @PabloSanDev Рік тому

    Thank you beauty

  • @nitika9769
    @nitika9769 Рік тому

    next video link

  • @Farzadx-65
    @Farzadx-65 6 місяців тому +2

  • @waheedkhan3508
    @waheedkhan3508 Рік тому +7

    how are you mem

    • @CodeBeauty
      @CodeBeauty  Рік тому +1

      good, you? 😂😂

    • @Relaxingmusic-nk7py
      @Relaxingmusic-nk7py Рік тому

      ​@@CodeBeautymam please make vedios on Salesforce with subtitles ❤❤❤🙏🙏🙏🙏

  • @jorgetorres1670
    @jorgetorres1670 Рік тому

    ok

  • @abhishekmaurya31
    @abhishekmaurya31 2 місяці тому

    ❤❤❤

  • @xamsemahdi3861
    @xamsemahdi3861 Рік тому +7

    Beautiful honey❤❤❤❤❤ l love you ❤❤❤❤