Difference between copy constructor and assignment operator in C++ (OOP tutorial for beginners)

preview_player
Показать описание
In this programming tutorial, we will learn the difference between an assignment operator and a copy constructor in C++. I'll teach you how to use an assignment operator as well as how to use a copy constructor. We will also learn some common mistakes that developers make when working with assignment operators and copy constructors. It is crucial that you understand the difference between these two so that you don't make any bugs while using them.
Enjoy the video and leave your questions in the comment section.

If you want to gain more practical experience in building real apps and get career-ready skills, join my Practical Programming Course below.

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.

📚 Learn programming with these Free E-Books ⬇

I use it to enhance the performance, features, and support for C, C#, and C++ development in Visual Studio.
It is a powerful, secure text editor designed specifically for programmers.

Related videos:

CONTENTS:
00:00 - Assignment operator VS copy constructor
01:06 - Where to learn practical programming
03:19 - Explaining the startup code
06:15 - How we use the copy constructor
11:10 - How we use the assignment operator
15:36 - The most important difference between the assignment operator and copy constructor
22:44 - Working with pointers

Add me on:

*******CODE IS IN THE COMMENTS*******
Рекомендации по теме
Комментарии
Автор

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!💰Use this coupon to save 10% (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

📚 Learn programming with these Free E-Books ⬇

FROM THE

#include <iostream>
using namespace std;

class Movie {
public:
string Name;
string Genre;
float Rating;

Movie(string name, string genre, float rating) {
Name = name;
Genre = genre;
Rating = rating;
}
Movie()
{
Name = "";
Genre = "";
Rating = 0;
}
Movie(const Movie& original) {
Name = original.Name;
Genre = original.Genre;
Rating = original.Rating;
}
Movie& operator=(const Movie& original) {
Name = original.Name;
Genre = original.Genre;
Rating = original.Rating;

return *this;
}
};

int main()
{
Movie movie1("The Dark Knight", "Action", 9.5);
Movie movie2("The Lion King", "Animated", 8);

Movie movie3;
Movie movie4(movie1);
movie4 = movie2;
//movie4.operator=(movie2);

Movie movie5 = movie1;
movie5 = movie2;

cin.get();
}

CodeBeauty
Автор

I've been waiting patiently for more coding videos in C++ and it looks like my patience is paying off because lookie here! A new C++ coding video🤗!! Thank You very much Saldina✌️!!

christopherrice
Автор

These technical differences particularly how the memory is allocated and deallocated at the backend you taught through excel they are highly commendable. Dynamic allocation is the tough part for beginners but you explained really well the topic that is hard to find on youtube

sbrhqml
Автор

I am very excited for this video🥰I like your C++ programming tutorials, it helps me very much💝

snehadey
Автор

Hi Iike your teaching method, the way you explained the difference between copy constructor and assingment costructor is very interesting

yawsokpor
Автор

Great teacher, thank you so much, you have an easy explanation way i understand all what you say, much love for you Saldina, i hope i have enough money to pay for your course

ProgrammerSolver
Автор

it was very good
please publish more videos on OOP

amirhosseinmoghimzadeh
Автор

Thanks, SN
Shallow Copy and Deep Copy
Rule of 3 (and the lesser known Law of the Big Two)

joebosah
Автор

We are waiting, and happy that the content picture does not include sub-messages. 😂

fikirgunlugum
Автор

I Like The Way You Insist On Details I'd Like To See The Process Of Writing an object with A String Field In a Binary File : Save Method
Good Continuation... ♥

dabbabimarwen
Автор

26:53 you imagine that the array contains 2 variables so that the explanation could fit better. However, so far in the lecture/code, the counter is set to 0 and the array is empty.

marcusantenor
Автор

It would make sense to also cover move constructor, move assignment operator, to be complete.

nicholaskomsa
Автор

Hi can you make a video in that serie that talks about move constructor?

marcusantenor
Автор

Hi, Sabina, you should probably add "Orthodox Canonical Style" to the video title:) trust me, a lot of people will look for it as soon as it explained quite poor in YouTube

vkatasonov
Автор

I have a question
when you delete the array of Pointers in the assignment operator and make a new srting dinamic array
why the adress changes in Exel explanition althout u don't make
Actors= nullptr?

سراجمصطفىمحمد
Автор

Thank you Saldina....vedio is graat and Your gorgeous.

naderhumood
Автор

This is some high level content with low level programming!



yess I know C++ is a high level programming language ok

whitewolf
Автор

What is difference between constructors and structures

Muhammadaliofficial
Автор

While an assignment-copy is a "Deep Copy", moving is a "Shallow Copy". Shallow copy steals pointers rather than duplicate their contents. here you go:
struct Movie {
std::string title;
int year;

Movie() {
std::cout << "()\n";
}
~Movie() {
std::cout << "~\n";
}

Movie(const Movie& other) : title(other.title), year(other.year) {
std::cout << "copy\n";
}
Movie(Movie&& other) noexcept : title(std::move(other.title)), year(other.year) {
std::cout << "move\n";
}
Movie(const std::string& title, int year) : title(title), year(year) {
std::cout << "ctor\n";
}

Movie& operator=(const Movie& other) {
std::cout << "copy=\n";
if (this != &other) {
title = other.title;
year = other.year;
}
return *this;
}
Movie& operator=(Movie&& other) noexcept {

std::cout << "move=\n";
if (this != &other) {
title = std::move(other.title);
year = other.year;
}
return *this;
}
};

Movie hobbit("Hobbit", 2010);
Movie lionking("Lion King", 1994);
std::array<Movie, 3> movies{ Movie{"The Matrix", 1999}, lionking, std::move(hobbit) };

movies.front() = Movie{ "The Matrix 2", 2003 };
movies.back() = movies.front();

nicholaskomsa
Автор

I do not understand why initializer list has been omitted for the demonstrated constructors. 😢

danielkoziarski