C++之重写运算符练习
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class MyString{ 6 private: 7 char* cp; 8 public: 9 MyString(char*); 10 ~MyString(); 11 MyString(MyString&ms); 12 MyString& operator +(MyString&); 13 MyString&operator=(MyString&); 14 MyString&operator+=(MyString&); 15 char operator[](int i); 16 char* getP(); 17 18 }; 19 MyString::MyString(char*cp=NULL):cp(cp){ 20 } 21 MyString::~MyString(){} 22 MyString::MyString(MyString&ms){//深复制 23 int len = strlen(ms.cp);//先确定原来字符串长度 24 char*tempcp = new char[len+1];//开辟足够的空间 25 strcpy(tempcp,ms.cp);//复制相同的内容 26 cp = tempcp;//初始化对象的指针 27 } 28 29 MyString& MyString:: operator +(MyString&ms){ 30 int len = strlen(cp) + strlen(ms.cp); 31 char *tempcp = new char[len+1]; 32 strcpy(tempcp,cp); 33 strcat(tempcp,ms.cp); 34 35 return *(new MyString(tempcp));//不改变两边对象的内容,返回一个新的对象 36 } 37 MyString&MyString::operator=(MyString&ms){ 38 if (cp == NULL || strlen(cp) < strlen(ms.cp)){ 39 if (cp){ 40 delete[] cp;//释放原来的小空间 41 } 42 cp = new char[(strlen(ms.cp) + 1)];//开辟足够的空间 43 } 44 cp=strcpy(cp, ms.cp);//复制内容 45 return *this; 46 } 47 MyString& MyString::operator+=(MyString&ms){ 48 49 char*tempcp = cp;//暂存cp,因为要重新开辟空间//如果直接使strcat空间不够会出错 50 cp = new char[strlen(cp)+strlen(ms.cp)+1]; 51 strcpy(cp,tempcp); 52 strcat(cp,ms.cp); 53 54 return *this; 55 } 56 char MyString::operator[](int i){ 57 return cp[i]; 58 } 59 char*MyString::getP(){//用于主函数中查看对象内容 60 return cp; 61 } 62 int main(){ 63 MyString ms1("hello"),ms2("world"); 64 cout << "+result:" << (ms1 + ms2).getP() << endl; 65 ms1 += ms2; 66 cout << "+=result:" << ms1.getP() << endl; 67 ms1 = ms2; 68 cout << "=result:" << ms1.getP() << endl; 69 cout << "[]result:" << ms1[0] << endl; 70 71 }
1 #include<iostream> 2 3 using namespace std; 4 5 class Point{ 6 public: 7 Point& operator ++(); 8 Point operator ++(int); 9 Point&operator --(); 10 Point operator --(int); 11 Point(){ _x = _y = 0; } 12 int x(){ return _x; } 13 int y(){ return _y; } 14 private: 15 int _x, _y; 16 }; 17 Point& Point::operator ++(){ 18 _x++; 19 _y++; 20 return *this; 21 } 22 Point Point::operator ++(int){ 23 Point temp = *this; 24 ++*this; 25 return temp; 26 } 27 28 Point& Point::operator --(){ 29 _x--; 30 _y--; 31 return *this; 32 } 33 Point Point::operator --(int){ 34 Point temp = *this; 35 --*this; 36 return temp; 37 } 38 39 void main(){ 40 Point A; 41 cout << "A的值为" << A.x() << "," << A.y() << endl; 42 A++; 43 cout << "A的值为" << A.x() << "," << A.y() << endl; 44 ++A; 45 cout << "A的值为" << A.x() << "," << A.y() << endl; 46 A--; 47 cout << "A的值为" << A.x() << "," << A.y() << endl; 48 --A; 49 cout << "A的值为" << A.x() << "," << A.y() << endl; 50 51 52 }
//使用全局函数重载
1 #include<iostream> 2 using namespace std; 3 4 class Point{ 5 private: 6 int x; 7 int y; 8 public: 9 Point(int x, int y):x(x),y(y){} 10 friend Point& operator+(Point&one,Point& two); 11 int getX(){ return x; } 12 int getY(){ return y; } 13 }; 14 15 Point& operator+(Point&one, Point& two){ 16 int x = one.x + two.x; 17 int y = one.y + two.y; 18 return *new Point(x,y); 19 } 20 21 int main(){ 22 Point p1(1,1),p2(2,2); 23 Point& result = p1 + p2; 24 cout << result.getX() << ":" << result.getY(); 25 return 0; 26 }