函数重载和运算符重载
C++允许在同一个作用域内中的某个函数或者运算符指定多个定义,分别有函数重载和运算符重载,重载就是函数和方法具有相同的名称但是它们的参数列表不同,调用重载函数或者运算符时会根据参数列表进行匹配找到最适合的定义。
函数重载
同名函数参数列表不同:1.参数的个数不同;2.参数的类型不同;
特别注意:不能根据函数的返回值不同来区分不同的重载函数
class PrintData {
public:
void print(int i)
{
cout << "整型数为 :" << i << endl;
}
void print(double f)
{
cout << "浮点数为 :" << f << endl;
}
void print(string str)
{
cout << "字符串为 :" << str << endl;
}
};
void test1()
{
cout << "函数重载" << endl;
PrintData obj;
//输出整型数
obj.print(6);
//输出浮点数
obj.print(7.8);
//输出字符串
obj.print("hello world");
cout << endl << endl;
}
运行结果
运算符重载
C++内置的运算符只支持对基本的数据类型进行运算,当我们用到自定义的数据类型时,可以根据需要将运算符进行重载,使其能够进行自定义类型的运算,运算符重载本质上是运算符为名称的函数,其格式为 返回值 + operator待重载的运算符 + 参数列表,和函数重载的形式差不多,只是多了opreator这个关键字。
class Line
{
int length;
public:
Line(int len = 0)
{
length = len;
}
//单目运算符
//重载取反运算符
Line operator-()
{
Line line;
line.length = -this->length;
return line;
}
//++i
Line operator++()
{
this->length = ++this->length;
return *this;
}
//i++
Line operator++(int)
{
Line obj = *this;
++this->length;
return obj;
}
//重载+运算符
Line operator+(Line obj)
{
Line line;
line.length = this->length + obj.length;
return line;
}
//重载-运算符
Line operator-(Line obj)
{
Line line;
line.length = this->length - obj.length;
return line;
}
//重载*运算符
Line operator*(Line obj)
{
Line line;
line.length = this->length * obj.length;
return line;
}
//重载/运算符
Line operator/(Line obj)
{
Line line;
line.length = this->length / obj.length;
return line;
}
//重载关系运算法
//<,>,==
bool operator < (const Line& line)
{
if (this->length < line.length)
return true;
else
return false;
}
bool operator > (const Line& line)
{
if (this->length > line.length)
return true;
else
return false;
}
bool operator == (const Line& line)
{
if (this->length == line.length)
return true;
else
return false;
}
//重载输入输出运算符
friend ostream& operator<<(ostream& output, const Line& line)
{
output << "line of length : " << line.length << endl;
return output;
}
friend istream& operator>>(istream& input, Line& line)
{
input >> line.length;
return input;
}
//重载函数调用运算符
Line operator()(int a, int b)
{
Line line;
line.length = a + b;
return line;
}
int getLength()
{
return length;
}
};
单目运算符
Line operator-()
{
Line line;
line.length = -this->length;
return line;
}
//++i
Line operator++()
{
this->length = ++this->length;
return *this;
}
//i++
Line operator++(int)
{
Line obj = *this;
++this->length;
return obj;
}
void test2()
{
//单目运算符
cout << "单目运算符重载" << endl;
Line line(6);
Line line1 = -line;
cout << "取反后的结果 : " << line1.getLength() << endl;
cout << endl << endl;
cout << "前缀自增和后缀自增" << endl;
Line line2(10), line3, line4;
cout << "未自增前 : " << endl;
cout << "line2 : " << line2.getLength() << endl;
cout << "line3 : " << line3.getLength() << endl;
cout << "line4 : " << line4.getLength() << endl;
line3 = line2++;
cout << "后缀自增后 : " << endl;
cout << "line2 : " << line2.getLength() << endl;
cout << "line3 : " << line3.getLength() << endl;
line4 = ++line2;
cout << "前缀自增后 : " << endl;
cout << "line2 : " << line2.getLength() << endl;
cout << "line4 : " << line4.getLength() << endl;
}
运行结果
双目运算符
//重载+运算符
Line operator+(Line obj)
{
Line line;
line.length = this->length + obj.length;
return line;
}
//重载-运算符
Line operator-(Line obj)
{
Line line;
line.length = this->length - obj.length;
return line;
}
//重载*运算符
Line operator*(Line obj)
{
Line line;
line.length = this->length * obj.length;
return line;
}
//重载/运算符
Line operator/(Line obj)
{
Line line;
line.length = this->length / obj.length;
return line;
}
void test3()
{
Line line1(6);
Line line2(2);
Line line3 = line1 + line2;
cout << "重载+运算符号" << endl;
cout << "line3 :" << line3.getLength() << endl;
line3 = line1 - line2;
cout << "重载-运算符号" << endl;
cout << "line3 :" << line3.getLength() << endl;
line3 = line1 * line2;
cout << "重载*运算符号" << endl;
cout << "line3 :" << line3.getLength() << endl;
line3 = line1 / line2;
cout << "重载/运算符号" << endl;
cout << "line3 :" << line3.getLength() << endl;
}
运行结果
逻辑运算符
//重载关系运算法
//<,>,==
bool operator < (const Line& line)
{
if (this->length < line.length)
return true;
else
return false;
}
bool operator > (const Line& line)
{
if (this->length > line.length)
return true;
else
return false;
}
bool operator == (const Line& line)
{
if (this->length == line.length)
return true;
else
return false;
}
void test4()
{
Line line1(6);
Line line2(5);
if (line1 < line2)
cout << "line1 is samller than line2" << endl;
else
cout << "line1 is not samller than line2" << endl;
if(line1 > line2)
cout << "line1 is larger than line2" << endl;
else
cout << "line1 is not larger than line2" << endl;
if(line1 == line2)
cout << "line1 is equal to line2" << endl;
else
cout << "line1 is not equal to line2" << endl;
}
运行结果
输入输出运算符
//重载输入输出运算符
friend ostream& operator<<(ostream& output, const Line& line)
{
output << "line of length : " << line.length << endl;
return output;
}
friend istream& operator>>(istream& input, Line& line)
{
input >> line.length;
return input;
}
void test5()
{
Line line;
cin >> line;
cout << line;
}
函数()运算符
// 重载函数调用运算符
Line operator()(int a, int b)
{
Line line;
line.length = a + b;
return line;
}
void test6()
{
Line line1(6), line2;
line2 = line1(2, 3);
cout << line1 << endl;
cout << line2 << endl;
}
运行结果
索引运算符
const int SIZE = 10;
class MyArray {
int arr[SIZE];
public:
MyArray()
{
for (int i = 0; i < SIZE; i++)
{
arr[i] = i;
}
}
//重载[]
int& operator[](int index)
{
if (index >= SIZE)
{
cout << "索引超过最大值 ";
return arr[0];
}
return arr[index];
}
};
void test7()
{
MyArray arr;
cout << "arr[1]的值为 : " << arr[1] << endl;
cout << "arr[6]的值为 : " << arr[6] << endl;
cout << "arr[12]的值为 : " << arr[12] << endl;
}