Huge Integers
Design a class that can handle arbitrarily large integers (using strings) that otherwise do not fit in a primitive type. A string will be treated as an integer if it contains all numeric type characters only. A sign indicating positive or negative integer may precede the string e.g. “-123456789009876543210”. Provide support for basic arithmetic and relational operations (using operator overloading) that include:
addition: + and ++ (both pre and post increment versions)
subtraction: - (binary as well as unary) and -- (both pre and post increment versions)
multiplication
division
comparison operators: ==, <, >
#include<iostream>
using namespace std;
class OperatorOverload {
private:
int real, foo;
public:
OperatorOverload(int r = 0, int i =0) {real = r; foo = i;}
OperatorOverload operator + (OperatorOverload const &obj) {
OperatorOverload bar;
bar.real = real + obj.real;
bar.foo = foo + obj.foo;
return bar;
}
OperatorOverload operator * (OperatorOverload const &obj) {
OperatorOverload bar;
bar.real = real * obj.real;
bar.foo = foo * obj.foo;
return bar;
}
OperatorOverload operator / (OperatorOverload const &obj) {
OperatorOverload bar;
bar.real = real / obj.real;
bar.foo = foo / obj.foo;
return bar;
}
OperatorOverload operator - (OperatorOverload const &obj) {
OperatorOverload bar;
bar.real = real - obj.real;
bar.foo = foo - obj.foo;
return bar;
}
};
Comments
Leave a comment