查漏补缺:
在priavte的函数中 *类型的永远要注意有没有用new函数分配内存 否则会报错
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char * s;
public:
String();
String(const char *);
String(const String &);
~String();
String & operator=(const char *);
String & operator=(const String &);
String operator+(const char *);
String operator+(const String &);
String & operator+=(const char *);
String & operator+=(const String &);
friend istream & operator>>(istream &, String &);
friend ostream & operator<<(ostream &, const String &);
friend bool operator==(const String &, const char *);
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const char *);
friend bool operator!=(const String &, const String &);
};
String::String() : s(new char[1]) { s[0] = '\0'; }
String:: String(const char *a) : s(new char[strlen(a) + 1]) {
strcpy(s, a);
}
String::String(const String &o) {
s = new char[strlen(o.s) + 1];
strcpy(s, o.s);
}
String::~String() { delete[] s; }
bool operator==(const String &a, const char *string){
return strcmp(a.s,string)==0;
}
bool operator==(const String &a, const String &b){
return strcmp(a.s,b.s)==0;
}
bool operator!=(const String &a, const char *string){
return !strcmp(a.s,string)==0;
}
bool operator!=(const String &a, const String &b){
return !strcmp(a.s,b.s)==0;
}
String & String::operator=(const char *a){
if(s!=a){
delete[] s;
}
strcpy(s,a);
return *this;
}
String & String::operator=(const String &a){
if(s!=a.s){
delete[] s;
}
strcpy(s,a.s);
return *this;
}
String String::operator+(const char *a){
String temp;
temp.s=new char[strlen(s)+strlen(a)+1];
strcpy(temp.s,s);
strcat(temp.s,a);
return temp;
}
String String::operator+(const String &a){
String temp;
temp.s=new char[strlen(s)+strlen(a.s)+1];
strcpy(temp.s,s);
strcat(temp.s,a.s);
return temp;
}
//String & String::operator+=(const char *a){
// String temp;
// temp.s=new char[strlen(s)+strlen(a)+1];
// strcpy(temp.s,s);
// strcat(temp.s,a);
// delete []s;
// strcpy(s,temp.s);
// return *this;
//}
String & String::operator+=(const char *str) {
char *temp;
temp= new char[strlen(s)+strlen(str)+1];
strcpy(temp, s);
strcat(temp, str);
delete[] s;
s = temp;
return *this;
}
String &String::operator+=(const String &other) { return *this += other.s; }
istream & operator>>(istream &is, String &a){
is>>a.s;
return is;
}
ostream & operator<<(ostream &os, const String &a){
os<<a.s;
return os;
}
int main()
{
String s;
s += "hello";
cout<<s<<endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}
方式
友元函数 成员函数
1、friend TYPE operator@(形参表);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &)
重载运算符
自增自减运算符重载
#include<iostream>
using namespace std;
class CheckedPtr
{
public:
CheckedPtr(int * b, int * e) : beg(b), end(e), curr(b) { }
CheckedPtr & operator ++(); // prefix ++
CheckedPtr & operator --(); // prefix --
CheckedPtr operator ++(int); // postfix ++
CheckedPtr operator --(int); // postfix --
int * GetBeg();
int * GetEnd();
int * GetCurr();
private:
int * beg; // pointer to beginning of the array
int * end; // one past the end of the array
int * curr; // current position within the array
};
int* CheckedPtr::GetBeg() {
return beg;
}
int* CheckedPtr::GetCurr(){
return curr;
}
int* CheckedPtr::GetEnd(){
return end;
}
CheckedPtr & CheckedPtr::operator ++(){
if(curr<end) ++curr;
return *this;
}
CheckedPtr & CheckedPtr::operator --(){
if(curr>beg) --curr;
return *this;
}
CheckedPtr CheckedPtr:: operator ++(int){
CheckedPtr temp(*this);
++(*this);
return temp;
}
CheckedPtr CheckedPtr ::operator --(int){
CheckedPtr temp(*this);
--(*this);
return temp;
}
int main(){
int n;
cin>>n;
int * array = new int[n];
for(int i=0;i<n;i++)
cin>>array[i];
CheckedPtr cp(array, array+n);
for(;cp.GetCurr()<cp.GetEnd();cp++)
cout<<*cp.GetCurr()<<" ";
cout<<endl;
for(--cp;cp.GetCurr()>cp.GetBeg();cp--)
cout<<*cp.GetCurr()<<" ";
cout<<*cp.GetCurr()<<endl;
delete [] array;
return 0;
}
下标重载