1> 完善my_string类,将能够重载的运算符,全部进行重载
#include <iostream> #include <cstring> using namespace std; class myString { private: char *str; //记录c风格的字符串 int size; //记录字符串的实际长度 public: //无参构造 myString():size(10) { str = new char[size]; //构造出一个长度为10的字符串 strcpy(str,""); //赋值为空串 } //有参构造 myString(const char *s) //string s("hello world") { size = strlen(s); str = new char[size+1]; strcpy(str, s); } //拷贝构造 myString(const myString &other):str(new char(*(other.str))),size(other.size) { strcpy(this->str,other.str); } //析构函数 ~myString() { delete []str; str = nullptr; } //拷贝赋值函数 myString & operator=(const myString &other) { this->str = new char(*(other.str)); this->size = other.size; strcpy(this->str,other.str); return *this; } //判空函数 bool empty1() { return *(str)=0?1:0; } //size函数 int size1() { return size; } //c_str函数 char* &c_str() { return str; } //at函数 char &at(int pos) { return *(str+pos); } //加号运算符重载 myString &operator + (const myString &R) { this->str = strncat(this->str,R.str,R.size); return *this; } //加等于运算符重载 myString &operator += (const myString &R) { char *temp = this->str; delete [] str; this->size = R.size + this->size; this->str = new char[this->size + 1]; strcpy(this->str, temp); strcpy(this->str, R.str); return *this; } //关系运算符重载(>) bool operator >(const myString &R)const { return strcmp(this->str,R.str)>0; } //前置自增运算符重载 myString &operator ++() { ++this->str; ++this->size; return *this; } //中括号运算符重载函数 char operator[](int index) { if(index <0 ||index > this->size) { cout<<"越界"<<endl; } return this->str[index]; } friend ostream& operator<< (ostream& cout, const myString& other); }; //输入<<运算符重载函数 ostream& operator<< (ostream& cout, const myString& other) { cout<<other.str; return cout; } int main() { myString str1; myString str2("ni hao"); cout<<str2.c_str()<<endl;//有参构造 str1= str2; cout<<str1.c_str()<<endl;//拷贝赋值 myString str3(str1); cout<<str3.c_str()<<endl; //拷贝构造 myString str4("zhangsan"); myString str5("lisi"); myString str6=str4+str5; cout<<str6.c_str()<<endl; //加运算符 myString str7=str4+str5; cout<<str7.c_str()<<endl; //加等于运算符 cout<<"****************************"<<endl; myString str8("hello"); //前置自增 myString str10 =++str8; cout<<str10.c_str()<<endl; //[]运算符 cout<<str8[2]<<endl; return 0; }
2> 将继承过程中特殊成员函数相关代码重新实现一遍
#include <iostream> using namespace std; class Father { public: string name; public: //无参构造 Father() {cout<<"Father::无参构造"<<endl;} //有参构造 Father(string n):name(n) {cout<<"Father::有参构造"<<endl;} //拷贝构造 Father(const Father&other):name(other.name) {cout<<"Father::拷贝构造"<<endl;} //拷贝赋值 Father& operator=(const Father&other) { if(this != &other) { this->name = other.name; } cout<<"Father::拷贝赋值函数"<<endl; return *this; } //析构函数 ~Father() {cout<<"Father::析构函数"<<endl;} }; class Son:public Father { private: string toy; //玩具 public: //子类无参构造 Son(){cout<<"Son::无参构造"<<endl;} //子类有参构造 Son(string n,string t):Father(n),toy(t) { cout<<"Son::有参构造"<<endl; } //子类析构函数 ~Son(){cout<<"Son::析构函数"<<endl;} //子类的拷贝构造函数 Son(const Son &other):Father(other),toy(other.toy) { cout<<"Son::拷贝构造"<<endl; } //子类的拷贝赋值函数 Son &operator = (const Son &other) { if(this != &other) { Father::operator=(other); this->toy = other.toy; } cout<<"Son::拷贝赋值"<<endl; return *this; } void show() { cout<<"name = "<<name<<endl; } }; int main() { Son s1; //无参构造 Son s2("zhangsan","car"); //有参构造 Son s3(s2); //拷贝构造 s1=s3; //拷贝赋值函数 s1.show(); cout<<"0000000000000000000000000000"<<endl; return 0; }
3> 完成今日的内容的思维导图绘制