Created
July 27, 2016 15:35
-
-
Save ravikiran0606/ba3f229fd3b722d535fc35ebe450050b to your computer and use it in GitHub Desktop.
C++ program to implement a digit class and make use of operator overloading:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class digit{ | |
int val; | |
public: | |
digit(); | |
digit(int num); | |
friend istream& operator >>(istream& inp,digit &a){ | |
inp>>a.val; | |
return inp; | |
} | |
friend ostream& operator <<(ostream& out,digit &a){ | |
out<<a.val; | |
return out; | |
} | |
digit operator ++(); // for prefix usage | |
digit operator ++(int); // for postfix usage | |
digit operator --(); // for prefix usage | |
digit operator --(int); // for postfix usage | |
}; | |
digit::digit(){ | |
val=0; | |
} | |
digit::digit(int num){ | |
val=val; | |
} | |
digit digit::operator++(){ | |
val++; | |
if(val==10)val=0; | |
digit t(val); | |
return t; | |
} | |
digit digit::operator++(int){ | |
digit t(val); | |
val++; | |
if(val==10)val=0; | |
return t; | |
} | |
digit digit::operator--(){ | |
val--; | |
if(val==-1)val=9; | |
digit t(val); | |
return t; | |
} | |
digit digit::operator--(int){ | |
digit t(val); | |
val--; | |
if(val==-1)val=9; | |
return t; | |
} | |
int main() | |
{ | |
cout<<"Choice : 1)Pre increment 2) Post increment 3) Pre decrement 4) Post decrement 5) Exit"; | |
digit d; | |
int ch; | |
cout<<"\nEnter any digit.."; | |
cin>>d; | |
while(1){ | |
cout<<"\nEnter your choice "; | |
cin>>ch; | |
if(ch==1){ | |
++d; | |
cout<<"The new value is "<<d; | |
} | |
else if(ch==2){ | |
d++; | |
cout<<"The new value is "<<d; | |
} | |
else if(ch==3){ | |
--d; | |
cout<<"The new value is "<<d; | |
} | |
else if(ch==4){ | |
d--; | |
cout<<"The new value is "<<d; | |
} | |
else{ | |
break; | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment