C++運算符重載

轉載於:http://blog.chinaunix.net/uid-24219701-id-2128176.html


/*

 * 運算符重載

 * 運算符重載就是給已有運算符賦予更多的含義,

 * 使它能夠用於特定類的對象,執行特定的功能,

 * 而且使用形式與基本類型數據類型的形式相同

 * 運算符重載實際上就是函數重載

 */

 

/*

 * 使用普通函數完成自定義的加法運算


 */

 

#include

using namespace std;

 

class ClassA

{

private:

    int x, y;

public:

    ClassA(){}

    ClassA(int _x, int _y):x(_x),y(_y){}

    ClassA add(ClassA &a, ClassA &b)

    {

        this->x = a.x + b.x;

        this->y = a.y + b.y;

        return *this;

    }

 

    void display(){cout<<x<<" "<

};

 

int main(void)

{

    ClassA a(1,1),b(2,2);

    ClassA c;

    c.add(a,b);

    c.display();

 

}

 

 

/*

 * 運算符重載

 * 運算符通常是針對類中的私有成員進行操作,因此重載運算符應該能夠訪問類中的私有成員

 * 所以運算符重載一般採用成員函數或友元函數的方式

 */

/*

 * 成員函數重載運算符"+"

 

 */

 

#include

using namespace std;

 

class ClassA

{

private:

    int x, y;

public:

    ClassA(){}

    ClassA(int _x, int _y):x(_x),y(_y){}

    ClassA operator + (ClassA &a)

    {

        ClassA c;

        c.x = this->x + a.x;

        c.y = this->y + a.y;

        return c;

    }

 

    void display(){cout<<x<<" "<

};

/*

 * obj3 = obj1 + obj2;

 * 運算符"+"可以訪問兩個對象:運算符左側對象obj1是將要調用重載運算符函數的對象,

 * 右側對象obj2作爲函數的參數

 */

 

int main(void)

{

    ClassA a(1,1),b(2,2);

    ClassA c;

    c = a + b;

    c.display();

 

}

 

 

/*

 * 友元函數重載運算符"*"

 * Lzy      2011-8-8

 */

 

 

#include

using namespace std;

 

class ClassA

{

private:

    int x, y;

public:

    ClassA(){}

    ClassA(int _x, int _y):x(_x),y(_y){}   

    void display(){cout<<x<<" "<

 

    friend ClassA operator * (ClassA &a1, ClassA &a2);

};

 

ClassA operator * (ClassA &a1, ClassA &a2)

{

    ClassA b;

    b.x = a1.x * a2.x;

    b.y = a1.y * a2.y;

    return b;

}

 

int main(void)

{

    ClassA a(2,2),b(2,2);

    ClassA c;

    c = a * b;

    c.display();

 

}

 

 

/*

 * 成員函數重載運算符前綴"++"

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    int x, y;

public:

    CPoint(int _x=0, int _y=0):x(_x),y(_y){}

    void display(){cout<<x<<" "<

    CPoint operator ++ (void);

};

 

CPoint CPoint::operator ++ (void)

{

    ++x;

    ++y;

    return *this;

}

 

int main(void)

{

    CPoint c(1,1);

    c++;

    c.display();

    return 0;

}

 

 

/*

 * 成員函數重載運算符後綴"++"

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    int x, y;

public:

    CPoint(int _x=0, int _y=0):x(_x),y(_y){}

    void display(){cout<<x<<" "<

    CPoint operator ++ (int);

};

 

CPoint CPoint::operator ++ (int)

{  

    return CPoint(x++, y++);

}

 

int main(void)

{

    CPoint a,c(1,1);

    a=c++;

    a.display();

    c.display();

    return 0;

}

 

 

/*

 * 友元函數重載運算符"++"

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    int x, y;

public:

    CPoint(int _x=0, int _y=0):x(_x),y(_y){}

    void display(){cout<<x<<" "<

    friend CPoint operator ++ (CPoint &c);      //前綴

    friend CPoint operator ++ (CPoint &c, int); //後綴

};

 

CPoint operator ++ (CPoint &c)

{  

    return CPoint(++c.x, ++c.y);

}

 

CPoint operator ++ (CPoint &c,int)

{  

    return CPoint(c.x++, c.y++);

}

 

int main(void)

{

    CPoint a,c(1,1);

    a=c++;

    a.display();

    c.display();

 

    a=++c;

    a.display();

    c.display();

 

    return 0;

}

 

 

/*

 * 重載運算符"="

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    int x, y;

public:

    CPoint(int _x=0, int _y=0):x(_x),y(_y){}

    void display(){cout<<x<<" "<

    CPoint operator = (CPoint c);  

};

 

CPoint CPoint::operator = (CPoint c)

{  

    x= c.x;

    y= c.y;

    return *this;

}

 

int main(void)

{

    CPoint a,c(1,1);

    a=c;

    a.display();   

 

    return 0;

}

 

 

/*

 * 下標運算符[]的重載

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CStrArray

{

private:

    char *ptr;

    int len;

public:

    CStrArray(int i):len(i){ptr = new char[len];}

    ~CStrArray(){delete[] ptr;}

    int getlen(){return len;}

    char & operator[](int);

};

 

char & CStrArray::operator [] (int i)

{

    static char ch = '\0';

 

    if(i<=len && i>=0)

        return *(ptr+i);

    else

    {

        cout<<"數組下標越界"<

        return ch;

    }

}

 

int main(void)

{

    CStrArray str(5);

    cout<<"輸入個字符: ";

   

    for(int i=0; i<5; i++)

        cin>>str[i];

   

 

    for(int i=0; i<5; i++)

        cout<

   

 

    return 0;

}

 

 

 

/*

 * 重載運算符"<<" 和">>"

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    int x, y;

public:

    CPoint(int _x=0, int _y=0):x(_x),y(_y){}

    friend ostream & operator <<(ostream &, const CPoint &);

    friend istream & operator >>(istream &, CPoint &); 

};

 

ostream & operator <<(ostream &output, const CPoint &c)

{

    output<<c.x<<" "<

    return output;

}

 

istream & operator >>(istream &input, CPoint &c)

{

//  input.ignore();         //刪除輸入流中指定數目的字符,默認個數爲

    input>>c.x;

    input>>c.y;

    return input;

}

 

int main(void)

{

    CPoint c;

    cin>>c;

    cout<<c;   

 

    return 0;

}

 

 

 

/*

 * 轉換函數實現類型強制轉換

 * Lzy      2011-8-8

 */

 

#include

using namespace std;

 

class CPoint

{

private:

    float x;

public:

    CPoint(float _x=0):x(_x){}

    operator int(){return (int)x;}

};

 

int main(void)

{

    CPoint c(12.5);

    int y = (int)c;

    cout<

 

    return 0;

}

發佈了111 篇原創文章 · 獲贊 68 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章