分类: C/C++
2011-10-14 17:15:36
#include
using namespace std;
class INT
{
friend ostream & operator << (ostream &, const INT &);
public:
INT(){ val = 0; }
INT(const INT & c){ val = c.val; }
INT(const int & t){ val = t; }
INT operator + (const INT &);
INT & operator ++ (); //prefix ++
INT & operator -- (); //prefix --
const INT operator ++(int); //postfix ++
const INT operator --(int); //postfix --
double getVal() const { return val; }
void setVal(const int &d) { val = d; };
private:
int val;
};
ostream & operator << (ostream & output, const INT & c)
{
output<
return output;
}
INT INT :: operator + (const INT & c)
{
return INT(c.val + this->val);
}
INT & INT :: operator ++ ()
{
++(this->val);
return *this;
}
const INT INT :: operator ++(int)
{
INT temp(*this);
++(this->val);
return temp;
}
INT & INT :: operator -- ()
{
--(this->val);
return *this;
}
const INT INT :: operator --(int)
{
INT temp(*this);
--(this->val);
return temp;
}
int main(void)
{
INT c1;
cout<
c1.setVal(2);
INT c2;
cout<
c2.setVal(3);
cout<
c1++;
cout<
++c1;
cout<
return 0;
}