📚 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
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.
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.
@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.
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!!
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 🇮🇳 🤎
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. ❤
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!
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.
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.
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
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 👍
@@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.
@@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.
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.
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.
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.
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.
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
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 🙂
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?
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? 😊😊
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 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.
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?
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. 😊
@@CodeBeauty yes, that answers my question. that would work. however, using RAII-compliant smart pointers would provide a better, less cluttered solution?
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.
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
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!
@@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.
📚 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
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.
i'm so happy that i fund your channel. you saved my career
so glad to help
the best programming teacher ever
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.
Glad that I managed to provide some insight for someone who already has experience 😊😊
You outdid yourself today....Great professor......Thank you very much indeed.
you're making me feel shy 🤭
Lovely channel. Lovely lady.....I've learnt a lot. Thank you so much.
@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.
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.)
Another excellent video! Cheers!🤗
Thank you! Cheers!
Best explanation ever. I understand your videos perfectly. Thanks
You are welcome!
I prefer watching your videos than scrolling on social media in my free time, keep making amazing videos❤
Thank you for teaching debugger useage at the same time as well.
thank you for making these videos
i like how you recommended the millionaire fastlane. its one of my favorites 😊
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 🚀🚀
Mam please create a separate video on constructors
The previous video is about that.
ua-cam.com/video/hBq6EGX5II4/v-deo.html
There you go, thank you Marcos
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!!
@CodeBeauty I can't agree more with @vickymar. How do you think about it?
great video, you really shared important info that i didn't know before
Glad it was helpful!
Love from India , i have purchased a course of DSA and this topic was hard for me but now it's crystal-clear. Thanks!
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 🇮🇳 🤎
@@CodeBeauty how to enroll ma'am?
very good way you cover the topics, as for the bug, we should do a deep copy
Excelent, that's the reason, and to fix it we should implement copy constructor which I will cover in one of my next videos.
can you upload video about graph data structure. It is great help for me.❤
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. ❤
best tutor for c++ , very detailed explanation
🥰🥰
amazing, thank you
Glad you liked it!
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!
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.
Thank you
Welcome!
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.
Thankx
There are many other errors in this video, check my comment and the comment from
@billjohnes9380
Beautiful explanation thank you ur a miracle worker
Do you do java please 🙏 in need of help
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
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 👍
Yess, correct answer! ✋️
but, what's the problem? where is the leak memory? The book3 lost reference but not leak memory in my option. Please help me.
@@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.
@@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.
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.
Thank you. I learned a lot from your C++ video tutorials. Stay blessed ❤
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.
Please make a video on Aggregation and composition in C++
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.
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.
love from india ma'am your way of explaination is best among all youtubers i love you ma'am❤❤
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
@@CodeBeautyI am in college fourth semester and currently learning dsa with you just i have finished the linked list video and it was ♾️❤️ 🙌 .
@@CodeBeauty i can see the effort you made to create videos surely appreciated ❤️
And i am sure being your student i can get job in a big MNC.
I am watching from Bangladesh. And mam i am a great fan of you🥰.
Thank you
Can I know if you have an estimation about when you will start the programming course? Thanks in advance
Love your teaching style ❤🎉 , could you do a tutorial series on RUST 😊
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 🙂
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
I make the same code in my visual studio 2012 and here didn't crash code. Everything fine.
well even better 😂
Please can you answer is your course is c++ or c# before I enroll I wanna know thanks 🙏
you helped me a lot thanks
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?
How that pointer is holding the address of an array but the type of the pointer is int not integer array?
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? 😊😊
@@CodeBeauty Awesome explanation Thanks!
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.
you are actually helping me a lot to learn oops♥
Sorry about my ignorance, but where can I find the link of the complete course you mentioned?
Look in the Video description. It ist the third link.
Hello from Lebanon 🇱🇧♥️ I wish you make data structure and algorithms tutorial 😢 Thank you❤
Is there any way to handle multiple classes destructor handle in a single class
Each class should have its own destructor to handle memory allocation and releasing own resources. I hope that answers your questions. Cheers
@@CodeBeauty Yes i understand it 🤗
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++ ?
Unreal engine added support for a new language?
@@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.
Hi saldina I understand your videos very well ....and can U please make a video about UNIONS....Thank U for helping us 🥺🙏
very good...
Good videos
Glad you like them! ❤
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?
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. 😊
@@CodeBeauty yes, that answers my question. that would work. however, using RAII-compliant smart pointers would provide a better, less cluttered solution?
@@CodeBeauty of course, this is off topic. your video is already complete.
Would like if you manage to upload lectures on python language
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.
and one thing more destructor is necessary to used
not strictly but it's always a good practice. ✌
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
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!
@@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.
listning you from pakistan i love you
Jalsa nahi ho raha hain jo sun rahe ho
I'm very well, thank you. Love for Pakistan
❤
❤
i am listning you from pakistan love you
Greetings for Pakistan ❤
Thank you beauty
next video link
❤
🥰
how are you mem
good, you? 😂😂
@@CodeBeautymam please make vedios on Salesforce with subtitles ❤❤❤🙏🙏🙏🙏
ok
❤❤❤
Beautiful honey❤❤❤❤❤ l love you ❤❤❤❤
❤
Honey give me phone number