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.
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:
According to Tutorialspoint
Previous lesson: Load the operator ++ and - in C ++
Next lesson: Load the operator stack to call the function () in C ++
Update 25 May 2019
You should read it
- Operator in PHP
- Operator overloading in C #
- Stack operator in Python
- Operator in C ++
- ! = and! == What is the difference in JavaScript?
- Operator in JavaScript
- Operator in programming C
- Overload binary operators in C ++
- Load the Input / Output operator stack in C ++
- Load operator ++ and - in C ++
- Boolean operators AND and OR in SQL
- C Shell operators
Maybe you are interested
5 simple ways to help you remember everything in life 20 things to give up to be happier before the new year Elon Musk released 8 photos of the first human journey to Mars in the future Watch Elon Musk's presentation on the plan to conquer Mars black hole, white hole, deep hole Detecting the tremors of the 'twin brothers' of the solar system, the key to deciphering early life