Load the stack of assignment operators in C ++

You can overload the assignment operator (=) as you can with other operators in C ++ and it can be used to create an object like copying constructors.

The following example illustrates how to overload the assignment operator in C ++:

 #include using namespace std ; class KhoangCach { private : int met ; int centimet ; public : // phan khai bao cac constructor can thiet KhoangCach (){ met = 0 ; centimet = 0 ; } KhoangCach ( int m , int c ){ met = m ; centimet = c ; } void operator =( const KhoangCach & K ) { met = K . met ; centimet = K . centimet ; } // phuong thuc de hien thi khoang cach void hienthiKC () { cout << "nDo dai bang m la: " << met << "nVa do dai bang cm la: " << centimet << endl ; } }; int main () { KhoangCach K1 ( 23 , 16 ), K2 ( 15 , 46 ); cout << "Khoang cach dau tien: " ; K1 . hienthiKC (); cout << "n-------------------n" ; cout << "Khoang cach thu hai:" ; K2 . hienthiKC (); // su dung toan tu gan K1 = K2 ; cout << "n-------------------n" ; cout << "Khoang cach dau tien: " ; K1 . hienthiKC (); return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Load the stack of assignment operators in C ++ Picture 1

According to Tutorialspoint

Previous lesson: Load the operator ++ and - in C ++

Next lesson: Load the operator stack to call the function () in C ++

4 ★ | 1 Vote

May be interested

  • Load operator ++ and - in C ++Load operator ++ and - in C ++
    the incremental operators (++) and the reduced operator (-) are the two important unary operators available in c ++.
  • Indexer in C #Indexer in C #
    indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
  • C Shell operatorsC Shell operators
    this tutorial lists all the operators available in c shell. here, most operators are similar to those we have in the c programming language.
  • How to Implement a Stack Data Structure in C++How to Implement a Stack Data Structure in C++
    a stack is a basic data structure that is commonly used throughout computer science. for example, a stack is utilized by your web browser to remember the last few pages you were on. developers and programmers should be very familiar with...
  • Basic Shell operatorsBasic Shell operators
    there are many operators supported by each shell. our tutorial is based on the default shell (bourne) so we are discussing bourne shell operators in this chapter.
  • Korn Shell OperatorKorn Shell Operator
    this tutorial lists all the operators available in the korn shell. here most of these operators are similar to what we have in the c program language.
  • Overload subscript operator [] in C ++Overload subscript operator [] in C ++
    the subscript operator [] in c ++ is often used to access array elements. this operator can be overloaded to enhance existing functionality in arrays in c ++.
  • Comparison operators in SQL ServerComparison operators in SQL Server
    this article will show the comparison operators that are used to check for equilibrium along with more advanced operators in sql server.
  • How to Become a Full Stack ProgrammerHow to Become a Full Stack Programmer
    a full-stack programmer is a versatile developer who has experience and understanding of front-end and back-end software and hardware. full-stack developers also have a firm grasp of a multitude of programming languages, making them agile...
  • How to load Au Mix, load Au Diamond mixHow to load Au Mix, load Au Diamond mix
    here's how to top up the au mix game, including how to top up the diamonds and top up the au mix packages.