Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<class T> class frac
- {
- T num , den;
- public:
- frac(): num(0), den(0) {}
- explicit frac(int n, int d ): num(n), den(d) {}
- operator int();
- operator bool();
- operator char();
- };
- template<class T> frac<T>::operator int()
- {
- cout << "\nOPERATOR(INT)\n";
- return (int)(num/den);
- }
- template<class T> frac<T>::operator bool()
- {
- cout << "\nOPERATOR(BOOL)\n";
- return (den != 0);
- }
- template<class T> frac<T>::operator char()
- {
- cout << "\nOPERATOR(CHAR)\n";
- if( (int)num/den > 255 || (int)num/den < 1 ) return (char)48;
- return (char)num/den;
- }
- int main()
- {
- float a = 7 , b = 3;
- frac<float> f1(a,b);
- cout << (int)f1 << endl;
- cout << (bool)f1 << endl;
- cout << (char)f1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement