How to Create Custom Assignment Operator in C++? Last Updated : 19 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, operator overloading allows us to redefine the behavior of an operator for a class. In this article, we will learn how to create a custom assignment operator for a class in C++. Creating Custom Assignment (=) Operator in C++To create a custom assignment operator, we need to define an overloaded assignment operator function in our class. The overloaded assignment operator function should have the following signature: ClassName& operator= (const ClassName& other);Here, ClassName is the name of the class for which we are defining the custom assignment operator and other represents the object being assigned from. C++ Program to Write Custom Assignment Operator for a ClassThe below example demonstrates how we can write our own assignment operator for our class in C++. C++ // C++ program to create custom assignment operator #include <iostream> using namespace std; // Class to represent a Date class Date { public: // Member variables for day, month, and year int day, month, year; // Constructor to initialize the Date object Date(int d, int m, int y) : day(d) , month(m) , year(y) { } // Custom assignment operator Date& operator=(const Date& other) { // Check for self-assignment if (this != &other) { // Copy data from other to this object day = other.day; month = other.month; year = other.year; } return *this; // Return a reference to this object } // Method to print the date void print() { cout << day << "/" << month << "/" << year << endl; } }; int main() { // Create two Date objects Date d1(1, 1, 2000); Date d2(2, 2, 2002); // Print the initial dates cout << "Initial Dates: " << endl; d1.print(); d2.print(); // Assign the values of d2 to d1 using the custom // assignment operator d1 = d2; // Print the dates after assignment cout << "Dates after assignment of Date2 to Date1: " << endl; d1.print(); d2.print(); return 0; } OutputInitial Dates: 1/1/2000 2/2/2002 Dates after assignment of Date2 to Date1: 2/2/2002 2/2/2002 The above assignment operator syntax is for copy assignment operator. It is generally recommended to also define the move assignment operator along with it. To know more, refer to this article - C++ Assignment Operator Overloading Comment More infoAdvertise with us Next Article How to Create Custom Assignment Operator in C++? H hadaa914 Follow Improve Article Tags : C++ Programs C++ cpp-operator CPP Examples Practice Tags : CPPcpp-operator Similar Reads How to Create Custom Memory Allocator in C++? In C++ containers, memory allocation is generally managed by allocators through new and delete operators or malloc() and free() functions but for custom memory management we can use the custom allocators. In this article, we will learn how can one create a custom memory allocator in C++. Custom Memo 2 min read How to Implement Move Assignment Operator in C++? In C+, the move assignment operator is used to transfer the ownership of the resources from one object to another when the object is assigned some rvalue references or using std::move. It allows efficient memory manipulation by avoiding unnecessary copies of the objects. In this article, we will lea 3 min read How to Create std::vector of Custom Classes or Structs in C++? In C++, std::vectors are dynamic arrays that can resize themselves during the runtime, and classes or structs allow the users to create custom data types of their choice. There might be many situations when we want to store a custom data type into a vector. In this article, we will learn how to crea 3 min read How to Use Custom Comparator with Set in C++? In C++, sets are associative containers that store unique elements in some sorted order. By default, set store data in increasing order but we can change this using a custom comparator. In this article, we will learn, how to declare a set with a custom comparator in C++ STL. Example Input: Data = {1 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read Like