Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <math.h>
#include <iostream>
using namespace std;
class Integer
{
public://-- Creation
//Intialize an Integer object with value 'v'.
Integer (int v = 0) :
_value (v)
{
}
//Intialize an Integer object from 'other'.
Integer (const Integer& other) :
_value (other.value ())
{
}
public://-- Access
//The value hold by this Integer object.
int value () const
{
return _value;
}
public://-- Element change
//Assign 'other' to this Integer object.
Integer& operator= (const Integer& other)
{
_value = other.value ();
return *this;
}
public://-- Basic operations
//result = this ^ exponent.
Integer operator^ (const Integer& exponent) const
{
return Integer (pow (_value, exponent.value ()));
}
//result = this ^ exponent.
Integer operator^ (int exponent) const
{
return Integer (pow (_value, exponent));
}
//result = this + other.
Integer operator+ (const Integer& other) const
{
return Integer (_value + other.value ());
}
//TODO: define and implement other operations here.
private://-- Attributes
int _value;
};
//Print Integer object 'v' on 'stream'.
ostream& operator<< (ostream& stream, const Integer& v)
{
return stream << v.value ();
}
//result = value + other(make Integer::operator+ symetrical).
Integer operator+ (int value, const Integer& other)
{
return Integer (value + other.value ());
}
//Test it
int main ()
{
Integer wrong = 2 + Integer (3) ^ 2;//precedence problem
Integer right = 2 + (Integer (3) ^ 2);//solved with paranthesis
cout << "wrong 2 + 3^2 = " << wrong << endl;
cout << "right 2 + 3^2 = " << right << endl;
return 0;
}