Chinaunix首页 | 论坛 | 博客
  • 博客访问: 854916
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: C/C++

2011-11-24 12:21:45

与类相关的重载操作符的两种方法:
1. 非成员函数

/* strops.cpp
* 重载操作符
*/
#include
#include
#include

using namespace std;

class TStrOp {
private:
    string value;
public:
    TStrOp():value("0") { }
    TStrOp(string arg):value(arg) { }
    long GetValue() { return atol (value.c_str()); }
    friend long operator + (TStrOp a, TStrOp b);
    friend long operator - (TStrOp a, TStrOp b);
};

int main()
{
    TStrOp a("1234");
    TStrOp b("4321");

    cout << "Value of a == " << a.GetValue() << endl;
    cout << "Value of b == " << b.GetValue() << endl;
    cout << "a + b + 6 == " << (a + b + 6) << endl;
    cout << "a - b + 10 == " << (a - b + 10) << endl;

    return 0;
}

//由于函数是朋友无需加类名(非类TStrOp的成员函数)
//在不访问私有数据情况下不用设为友元函数

long operator + (TStrOp a, TStrOp b)
{
    return ( atol(a.value.c_str()) + atol(b.value.c_str()) );
}

long operator- (TStrOp a, TStrOp b)
{
    return ( atol(a.value.c_str()) - atol(b.value.c_str()) );
}

2. 成员函数

/* strops2.cpp
* 重载类成员函数而不是使用朋友
*/
#include
#include
#include

using namespace std;

class TStrOp {
private:
    string value;
public:
    TStrOp():value("0") { }
    TStrOp(string arg):value(arg) { }
    long GetValue() { return atol (value.c_str()); }
    long operator + (TStrOp b);
    long operator - (TStrOp b);
};

int main()
{
    TStrOp a("1234");
    TStrOp b("4321");

    cout << "Value of a == " << a.GetValue() << endl;
    cout << "Value of b == " << b.GetValue() << endl;
    cout << "a + b + 6 == " << (a + b + 6) << endl;
    cout << "a - b + 10 == " << (a - b + 10) << endl;

    return 0;
}

//重载类TStrOp 的成员函数
//重载的操作符函数已经是类的成员,可以访问
//保护成员,并接收隐藏的this指针,该指针指向
//调用其函数的对象,因此只需一个参数。但它
//们仍然是二元操作符,因为仍然接收两个参数

long TStrOp::operator + ( TStrOp b)
{
    return ( atol(value.c_str()) + atol(b.value.c_str()) );
}

long TStrOp::operator- (TStrOp b)
{
    return ( atol(value.c_str()) - atol(b.value.c_str()) );
}

结果都相同:



/* strops3.cpp
*通过重载类型转换字符,简化操作符重载
*/
#include
#include
#include

using namespace std;

class TStrOp{
private:
    string value;
public:
    TStrOp():value("0") { }
    TStrOp(string arg):value(arg) { }
    long GetValue() { return (long)(* this); }
    long operator + (TStrOp b);
    long operator - (TStrOp b);
    long operator - ();
    operator long () { return atol(value.c_str()); }  //重载long型转换符,可换成其它类型
};

int main()
{
   
    TStrOp a("1234");
    TStrOp b("4321");

    cout << "Value of a == " << a.GetValue() << endl;
    cout << "Value of b == " << b.GetValue() << endl;
    cout << "a + b + 6 == " << (a + b + 6) << endl;
    cout << "a - b + 10 == " << (a - b + 10) << endl;

    TStrOp myvalue("9876");
    long x = myvalue;
    cout << "x + b - 9 == " << (x + b - 9) << endl;
   
    return 0;
}

long TStrOp::operator + (TStrOp b)
{
    return (long)*this + (long)b;     //内部调用类型重载操作符,强制转换
}

long TStrOp::operator -(TStrOp b)
{
    return (long)*this - (long)b;
}

long TStrOp::operator -()
{
    return - (long)(*this);
}


重载操作符一些定义:
int operator ++() { return ++x; }      //prefix ++
int operator ++(int) { return x++; } //postfix ++
int operator --() { return --x; }      //prefix --
int operator --(int) { return x--; } //postfix --

friend ostream &
        operator << (ostream & os, const TCoordinate &t);  //重载 <<  输出流操作符
friend istream &
        operator >> (istream & is, const TCoordinate &t);  //重载 >>  输入流操作符
int &operator[](unsigned i);  //重载[]操作符

/* ssop.cpp */
#include

using namespace std;

class TError { };
class PseudoArray {
private:
    int value0;
    int value1;
    int value2;
    int value3;
public:
    PseudoArray(int v0, int v1, int v2, int v3):
        value0(v0), value1(v1), value2(v2), value3(v3) { }
    int &operator[](unsigned i) throw (TError);
};

int main()
{
    PseudoArray pa(10, 20, 30, 40);

    try {
        for (int i = 0; i <= 3; i++)
            cout << "pa[" << i << "] == " << pa[i] << endl;
        pa[2] = 123;
        cout << "pa[2] == " << pa[2] << endl;
        }
    catch (TError) {
        cout << "* * * Error detected" << endl;
        }

    return 0;
}

int &PseudoArray::operator[](unsigned i) throw (TError)
{
    switch(i) {
            case 0: return value0;
            case 1: return value1;
            case 2: return value2;
            case 3: return value3;
            default: throw TError();
        }
}



/* pointout.cpp */
#include

using namespace std;

class TCoordinate {
private:
    int tc_x, tc_y;
public:
    TCoordinate ():tc_x(0), tc_y(0) { }
    TCoordinate(int x, int y):tc_x(x), tc_y(y) { }
    void Setxy(int x, int y);
    int Getx()const;
    int Gety()const;
    friend ostream&
        operator << (ostream & os, const TCoordinate &t);
    friend stream &
        operator >> (istream & is, const TCoordinate &t);
};

int main()
{
    TCoordinate p(10, 20);
    cout << p << endl;

    return 0;
}

ostream &
    operator << (ostream & os, const TCoordinate &t)

{
    os << "x == " << t.tc_x << ";y == " << t.tc_y;
    return os;
}

istream &
        operator >> (istream & is, const TCoordinate &t);
{
    is >> t.tc_x >> t.tc_y;
    return is;
}

阅读(827) | 评论(0) | 转发(0) |
0

上一篇:朋友类及函数

下一篇:重载new操作符

给主人留下些什么吧!~~