C++基础12-类和对象之操作符重载-string练习
生活随笔
收集整理的這篇文章主要介紹了
C++基础12-类和对象之操作符重载-string练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
總結:
1、等號操作符重載注意事項:
?? ?(1)防止自身賦值
?? ?(2)先將自身的額外開辟的空間回收掉
?? ?(3)執行深拷貝
2、注意函數的返回引用或者元素:如果需要連續使用 返回元素
3、如果一個函數傳遞const修飾的對象,那么此對象調用的成員函數也應為const類型
4、重載小括號 稱這種對象為仿函數
5、字符串>>重載時防止初始化的字符串長度為空
MyString.h:
?
MyString.cpp:
#include "MyString.h"MyString::MyString() {this->len = 0;this->str = NULL; //表示零值或空值//this->str = ""; //表示空字符串 里面有\0 }MyString::MyString(int len) {this->len = len;this->str = new char[len]; }MyString::MyString(const char * str) {/*meint len = strlen(str);this->len = len + 1;this->str = new char[len + 1];for (int i = 0; i < len; i++){this->str[i] = str[i];}*/if (str == NULL) {this->len = 0;this->str = new char[0+1]; strcpy(this->str, "");}else {int len = strlen(str);this->len = len;this->str = new char[len + 1];strcpy(this->str, str);} }MyString::MyString(const MyString & another) {//拷貝構造函數不需要回收垃圾 初始化時被調用this->len = another.len;this->str = new char[this->len + 1];strcpy(this->str, another.str);}MyString::~MyString() {if (this->str != NULL) {cout << this->str << "執行了析構函數" << endl;delete [] this->str;this->str = NULL;this->len = 0;} }char& MyString::operator[](int index) {// TODO: 在此處插入 return 語句return this->str[index]; }bool MyString::operator==(const MyString & mystring2) {if (this->len != mystring2.len) {return false;}for (int i = 0; i < this->len; i++){if (this->str[i] != mystring2.str[i]) {return false;}}return true; }MyString& MyString::operator=(const MyString & another) {if (this == &another) {return *this;}// TODO: 在此處插入 return 語句if (this->str != NULL) {delete this->str;this->str = NULL;this->len = 0;}this->len = another.len;this->str = new char[this->len + 1];strcpy(this->str, another.str);return *this;}ostream& operator<<(ostream& os, MyString &mystring) {cout << "len:";os << mystring.len;cout << "str:";os<< mystring.str ;return os; }istream& operator>>(istream &is, MyString &mystring) {// TODO: 在此處插入 return 語句//先釋放mystring內存//char *str=NULL;cin>>str; 程序崩潰//1、將mystring之前的字符串釋放掉if (mystring.str != NULL) {delete mystring.str;mystring.str = NULL;mystring.len = 0;}//2、通過cin添加新的字符串char temp_str[4096] = { 0 };cin >> temp_str;int len = strlen(temp_str);mystring.str = new char[len + 1];strcpy(mystring.str, temp_str);mystring.len = len;return is; }MyString MyString::operator+(MyString & another) {// TODO: 在此處插入 return 語句MyString temp;int len= this->len + another.len;temp.len = len;temp.str = new char[len + 1];memset(temp.str, 0, len + 1); //清空 strcat需要memset strcpy不需要strcat(temp.str, this->str);strcat(temp.str, another.str);//temp.str = this->str + another.str;return temp; } bool operator!=(MyString &mystring1,MyString &mytring2) {return !(mystring1 == mytring2); }main.cpp:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include"MyString.h" using namespace std;void test01() {string s1("123");//equal tostring s2 = "123";string s3(s1); //重載拷貝構造string s4; s4 = s1; //重載=s4 = s1 + s2; //重載+s2[1] = 'x'; //重載[]cout << s2 << endl; //重載<< } /* 1x3 */void test02() {MyString s1;MyString s2("123");//MyString s3(NULL);MyString s4(s2);//cout <<"s1"<< s1 << endl; 截斷后面輸出cout << "s2" << s2 << endl;cout << "s4" << s4 << endl;s2[1] = 'x';cout << "s2" << s2 << endl;cout << "s4" << s4 << endl;MyString s5;s5 = s2;cout << "s5" << s5 << endl; } /* s2len:3str:123 s4len:3str:123 s2len:3str:1x3 s4len:3str:123 s5len:3str:1x3 1x3執行了析構函數 123執行了析構函數 1x3執行了析構函數 */ void test03() {MyString s1("abc");cin >> s1;cout << s1 << endl; } /* ddd len:3str:ddd ddd執行了析構函數 */ void test04() { #if 0string s1("123");string s2("456");cout << (s1 + s2)+s2 << endl;cout << s1 << endl;cout << s2 << endl; #endifMyString s1("123");MyString s2("456");cout << (s1 + s2) << endl;cout << s1 << endl;cout << s2 << endl; } /* 123456執行了析構函數 len:6str:123456 123456執行了析構函數 len:3str:123 len:3str:456 456執行了析構函數 123執行了析構函數 */ void test05() { #if 0string s1("123");string s2("456");if (s1 == s2) {cout << "等于" << endl;}elsecout << "不等于"<< endl; #endif //等價于MyString s1("123");MyString s2("456");if (s1 == s2) {cout << "等于" << endl;}elsecout << "不等于" << endl;if (s1 != s2) {cout << "bu等于" << endl;}elsecout << "等于" << endl; } /* 不等于 bu等于 456執行了析構函數 123執行了析構函數 */ int main() {test01();//test02();//test03();//test04();//test05();return 0; }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的C++基础12-类和对象之操作符重载-string练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QImage QPixmap Mat区别
- 下一篇: opencv9-膨胀和腐蚀