Destructors in Programming: Practical Demonstration

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 129

  • @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 Год назад +11

    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.

  • @fatimakamran9996
    @fatimakamran9996 Год назад +6

    Mam please create a separate video on constructors

    • @SZMarcos
      @SZMarcos Год назад +6

      The previous video is about that.

    • @davidmorris2848
      @davidmorris2848 Год назад

      ruclips.net/video/hBq6EGX5II4/видео.html

    • @CodeBeauty
      @CodeBeauty  Год назад

      There you go, thank you Marcos

  • @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

  • @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 7 месяцев назад

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

  • @Ghisisan_
    @Ghisisan_ Год назад +7

    Another excellent video! Cheers!🤗

  • @威廷-n5o
    @威廷-n5o 10 месяцев назад +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.)

  • @jaijaijaijai123
    @jaijaijaijai123 Месяц назад

    @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.

  • @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. ❤

  • @dundyd3612
    @dundyd3612 Год назад +5

    thank you for making these videos

  • @l.p.1967
    @l.p.1967 Год назад +5

    amazing, thank you

  • @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 🚀🚀

  • @danny12345
    @danny12345 Год назад +4

    Thank you

  • @marym9003
    @marym9003 Год назад +4

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

  • @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❤

  • @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.

  • @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.

  • @ertemeren
    @ertemeren Год назад +1

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

  • @swastikbhandari2644
    @swastikbhandari2644 8 месяцев назад +1

    best tutor for c++ , very detailed explanation

  • @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

  • @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.

  • @bousriabderraouf6696
    @bousriabderraouf6696 Год назад

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

  • @QuizzzME
    @QuizzzME 7 месяцев назад

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

  • @sujalthakkar2118
    @sujalthakkar2118 9 месяцев назад

    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  9 месяцев назад

      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 7 месяцев назад

      @@CodeBeauty how to enroll ma'am?

  • @user-8863
    @user-8863 2 месяца назад

    Beautiful explanation thank you ur a miracle worker

  • @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.

  • @AravindhSK
    @AravindhSK Год назад

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

  • @MuhammadAbdullah-cl2hc
    @MuhammadAbdullah-cl2hc Год назад +1

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

  • @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.

  • @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 🤗

  • @pedrovinicius5128
    @pedrovinicius5128 Год назад

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

  • @chinthaka7027
    @chinthaka7027 11 месяцев назад +1

    Thank you

  • @proexpertsolution242
    @proexpertsolution242 Год назад +1

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

  • @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.

  • @orhan10
    @orhan10 8 месяцев назад

    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?

  • @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.

  • @shamaldesilva9533
    @shamaldesilva9533 Год назад

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

  • @zoro-i
    @zoro-i Год назад

    you helped me a lot thanks

  • @juniorlucival
    @juniorlucival Год назад +2

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

  • @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.

  • @فيصلالبابا
    @فيصلالبابا Год назад +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

  • @abdullahbilal9393
    @abdullahbilal9393 10 месяцев назад

    you are actually helping me a lot to learn oops♥

  • @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.

  • @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.

  • @mehulshah9027
    @mehulshah9027 Год назад

    very good...

  • @bebagcreativity3149
    @bebagcreativity3149 Год назад

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

  • @tkj12.hendrik98
    @tkj12.hendrik98 Год назад +1

    Good videos

  • @xamsemahdi3861
    @xamsemahdi3861 Год назад +6

  • @marwasolh9449
    @marwasolh9449 Год назад

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

  • @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

  • @youbeyou9769
    @youbeyou9769 Год назад

    next video link

  • @AlphaRocketLeo
    @AlphaRocketLeo Год назад +1

    and one thing more destructor is necessary to used

    • @CodeBeauty
      @CodeBeauty  Год назад

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

  • @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.

  • @Farzadx-65
    @Farzadx-65 7 месяцев назад +2

  • @PabloSanDev
    @PabloSanDev Год назад

    Thank you beauty

  • @abhishekmaurya31
    @abhishekmaurya31 3 месяца назад

    ❤❤❤

  • @waheedkhan3508
    @waheedkhan3508 Год назад +6

    i am listning you from pakistan love you

    • @CodeBeauty
      @CodeBeauty  Год назад +2

      Greetings for Pakistan ❤

  • @waheedkhan3508
    @waheedkhan3508 Год назад +7

    how are you mem

    • @CodeBeauty
      @CodeBeauty  Год назад +1

      good, you? 😂😂

    • @Relaxingmusic-nk7py
      @Relaxingmusic-nk7py Год назад

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

  • @xamsemahdi3861
    @xamsemahdi3861 Год назад +7

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

  • @jorgetorres1670
    @jorgetorres1670 Год назад

    ok

  • @paganisttc
    @paganisttc 21 день назад

    Too elementary
    For dummies