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 << "Khoang cach bang m la: " << met << endl ; cout << "Khoang cach bang cm la: " << centimet << endl ; cout << "nn================================nn" << endl ; } // nap chong toan tu (-) KhoangCach operator - () { met = - met ; centimet = - centimet ; return KhoangCach ( met , centimet ); } }; int main () { KhoangCach K1 ( 20 , 6 ), K2 (- 6 , 15 ); - K1 ; // ap dung su phu dinh K1 . hienthiKC (); // hien thi K1 - K2 ; // ap dung su phu dinh K2 . hienthiKC (); // hien thi K2 return 0 ; }
Compiling and running the above C ++ program will produce the following results:
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 ++
You should read it
May be interested
- How to fix CPU overload error 99%, 100% simple, effective!how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!
- Overload relational operator in C ++there are many different relational operators supported by c ++, such as: (, =, ==, ...) that can be used to compare the data types available in c ++.
- Fix CPU and 99% CPU overload errors on Windowsif your computer's fan is constantly running at high speed, your cpu is overloaded. this means your computer will slow down. that is really annoying if you don't fix it.
- How to fix OneDrive error makes CPU overload on Windows 10the error of opening onedrive causes cpu overload to occur frequently on windows 10 creators, causing the device to be in a state of 'stall' continuously.
- How to Get the Best Seat in a Movie Theaternot all seats in a movie theater were created equal. it's true! some movie theater seats are better than others. it should be easy to get the best movie theater seat if you put some forethought into how you buy your tickets and choose a...
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...
- Stack operator in Pythonyou can change the meaning of the operator in python depending on the operand used and we call that operator overloading. quantrimang will learn more about this content through the article. invites you to read the track.
- OpenAI Announces Operator, an AI Agent That Automatically Performs Tasks on User's Behalfthe company behind chatgpt is unveiling a new way to use ai. openai has just announced operator, an ai agent that can automatically perform tasks for you.
- DeepSeek's server burned down due to overload, forced to tighten beltthe popular ai startup deepseek is facing a difficult problem with server performance.