運算符重載
myString.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <cstring>
#include <iostream>
using namespace std;
class myString
{private:char *str; //記錄c風格的字符串int size; //記錄字符串的實際長度int capacity; //記錄字符串的容量public://無參構造myString():size(10), capacity(10){str = new char[size]; //構造出一個長度為10的字符串}//有參構造myString(const char *s); //有參構造 string s("hello wirld");//有參構造myString(int n, char ch); //string s(5, 'A');//析構函數~myString();void show();//拷貝構造函數myString(const myString &other);//拷貝賦值函數myString& operator=(const myString &other);//判空函數bool empty() const;//size函數int getSize() const;//c_str函數const char* c_str() const;//at函數char &at(int index);//二倍擴容void resize(int newCapacity);//實現+=運算符重載myString& operator+=(const myString &other);//取地址運算符重載myString* operator&();//將[]運算符重載char& operator[](const int index);////將+重載myString& operator+(const myString &other);//將==重載bool operator==(const myString &other) const;//將!=重載bool operator!=(const myString &other) const;//將>重載bool operator>(const myString &other) const;//將<重載bool operator<(const myString &other) const;//將>=重載bool operator>=(const myString &other) const;//將<=重載bool operator<=(const myString &other) const;// 友元函數,重載<<運算符friend ostream& operator<<(ostream &os, const myString &s){os << s.str;return os;}// 友元函數,重載>>運算符friend istream& operator>>(istream &is, const myString &s){is>> s.str;return is;}
};
#endif // MYSTRING_H
myString.cpp
#include"myString.h"
//有參構造
myString::myString(const char *s)
{if(s){size=strlen(s);capacity=size+1;str=new char[size];strcpy(str, s);}else {size = 0;capacity = 10;str = new char[size];}
}
//有參構造
myString::myString(int n, char ch): size(n), capacity(n + 1)
{str = new char[size];memset(str, ch, n);
}
//析構函數
myString::~myString()
{delete[]str;
}void myString::show()
{cout<<"字符串為:"<<this->str<<endl;
}
//拷貝構造函數
myString::myString(const myString &other): size(other.size), capacity(other.capacity)
{str = new char[size];strcpy(str, other.str);
}
//拷貝賦值函數
myString &myString::operator=(const myString &other)
{if (this != &other){delete[] str;size = other.size;capacity = other.capacity;str = new char[size];strcpy(str, other.str);}return *this;
}
//判空函數
bool myString::empty() const
{return size == 0;
}
//size函數
int myString::getSize() const
{return size;
}
// c_str函數
const char *myString::c_str() const
{return str;
}
// at函數
char &myString::at(int index)
{if (empty()||index < 0 || index >= size){cout<<"訪問元素失敗"<<endl;}return str[index];
}
//二倍擴容
void myString::resize(int newCapacity)
{char *newStr = new char[newCapacity];strcpy(newStr, str);delete[] str;str = newStr;capacity = newCapacity;
}
//實現+=運算符重載
myString &myString::operator+=(const myString &other)
{int newSize = size + other.size;if (newSize >= capacity) {resize(newSize * 2);}strcat(str, other.str);size = newSize;return *this;
}
//取地址運算符重載
myString *myString::operator&()
{return this;
}
//將[]運算符重載
char &myString::operator[](const int index)
{if(index<0||index>=size){cout<<"重載失敗"<<endl;}return str[index];
}
//將+重載
myString &myString::operator+(const myString &other)
{int newSize=size+other.size;if (newSize >= capacity) {resize(newSize * 2);}strcpy(this->str,str);strcat(this->str,other.str);return *this;
}//將==重載
bool myString::operator==(const myString &other) const
{return strcmp(str,other.str)==0;
}
//將!=重載
bool myString::operator!=(const myString &other) const
{return strcmp(str,other.str)!=0;
}
//將>重載
bool myString::operator>(const myString &other) const
{return strcmp(str,other.str)>0;
}
//將<重載
bool myString::operator<(const myString &other) const
{return strcmp(str,other.str)<0;
}
//將>=重載
bool myString::operator>=(const myString &other) const
{return strcmp(str,other.str)>=0;
}
//將<=重載
bool myString::operator<=(const myString &other) const
{return strcmp(str,other.str)<=0;
}
main.cpp
#include"myString.h"int main()
{myString s1("Hello");myString s2(" World");s1 += s2;s1.show(); // 輸出 "Hello World"cout << "size: " << s1.getSize() << endl; // 輸出 "Size: 11"cout<<s1[0]<<endl;myString s3=s1+s2;s3.show();myString s4("aaaaa");myString s5("bbbbb");if(s4==s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4!=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4>s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4<s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4>=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4<=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}myString s6;cin>>s6;s6.show();return 0;
}