(operator符号)
1.重载:重新赋予新的含义。
2.函数重载: 对一个已有的函数赋予新功能(函数名相同,参数类型或个数不同的成员函数)
3.运算符重载:一种特殊的函数重载
①.C++绝大部分的运算符可以重载,不能重载的运算符:
·(成员引用运算符).*(对象成员指针运算符)::(域运算符)?:(条件运算符)sizeof(取长度运算符)
②.重载为类的 “成员函数”
1)单目运算符 重载为成员函数(最好)
{++, --, +(正), -(负), *(解除引用), ->(成员选择), !(逻辑非), &(取址), ~(求反)}
2)参数个数比原来的运算对象个数少一个(右++右—除外)
③.重载为类的 友元函数
1)双目运算符 重载为友元函数(最好)
{>,<,>>,<<,=,>=等}
2)双目运算符不能将 =()【】-> 重载为类的友元函数
3)参数个数比原运算符的运算个数相同
④.运算符重载后,既不会改变原运算符的优先级和结合特性,
也不会改变使用运算符的语法和参数个数
⑤.1).单目运算符:运算对象只有1个
{取正(+)、取负(-)、取反(^)、或(|)、与(&)等等}
2).双目运算符:运算对象有2个
{加(+)减(-)乘(*)除(/)、自加(++)、自减(--)、逻辑与(||)、逻辑或(&&)、取余(%)、赋值(=)等}
3).三目运算符: ? : (需要三个操作数)
1.+ 和 -重载:(成员和友元)
=====================================
#include <iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample(){}
Sample(int a) {x=a;}
void disp() {cout<<"x="<<x<<endl;}
Sample operator+ (Sample &s);
//引用是传递地址。效率更高(对象是直传递,效率相对较低)
//也可以加const,只能引用s的值,不能改变s的值
friend Sample operator- (Sample &s1, Sample &s2); //加friend,类外的函数加 域::
};
//成员 +
Sample Sample::operator+ (Sample &s)
//仅1个参数,另一个被隐含
{
return Sample(x + s.x); //不能x.s
}
//友元 -
Sample operator- (Sample &s1, Sample &s2)
//仅2个参数,无隐含参数
{
return Sample(s1.x - s2.x);
}
int main() {
Sample obj1(10);
Sample obj2(20);
Sample obj3;
obj3=obj1+obj2;
obj3.disp();
obj3=obj1-obj2;
obj3.disp();
}
----------------------------------------------
x=20
x=-10
----------------------------------------------
2. = 重载
=====================================
#include <iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample(){}
Sample(int a) {x=a;}
void disp() {cout<<"x="<<x<<endl;}
void operator= (Sample &s);
};
void Sample::operator=(Sample &s)
{
x=s.x;
}
int main()
{
Sample obj1(10);
Sample obj2;
obj2=obj1;
obj1.disp();
obj2.disp();
}
----------------------------------------------
x=10
x=10
----------------------------------------------
3.x++: operator++ (int) 后置+
++x: operator++ () 前置+
=====================================
#include <iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample(){}
Sample(int a) {x=a;}
void disp() {cout<<"x="<<x<<endl;}
int operator++ (); // ++x
int operator++ (int); // x++
};
int Sample::operator++ (){x+=1; return x;}
int Sample::operator++ (int){x+=1; return x;}
int main()
{
Sample obj(5); obj.disp();
obj++; obj.disp();
obj++; obj.disp();
}
----------------------------------------------
x=5
x=6
x=7
----------------------------------------------4. (流输入)>> :规定类型 istream&
(流提取)<< :规定类型 ostream&
(双目记得用friend友元)
=====================================
#include <iostream>
using namespace std;
class Student{
private:
string name; //不用char,char仅2个字节
int score;
public:
static int rank;//用静态成员
Student(){}
friend istream &operator>>(istream& in,Student& s)
{
in>>s.name>>s.score;
s.rank++;
return in;
}
friend ostream& operator<<(ostream& out,Student& s);
};
ostream& operator<<(ostream& out, Student& s)//类外不要加friend
{
out<<s.rank<<". "<<s.name<<" ";
if(s.score>=60)
out<<"PASS";
else
out<<"FAIL";
return out;
}
int Student::rank=0;
int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;i<repeat;i++)
{
cin>>st;
cout<<st<<endl;
}
return 0;
}
----------------------------------------------
3
Li 75
Zhang 50
Yang 99
1. Li PASS
2. Zhang FAIL
3. Yang PASS
----------------------------------------------