the book im reading has an exercise to make a class Int and overload the basic operators. there is 2 things i dont understand. it asks for bounds checking, but i dont know why the answer in the back of the book needs to convert it to long double. the other thing i dont get is 'operator int()'. when does this come in play in the class? its a conversion from Int to type int, i know that much. but when is this operator called. thanks in advance for responses
Code://////////////////////////class Int///////////////////////// class Int { private: int i; public: Int(): i(0) //set default value (constructor) { } Int(int integer): i(integer) //1-arg constructor { } void put_int() //display int { cout << i << endl; } void get_int() //get int { cin >> i; } operator int() //conversion operator from Int to int { return i; } Int operator + (Int i2) { return limit_check(long double(i) + long double(i2)); } Int operator - (Int i2) { return limit_check(long double(i) - long double(i2)); } Int operator * (Int i2) { return limit_check(long double(i) * long double(i2)); } Int operator / (Int i2) { return limit_check(long double(i) / long double(i2)); } Int limit_check(long double num) { if(num > 2147482648.0L || num < -2147482648.0L) { cout << "\nOverflow error: Exiting." << endl; exit(1); } return Int(int(num)); } }; /////////////////////////////////////////////////////////////