成员函数和友元函数的重载:
使用友元函数重载需要传入全部参数,使用成员参数重载要传入所有参数-1。具体用法可以在下文看到
类外定义成员函数: 返回类型 类名 :: 成员函数名(参数列表);
类外定义友元函数: 返回类型 成员函数名(参数列表);
复制赋值运算符的重载
STRING& operator=(const STRING& s)
{
if(this == &s) //检查自赋值
return *this;
delete[] this->pstr;
this->pstr = new char[strlen(s.pstr) + 1];
strcpy(this->pstr, s.pstr);
return *this;
}
输入输出运算符的重载
以字符指针为数据成员的字符串类STRING为例:
函数接口定义:
class STRING{
char* pstr;
public:
STRING(const char* s="");//已实现
~STRING();//已实现
friend istream& operator>>(istream& in, STRING& s);
friend ostream& operator<<(ostream& os, const STRING& s);
};
istream& operator>>(istream& in, STRING& s)
{
char str[81]; //创建临时字符数组
in.getline(str, 81); //读入临时字符数组
delete[] s.pstr; //删除原有的字符串内存,防止内存泄漏
p.str = new char[strlen(str) + 1]; //根据输入字符的长度开辟新的内存空间
strcpy(s.pstr, str); //将输入的字符串复制到新的内存空间中
return in;
}
ostream& operator<<(ostream& os, const STRING& s)
{
os << s.pstr;
return os;
}
注意输出符重载传的第二个参数要加const ,要保证输出数据不被更改
+、-、+=、-=运算符的重载
类接口及定义:
class Complex{
double real, imag;
public:
Complex(double r=0, double i=0): real(r), imag(i){ }
friend istream& operator>>(istream& in, Complex& c); //已实现
friend ostream& operator<<(ostream& out, const Complex& c);//已实现
friend Complex operator+(const Complex& c1, const Complex& c2);
friend Complex operator-(const Complex& c1, const Complex& c2);
Complex& operator+=(const Complex& c);
Complex& operator-=(const Complex& c);
};
Complex operator+(const Complex& c1, const Complex& c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
Complex operator-(const Complex& c1, const Complex& c2)
{
Complex temp;
temp.real = c1.real - c2.real;
temp.imag = c1.imag - c2.imag;
return temp;
}
Complex& Complex :: operator+=(const Complex& c)
{
this->real += c.real; //用成员函数重载省略参数用的对象用this->来操作
this->imag += c.imag;
return *this;
}
Complex& Complex :: operator-=(const Complex& c)
{
this->real -= c.real;
this->imag -= c.imag;
return *this;
}
++、--运算符的重载
Time类接口及定义:
class Time{
int hour, minute, second;
public:
Time(int h=0, int m=0, int s=0);//已实现
Time& operator++();
Time& operator--();
Time operator++(int);
Time operator--(int);
friend istream& operator>>(istream& in, Time& t);//已实现
friend ostream& operator<<(ostream& out, const Time& t);//已实现
};
Time Time :: operator++(int) //后置++
{
this->second ++ ;
if(this->second >= 60)
{
this->minute ++ ;
this->second -= 60;
}
if(this->minute >= 60)
{
this->hour ++ ;
this->minute -= 60;
}
if(this->hour >= 24)
this->hour -= 24;
return *this;
}
Time Time :: operator--(int) //后置--
{
this->second -- ;
if(this->second < 0)
{
this->minute -- ;
this->second += 60;
}
if(this->minute < 0)
{
this->hour -- ;
this->minute += 60;
}
if(this->hour < 0)
this->hour += 24;
return *this;
}
Time& Time :: operator++() //前置++
{
this->second ++ ;
if(this->second >= 60)
{
this->minute ++;
this->second -= 60;
}
if(this->minute >= 60)
{
this->hour ++ ;
this->minute -= 60;
}
if(this->hour >= 24)
this->hour -= 24;
return *this;
}
Time& Time :: operator--() //前置--
{
this->second--;
if(this->second < 0)
{
this->minute -- ;
this->second += 60;
}
if(this->minute < 0)
{
this->hour -- ;
this->minute += 60;
}
if(this->hour < 0)
this->hour += 24;
return *this;
}
注意:传虚拟参数int表示该++或--运算符为后置
重载+运算符来实现字符数组的连接
类接口定义:
class STRING{
char* pstr;
public:
STRING(const char* s="");//已实现
~STRING();//已实现
STRING(const STRING& s);//已实现
STRING& operator=(const STRING& s); //已实现
STRING& operator+(const STRING& s); //将s连接到当前字符串后面
friend istream& operator>>(istream& in, STRING& s); //已实现
friend ostream& operator<<(ostream& os, const STRING& s); //已实现
};
STRING& STRING :: operator+(const STRING& s)
{
char* newpstr; //开一个新的字符数组
newpstr = new char[strlen(this->pstr) + strlen(s.pstr) + 1]; //为新字符数组开辟空间
strcat(newpstr, this->pstr);//把要连接的两个字符数组连接到新字符数组中
strcat(newpstr, s.pstr);//把要连接的两个字符数组连接到新字符数组中
delete[] this->pstr; //删除原字符数组中的内容
this->pstr = new char[strlen(newpstr)]; //为原字符数组开辟空间
strcpy(this->pstr, newpstr);//把新字符数组复制到原字符数组中
return *this; //返回原字符数组
}