C++允许函数和运算符多个定义,当重载函数或者重载运算符被调用的时候,编译器将决定使用的参数类型与定义函数中参数类型进行比对,选择对合适的函数,这个过程也就是重载决策。
1. 函数重载
函数同名,参数不同。
#include
using namespace std;
class Print
{
public:
void print(int n)
{
cout << "int : " << n << endl;
}
void print(float f)
{
cout << "float : " << f << endl;
}
};
int main(int argc, char **argv)
{
Print p;
p.print(3);
p.print(3.14);
return 0;
}
//输出:
//int : 3
//float : 3.14
2. 运算符重载
大部分运算符都可以进行重载,用于自定义运算类型,当然也有例外的运算符:
#include
using namespace std;
class AREA
{
private:
int length;
int width;
public:
AREA()
{
length = 0;
width = 0;
}
AREA(int len, int wid)
{
length = len;
width = wid;
}
public:
void SetLength(int len)
{
length = len;
}
void SetWidth(int wid)
{
width = wid;
}
int GetLength()
{
return length;
}
int GetWidth()
{
return width;
}
public:
AREA operator+(const AREA &area)
{
AREA temp;
temp.length = length + area.length;
temp.width = width + area.width;
return temp;
}
AREA operator-()
{
AREA temp;
temp.length = -length;
temp.width = -width;
return temp;
}
void operator=(const AREA &area)
{
length = area.length;
width = area.width;
}
bool operator==(const AREA &area)
{
return length == area.length && width == area.width;
}
bool operator<(const AREA &area)
{
if (length < area.length)
{
return true;
}
if (length == area.length && width < area.width)
{
return true;
}
return false;
}
bool operator>(const AREA &area)
{
if (length > area.length)
{
return true;
}
if (length == area.length && width > area.width)
{
return true;
}
return false;
}
friend ostream & operator<<(ostream & output, const AREA &area)
{
output << "len : " << area.length << ", wid : " << area.width << endl;
return output;
}
friend istream & operator>>(istream & input, AREA &area)
{
input >> area.length >> area.width;
return input;
}
AREA operator()(int a, int b, int c, int d)
{
AREA area;
area.length = a + b;
area.width = c + d;
return area;
}
AREA operator++()
{
++length;
++width;
return AREA(length, width);
}
AREA operator++(int)
{
AREA area(length, width);
++area.length;
++area.width;
return area;
}
};
int main(int argc, char **argv)
{
AREA a, b, c, d, e;
a.SetLength(3);
a.SetWidth(4);
b.SetLength(5);
b.SetWidth(5);
// 求和
c = a + b;
cout << "c.length : " << c.GetLength()<< endl;
cout << "c.width : " << c.GetWidth()<< endl;
// 大于
cout << "a > b : " << (a > b) << endl;
// 小于
cout << "a < b : " << (a < b) << endl;
// 取反
d = -a;
cout << "d.length : " << d.GetLength()<< endl;
cout << "d.width : " << d.GetWidth()<< endl;
// >>
// 输入length和width
cin >> e;
cout << "e.length : " << e.GetLength()<< endl;
cout << "e.width : " << e.GetWidth()<< endl;
// <<
cout << "a : " << a << endl;
// ()
a(1, 2, 1, 2);
// 前缀++
++b;
// 后缀++
c++;
return 0;
}
有一个特殊的运算符是[]
,不是因为用法特殊,而是集成不到上面这个类里(笑),所以单独写出来
#include
using namespace std;
const int ARRAY_SIZE = 10;
class SPECIAL
{
private:
int array[ARRAY_SIZE];
public:
SPECIAL()
{
for (int i = 0; i < ARRAY_SIZE; ++i)
{
array[i] = i;
}
}
int & operator[](int pos)
{ // 这里只是举个使用的示例,最好还是在使用数组下标前确认是否合法
if (pos < 0)
{
return array[0];
}
else if (pos > ARRAY_SIZE)
{
return array[ARRAY_SIZE - 1];
}
return array[pos];
}
};
int main(int argc, char **argv)
{
SPECIAL ps;
cout << "-1 : " << ps[-1] << endl;
cout << "100 : " << ps[100] << endl;
cout << "3 : " << ps[3] << endl;
return 0;
}
常见的运算符重载就这些,如果有遗漏,以后再补充。