单目运算符重载:
包括 前置++,前置- -;后置++, 后置- -
原型:
成员函数写法:className operator++( int )
友元函数写法:friend className operator++(className& obj ,int )
其中,有无 int 参数是区分前置还是后置的标志,有 int 参数的为后置
成员函数写法少写一个参数是因为成员函数中自带一个指向当前对象的指针this
下面以坐标点自加、自减为例
#include <iostream>
using namespace std;
class Cpoint
{
private:
int x;
int y;
public:
Cpoint(int x, int y)
{
this->x = x;
this->y = y;
}
~Cpoint()
{
}
void print()
{
cout << "(" << x << "," << y << ")" << endl;
}
Cpoint operator++();
Cpoint operator--();
//friend Cpoint operator++(Cpoint& c);
//friend Cpoint operator--(Cpoint& c);
//Cpoint operator++(int);
//Cpoint operator--(int);
friend Cpoint operator++(Cpoint& c, int);
friend Cpoint operator==(Cpoint& c, int);
};
Cpoint Cpoint::operator++()
{
x++;
y++;
return *this;
}
Cpoint Cpoint::operator--()
{
x--;
y--;
return *this;
}
/*
Cpoint operator++(Cpoint& c)
{
c.x++;
c.y++;
return c;
}
Cpoint operator--(Cpoint& c)
{
c.x--;
c.y--;
return c;
}
Cpoint Cpoint::operator++(int)
{
Cpoint tmp = *this;
x++;
y++;
return tmp;
}
Cpoint Cpoint::operator--(int)
{
Cpoint tmp = *this;
x--;
y--;
return tmp;
}
*/
Cpoint operator++(Cpoint& c, int)
{
Cpoint tmp(c); //Cpoint tmp = c;
c.x++;
c.y++;
return tmp;
}
Cpoint operator--(Cpoint& c, int)
{
Cpoint tmp(c);
c.x--;
c.y--;
return tmp;
}
int main()
{
Cpoint c(1,2);
/*c.print();
++c;
c.print();
--c;
c.print();*/
c--.print();
//c.print();
c--.print();
c.print();
return 0;
}
双目运算符重载:
1)+ - * /(加减乘除):
原型:
成员函数写法:className operatot + (className& obj) (其中obj一般指右操作数
友元函数写法:friend className operator+(className& obj1, className& obj2)
下面以复数的相加、减、乘、除为例()
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(float a, float b)
{
real = a;
imag = b;
}
~Complex()
{
}
void print()
{
cout << real << "+" << imag << "i" << endl;
}
Complex operator+(Complex& c);
friend Complex operator-(Complex& c1, Complex& c2);
Complex operator*(Complex& c1);
friend Complex operator/(Complex& c1, Complex& c2);
friend ostream& operator<<(ostream& os, Complex &c);
};
ostream& operator<<(ostream& os, Complex &c) //此为“<<”操作符重载
{
cout << c.real << "+" << c.imag << "i";
return os;
}
Complex Complex::operator+(Complex& c)
{
Complex tmp;
tmp.real = this->real + c.real;
tmp.imag = this->imag + c.imag;
return tmp;
}
Complex operator-(Complex& c1, Complex& c2)
{
Complex tmp;
tmp.real = c1.real - c2.real;
tmp.imag = c1.imag - c2.imag;
return tmp;
}
Complex Complex::operator*(Complex& c1)
{
Complex tmp;
tmp.real = c1.real * this->real - c1.imag * this->imag;
tmp.imag = c1.real * this->imag + this->real * c1.imag;
return tmp;
}
Complex operator/(Complex& c1, Complex& c2)
{
Complex tmp;
tmp.real = (c1.real * c2.real) / (c1.real * c1.real + c2.imag * c2.imag);
tmp.imag = (c1.imag * c2.real - c1.real * c2.imag) / (c1.real * c1.real + c2.imag * c2.imag);
return tmp;
}
int main()
{
Complex c1(1, 1);
Complex c2(1, 1);
Complex c3;
c3 = c1 + c2;
//c3 = c1 - c2;
//c3 = c1 * c2;
//c3 = c1 / c2;
//c3.print();
cout << "c3: " << c3 << endl;
return 0;
}
2)重载赋值运算符 “ = ”
重载运算符 “ = ” 可用于解决深拷贝/浅拷贝造成的指针悬挂问题
解决上述问题的方法有两种:
1.手写拷贝构造函数
2.重载运算符“ = ”
重载运算符 “ = ” 的注意事项:
1.不可用友元函数写法
2.不能被继承
3.不能被声明为虚函数
原型:className operate=(className& obj) (函数原型有引用符号)
#include <iostream>
using namespace std;
class Student
{
private:
int age;
char *name;
public:
Student(int age, char *name)
{
this->age = age;
this->name = new char [strlen(name) + 1];
strcpy(this->name, name);
}
Student()
{
age = 0;
strcpy(name, "");
}
~Student()
{
age = 0;
if(name != NULL)
{
delete []name;
name = NULL;
}
}
Student(const Student& s)
{
age = s.age;
name = new char [strlen(s.name) + 1];
strcpy(name, s.name);
}
void print()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl;
}
Student operator=(Student& s);
};
Student Student::operator=(Student& s) //函数原型有引用符号
{
//将旧对象的内存释放
if(name != NULL)
{
delete []name;
name = NULL;
age = 0;
}
//给旧对象分配空间并赋值
age = s.age;
name = new char[strlen(s.name) + 1];
strcpy(name, s.name);
return *this;
}
int main()
{
Student s1(20, "xiaoming");
Student s2 = s1; //初始化语句,调用构造函数
s2 = s1; //赋值语句,调用运算符重载
s2.print();
return 0;
}
重载数组下标运算符“ [ ] ”
注意事项:
1.不可用友元函数
2.只适用于一维数组
3.左操作数是当前类的对象
原型:<数组类型>& operator[] (参数)
例:定义一个数组类:int& operator[ ]( int index )
下面以数组类为例
#include <iostream>
using namespace std;
class Arr
{
private:
int len;
int *arr;
public:
Arr(int len)
{
this->len = len;
arr = new int[len];
}
~Arr()
{
if(arr != NULL)
{
delete []arr;
arr = NULL;
}
}
int& operator[](int index);
};
int& Arr::operator[](int index)
{
if(index < 0 || index >= len)
{
cout << "error..." << endl;
exit(1);
}
return arr[index];
}
int main()
{
Arr a(5);
for(int i = 0; i < 5; i++)
{
a[i] = i + 1;
}
cout << a[0] << endl;
cout << a[1] << endl;
cout << a[5] << endl; //超出数组大小,打印error并退出
cout << a[2] << endl;
return 0;
}
重载左移 << :
注意事项:
1.只能用友元函数写法
原型:friend ostream& operator<<(ostream& os, className& obj)
例:在Complex复数运算的例子里使用过,不再重复