C++之運算符重載
阿新 • • 發佈:2017-06-05
cells pro 運算符重載 似的 width uri erl wrap height
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
/* 名稱:C++運算符重載 作者:Michael Joessy 日期:2017-06-05 知識:給原有的運算符賦予新的功能 C++的一大特性就是重載(overload),通過重載可以把功能相似的幾個函數合為一個,使得程序更加簡潔、高效。 在C++中不僅函數可以重載,而且運算符也可以重載。 運算符重載的本質:函數重載 關鍵字:operator 分類:成員函數重載 友元函數重載 */ #include <iostream> #include <string> using namespace std; class Coordinate { //friend Coordinate& operator-(Coordinate& coor); //friend Coordinate operator+(const Coordinate& coor1, const Coordinate& coor2); friend ostream& operator<<(ostream& out, const Coordinate& coor); public: Coordinate( int getX(); int getY(); Coordinate& operator-(); Coordinate& operator++(); //前置++ Coordinate operator++(int); //後置++ Coordinate operator+(const Coordinate& coor); int operator[](int nIndex); protected: private: int m_nX; int m_nY; }; Coordinate::Coordinate( int nX, int nY ) { m_nX = nX; m_nY = nY; } Coordinate& Coordinate::operator-() { m_nX = -m_nX; m_nY = -m_nY; return *this; } Coordinate& Coordinate::operator++() { m_nX++; m_nY++; return *this; } Coordinate Coordinate::operator++( int ) { Coordinate old(*this); m_nX++; m_nY++; return old; } int Coordinate::getX() { return m_nX; } int Coordinate::getY() { return m_nY; } Coordinate Coordinate::operator+( const Coordinate& coor ) { Coordinate tmp(0, 0); tmp.m_nX = this->m_nX + coor.m_nX; tmp.m_nY = this->m_nY + coor.m_nY; return tmp; } int Coordinate::operator[]( int nIndex ) { if (0 == nIndex) { return m_nX; } else if (1 == nIndex) { return m_nY; } else { return -1; } } //Coordinate& Coordinate::operator-( Coordinate& coor ) //{ // coor.m_nX = -coor.m_nX; // coor.m_nY = -coor.m_nY; // return *this; //} //Coordinate Coordinate::operator+( const Coordinate& coor1, const Coordinate& coor2 ) //{ // Coordinate tmp(0, 0); // tmp.m_nX = coor1.m_nX + coor2.m_nX; // tmp.m_nY = coor1.m_nY + coor2.m_nY; // return tmp; //} ostream& operator<<(ostream& out, const Coordinate& coor) { out << coor.m_nX << "," << coor.m_nY; return out; } int main(void) { /************************************************************************/ /* string operator = + << /************************************************************************/ string str1("Michael"); string str2("Joessy"); string str3 = str1 + " " + str2; cout << str3 << endl; /************************************************************************/ /* 一元運算符重載 /************************************************************************/ // -(負號) Coordinate coor1(2, 3); -coor1; //coor1.operator-(); //operator-(coor1); friend cout << coor1.getX() << "," << coor1.getY() << endl; // ++(前置) ++coor1; //coor1.operator++(); cout << coor1.getX() << "," << coor1.getY() << endl; // ++(後置) coor1++; //coor1.operator++(0); cout << coor1.getX() << "," << coor1.getY() << endl; /************************************************************************/ /* 二元運算符重載 /************************************************************************/ Coordinate coorAdd1(1, 3); Coordinate coorAdd2(2, 5); Coordinate coorAddRes3(0, 0); coorAddRes3 = coorAdd1 + coorAdd2; //coorAdd1.operator+(coorAdd2) //operator+(coorAdd1, coorAdd2) friend cout << coorAddRes3.getX() << "," << coorAddRes3.getY() << endl; /************************************************************************/ /* 輸出運算符重載 /* 必須使用友元運算符重載 /************************************************************************/ Coordinate coorTest(7, 8); cout << coorTest << endl; //operator<<(cout, coorTest) /************************************************************************/ /* []索引運算符重載 /* 必須使用成員運算符重載 /************************************************************************/ Coordinate coorIndex(3, 5); cout << coorIndex[0] << endl; //coorIndex.operator[](0) cout << coorIndex[1] << endl; //coorIndex.operator[](1) cin.get(); return 0; } |
C++之運算符重載