Define A Copy Constructor To Create A Deep Copy Of An Object | C++ Tutorial

Sdílet
Vložit
  • čas přidán 29. 06. 2024
  • Learn how to create and use a copy constructor in C++ to initialize an object by using another object of the same type. The idea when defining a copy constructor is generally to create a copy of the object (as this is what would be expected when we initialize one object with another). The default copy constructor in C++ creates a shallow copy of an object, which may cause problems if our objects use dynamically allocated memory. In this video we demonstrate how to use the copy constructor to create a deep copy of the object, which is a common use case for creating a copy constructor. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
  • Jak na to + styl

Komentáře • 50

  • @ieduardoms
    @ieduardoms Před rokem +11

    Two weeks ago I was watching another channel for learning C++, the other channel was good, but THIS CHANNEL IS WAYS BETTER. Very well explained, thank you so much.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      Thank you for being so supportive Eduardo, I'm glad you've found the C++ videos on this channel to be helpful. :-)

  • @dwivedys
    @dwivedys Před měsícem

    Deep copy and Shallow copy concept sorted! Thank you!

  • @otheusma
    @otheusma Před rokem +1

    everytime i dont fully understand some new concept, i come to this channel and everything starts to make sense!

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      That's awesome Matheus, that makes me very happy to hear that the channel is helpful for you, thanks so much for sharing this! :-)

  • @ValliNayagamChokkalingam

    Thanks a ton! Your students are lucky to have an amazing professor like you!

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      You're welcome Valli-Nayagam, and thank you for the kind words! :-)

  • @punchline1729
    @punchline1729 Před rokem +3

    I subed
    The pace and tone of these tutorials are pretty good, the information penetrates my brain when I watch them

  • @dashdroptv
    @dashdroptv Před rokem +2

    Very thorough explanation with great examples. Thanks for clarifying!

  • @papertowers4216
    @papertowers4216 Před rokem +3

    This was extremely helpful and clear thank you so much.

  • @KaLaka16
    @KaLaka16 Před 2 lety +1

    Thanks a lot, this helped me solve a programming exercise i was stuck at.

  • @drankkkkkk
    @drankkkkkk Před 2 lety +1

    Excellent video!

  • @emfahmi18
    @emfahmi18 Před 2 lety

    This is so helpful, thank you so much

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      You're welcome! 🙂 Thank you for letting me know it was helpful for you.

  • @austinyu7404
    @austinyu7404 Před rokem

    I love u holy moly i was realy scratching my head on copy constructors

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 Před rokem

    great explanation, thank you

  • @isaackanda816
    @isaackanda816 Před rokem

    thanks very much brother man🙌

  • @Anonymous-bu5bg
    @Anonymous-bu5bg Před 11 měsíci +1

    Excellent explanation!!

    • @PortfolioCourses
      @PortfolioCourses  Před 11 měsíci

      I'm glad you enjoyed it! :-)

    • @Anonymous-bu5bg
      @Anonymous-bu5bg Před 11 měsíci

      @@PortfolioCourses do you have whole video like this on oops?

    • @PortfolioCourses
      @PortfolioCourses  Před 11 měsíci

      No, right now I just have videos in the C++ Tutorial playlist covering lots of different OOP concepts. One day I would like to make a whole video covering OOP in C++ that is more of a "full course".

  • @jixuancheng
    @jixuancheng Před 6 měsíci

    @2:17 num2=num1; is using copy assignment operator function, instead copy construct. num2(num1) is using copy construct in my knowledge. That’s very confusing. But concept must be right.

  • @kevinliao9938
    @kevinliao9938 Před rokem

    Really helpful
    Thanks

  • @makaty9883
    @makaty9883 Před 6 měsíci

    thx alot!

  • @itsmaxim01
    @itsmaxim01 Před 5 měsíci +1

    I get why you’re using malloc and free in this case (ie to throw the error) but really you should mention this is not considered good practice (and mention new/delete/smart pointers)

  • @keblinskiwastaken
    @keblinskiwastaken Před rokem

    I did not receive any errors when the exit() was removed. Should I be worried if I dont receive errors or is it just depending on the software? I use vscode.

  • @neonlights7173
    @neonlights7173 Před 9 měsíci

    what editor you use sir?

  • @akashislam9127
    @akashislam9127 Před rokem

    why is const have been used in "Number(const Number &otherNumber)"

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      Great question Akash! :-) Because we will not modify otherNumber, we just use it to make the copy. It would be a mistake if we did. So we help enforce this with the language feature const.

  • @abdellahguennioui1568

    hello, first i would to thank you for this tutorial, i do exactly the same thing in my computer but the deep copy he dosen't work, can you help me?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      You're welcome Abdellah! :-) The code in this video is found here, does using this exact code help resolve the issue? github.com/portfoliocourses/cplusplus-example-code/blob/main/copy_constructor.cpp

    • @abdellahguennioui1568
      @abdellahguennioui1568 Před rokem

      @@PortfolioCourses thank you so much for your videos, it's very helpful, i searched on internet and it's working now, thank you.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      @@abdellahguennioui1568 Awesome I'm glad to hear you got it working, and you're welcome Abdellah! 🙂

  • @ibrahimmalik4590
    @ibrahimmalik4590 Před rokem

    instead of malloc statement can we use new keyword?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      Yes Ibrahim. :-) I used malloc() in this video thinking that many programmers would be coming from C and may be more familiar with it, but a "C++ way" of doing it would be to use new instead.

  • @Victor-fl8ex
    @Victor-fl8ex Před 2 lety

    whts the diff between new and malloc?

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      One day I'm going to make a video on dynamic memory allocation in C++. :-) The big difference is whether the constructor/destructor is called or not though, this might be helpful in the meantime: www.geeksforgeeks.org/new-vs-malloc-and-free-vs-delete-in-c/

    • @Victor-fl8ex
      @Victor-fl8ex Před 2 lety

      @@PortfolioCourses i know what dynamic memory allocation is. What you mean is that when you use New keyword you have to use the delete in order to free the allocated memory, while in alloc it’s done automatically?

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      @@Victor-fl8ex No, with malloc and calloc you need to free the memory that's been allocated, it's not automatic. With new and delete, the constructor and destructor of any object will be executed, with malloc/calloc/free, the destructor and constructor will not be executed.

    • @Victor-fl8ex
      @Victor-fl8ex Před 2 lety

      @@PortfolioCourses ohhhh gotcha. Thanks kevin

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      @@Victor-fl8ex You're welcome! :-D