运算符重载概念
C++准许以运算符命名函数!!!
运算符重载是指在编程语言中,通过定义类的成员函数或全局函数来改变原有运算符的行为。通过运算符重载,可以使得用户自定义的类对象能够像基本类型一样进行运算操作。
运算符重载的概念包括以下几个方面:
-
对于类对象,可以通过成员函数或全局函数重载运算符。例如,可以定义一个类的成员函数来重载"+"运算符,使得两个对象相加时能够按照自定义的方式进行运算。
-
运算符重载可以改变运算符的操作数数量。例如,可以定义一个全局函数来重载"-"运算符,使得它可以接受一个参数或两个参数进行运算。
-
运算符重载可以改变运算符的操作数类型。例如,可以定义一个类的成员函数来重载"*"运算符,使得它能够用于不同类型的操作数。
-
运算符重载可以改变运算符的返回值类型。例如,可以定义一个类的成员函数来重载"="运算符,使得它返回一个引用,从而实现链式赋值。
运算符重载使得用户自定义的类对象能够方便地进行运算操作,提高了代码的可读性和可维护性。同时,运算符重载也需要谨慎使用,避免出现歧义或不符合预期的行为。
string a = “hello”;
a += “ world”;// +(a, “world”);
cout<<“hello”; // <<(cout, “hello”);
可以重载的运算符
在C++中,可以重载的运算符如下:
-
一元运算符:+、-、*、&、!、~、++、--(前缀和后缀形式)、sizeof、typeid、new、delete。
-
二元运算符:+、-、、/、%、=、+=、-=、=、/=、%=、==、!=、<、>、<=、>=、&&、||、&、|、^、<<、>>、[]、()、->、,。
-
也可以重载下标运算符[]和函数调用运算符()的形式。
需要注意的是,有些运算符是不允许被重载的,例如.
和.*
运算符、::
作用域运算符、?:
条件运算符、sizeof
运算符、typeid
运算符等。此外,也不能改变运算符的优先级和结合性。
在重载运算符时,可以通过类的成员函数形式或者全局函数形式来定义运算符的重载操作。根据具体的需求,可以选择成员函数形式或全局函数形式来进行运算符重载。
运算符分类
数学运算符
+ - * / ++ --
关系运算符
== >= <= !=
特殊运算符
[ ]
= 赋值运算符
( ) 仿函数
<< 输出运算符
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace std;
class Timer
{
public:
Timer()
{
hour = 0;
min = 0;
sec = 0;
}
~Timer()
{
}
void addtimer(int sec = 1)
{
this->min += (this->sec + sec) / 60;
this->sec = (this->sec + sec) % 60;
}
void show()
{
printf("%2d:%2d:%2d\n", hour, min, sec);
}
Timer operator+(int sec)
{
Timer tem;
tem.sec = this->sec + sec;
return tem;
}
Timer operator+(Timer& x)
{
Timer tem;
tem.sec = sec + x.sec;
tem.min = min + x.min;
tem.hour = hour + x.hour;
return tem;
}
Timer operator++(int)//后++
{
Timer tem = *this;//backup
sec++;
return tem;
}
Timer operator++()//前++ 数学运算符
{
sec++;
return *this;
}
bool operator==(Timer& x)//关系运算符
{
if (sec == x.sec && min == x.min && hour == x.hour)
return true;
return false;
}
int& operator[](int i)
{
switch (i)
{
case 0:
return hour;
case 1:
return min;
case 2:
return sec;
}
}
friend ostream& operator<<(ostream& out, const Timer& t);
private:
int hour;
int min;
int sec;
};
ostream& operator<<(ostream& out, const Timer& t)//输出运算符 不能写成成员函数
{
out << "hour: " << t.hour << " min: " << t.min << " sec: " << t.sec << endl;
}
int main()
{
Timer t;
t.addtimer(3);
Timer t1;
t1.addtimer(5);
Timer t2;
t2 = t + t1;
t2.show();
t2 = t2 + 5;
t2.show();
Timer t3 = ++t2;
t3.show();
t2.show();
int i = 0;
for (;i < 10; ++i)
printf("++\n");
if (t2 == t3)
printf("OK..time out!\n");
printf("hour: %d\n", t2[0]);
printf("min : %d\n", t2[1]);
printf("sec : %d\n", t2[2]);
t2[1] = 30;
printf("hour: %d\n", t2[0]);
printf("min : %d\n", t2[1]);
printf("sec : %d\n", t2[2]);
cout << t2;
#if 0
while (1)
{
t.addtimer(1);
t.show();
sleep(1);
}
#endif
}
#include <stdio.h>
#include <string.h>
class A
{
public:
A()
{
printf("A()\n");
p = new char[10];
strcpy(p, "hello");
printf("p: %s\n", p);
printf("p: %s\n", this->p);
}
A(const A& x)
{
printf("A(const A &x)\n");
p = new char[10];
strcpy(p, x.p);
}
~A()
{
printf("~A()\n");
delete[] p;
}
A& operator=(A& x)//赋值运算符符
{
printf("operator=\n");
p = new char[10];
strcpy(p, x.p);
return *this;
}
private:
char* p;
};
int main()
{
A x;
A y = x;
y = x;
}
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
// printf("input: ");fflush(stdout);
cout<<"input: ";
char buf[100];
// gets(buf);
cin >> buf;
// printf("%s\n", buf);
cout << buf << endl;
std::cout<<10<<std::endl;
cout<<10<<endl;
cout<< hex <<10<<endl;
}
#include <iostream>
using namespace std;
class Converter
{
public:
Converter(double rate)
{
this->rate = rate;
}
double operator()(double rmb)
{
return rmb * rate;
}
private:
double rate;
};
double RMBto(double rmb, double rate)
{
return rmb * rate;
}
int main()
{
// std::cout << "hello"<<std::endl;
// cout << "hello"<<endl;
Converter RMBtoUS(6.4);
cout << RMBtoUS(10) << endl;//RMBtoUS(10)仿函数
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
Converter RMBtoE(8.4);
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
#if 0
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
#endif
}
实现形式
成员函数式(第一个行参是对象的引用)
class Time{
public:
Time operator+(Time t);
private:
int hour;
int min;
int sec;
};
Time a, b;
Time c = a+b;
友元函数式(左操作数不是本身, 可交换型)
class Time{
public:
friend void operator<<(ostream, Time);
private:
int hour;
int min;
int sec;
};
Time a;
std::out<<a;