C++ program to check whether a number is even or odd by ! operator overloading
Write a Write a C++ program to check whether a number is even or odd by ! operator overloading.
In this examples, i am showing you that how to overload the ! operator for checking the even and odd number. The Formula as you know is;
if number%2==0 then number is even, other the number is odd number.
After cin on line 26, the number moves to the actual parameter on line 27. Then moves to the formal parameter a on line 6. This 6 then moves from line 6 to line 7 and so on.
!obj on line 28 is not possible without operator overloading. So we are overloading the ! operator on line number 14 to line number 20.
After operator overloading the compiler is able to understand that how ! can work on the object of the class T4Tutorials.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<iostream> using namespace std; class T4Tutorials{ int a; public: void SET_Number(int a){ this->a=a; } int GET_Number(){ return a; } }; T4Tutorials operator !( T4Tutorials obj){ if(obj.GET_Number()
%2==0){ cout<<"It is a even number."<<endl; } else{cout<<"It is a odd number "<<endl; } } int main(){ T4Tutorials obj; int num; cout<<"Please Enter a number :"<<endl; cin>>num; obj.SET_Number(num); !obj; } |
Output
Please Enter a number :
9
It is a even number.
FAQ
even odd program in c++ using if ele by ! operator overloading.
display odd and even numbers in c++ by ! operator overloading.
c++ program to find even and odd numbers using function by ! operator overloading.
odd number program in c++ using Classes by ! operator overloading.
even number program in c++ using for loop by ! operator overloading.