Site icon Learn C++

What Is An Assignment Operator In C++?

What Is An Assignment Operator In C++

One of the most commonly used features of C++ software, in common with many programming languages, are assignment operators. These take the form of copy assignment and move assignment operators. In C++, a copy assignment operator can be used with “operator=” to create a new object from an existing one. In this post, we explain assignment operators with copy assignment operator examples in C++.

If you are looking for more information on assignment operators in C or C++, the following post discusses them in depth.

What are classes and objects in C++?

Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects, and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.

[crayon-6766cff0a4a33925736677/]

What is a copy assignment operator in C++?

The Copy Assignment Operator, in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator which is defaulted,

Syntax (Since C++11).

[crayon-6766cff0a4a3b411641204/]

Here is an example in a class.

[crayon-6766cff0a4a3d032539966/]

Is there a simple example of using the assignment operator in C++?

The forced copy assignment operator is default in any class declarations. This means you don’t need to declare it as above. Let’s give examples without using it.

Let’s give a simple C++ example of a copy assignment operator with default option. Here is a simple class:

[crayon-6766cff0a4a45986781916/]

Because this is default in any class declaration, and it is automatically declared. This class is same as below.

[crayon-6766cff0a4a47745284116/]

And here is how you can use this “=” copy assignment operator with both class examples above.

[crayon-6766cff0a4a49028972481/]

Is there a full example of how to use the assignment operator in C++?

Here is an example with a copy assignment operator in a class,

[crayon-6766cff0a4a4a555032303/]

Here is the output.

[crayon-6766cff0a4a4c799613189/]

As you see, in Modern C++, we can specialize the “=” operator on what to copy or not, with the copy assignment operator.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version