Overload the one-seat operator in C ++

The unary operator in C ++ operates on a single operand and here are some examples of single-operator operators:

Overloading the increment operator (++) and decreasing operator (-) in C ++.

Operator unary (-) in C ++

Negative logical operator (!) In C ++

The unary operator operates on the object to which they are called, and usually this operator appears to the left of the object, such as :! Obj, -obj, and ++ obj , but sometimes they can be used as is suffix like obj ++ or obj-- .

The following example explains how the (-) operator can be overloaded with the use of the prefix as well as the suffix.

 #include using namespace std ; class KhoangCach { private : int met ; int centimet ; public : // khai bao cac constructor KhoangCach (){ met = 0 ; centimet = 0 ; } KhoangCach ( int m , int c ){ met = m ; centimet = c ; } // phuong thuc de hien thi khoang cach void hienthiKC () { cout 

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

Picture 1 of Overload the one-seat operator in C ++

Hopefully the above example will help you understand this concept and you can apply for operator operator (!) In C ++.

According to Tutorialspoint

Previous post: Load operator and Load overlap in C ++

Next lesson: Overload binary operator in C ++

ncG1vNJzZmismaXArq3KnmWcp51kvLex0aWmmpxdqbWmec6nnKydkal6sLzEq5itp6Jitq95wmY%3D