1- 基本要求
C++预定义的运算符,只能用于基本数据类型的运算:整型、实型、字符型、逻辑型…+、-、*、/、%、^、&、~、!、|、=、<<>>、!=、
在数学上,两个复数可以直接进行+、-等运算。但在C++中,直接将+或-用于复数对象是不允许的。
2- 运算符重载定义
- 运算符重载,就是对已有的运算符(C++中预定义的运算符)赋予多重的含义,使同一运算符作用于不同类型的数据时导致不同类型的行为。
- 运算符重载的目的是:扩展C++中提供的运算符的适用范围,使之能作用于对象。同一个运算符,对不同类型的操作数,所发生的行为不同。
3- 运算符重载作用
赋予运算符自定义功能。
默认:常数+常数
比如对象+对象,默认情况下是不被允许的,当我们重载了+后,就可以实现对象相加了。
对象+常数,常数+对象重载函数,函数的两个参数在小括号内,运算符的两个参数在两侧。
4- 基本形式
- 运算符重载的实质是函数重载
- 可以重载为普通函数,也可以重载为成员函数 。
- 把含运算符的表达式转换成对运算符函数的调用。
- 把运算符的操作数转换成运算符函数的参数。运算符被多次重载时,根据实参的类型决定调用哪个运算符函数。
返回值类型 operator 运算符(形参表)
{
……
}
5- 运算符重载案列
5.1 无函数返回值
#include <iostream>
using namespace std;
class CStu
{
public:
// 声明两个成员变量
int nAge;
double dScore;
// 创建构造函数
CStu()
{
nAge = 12;
dScore = 12.12;
}
};
//运算符重载
void operator+(CStu& st, int a)
{
cout << (st.nAge + a) << endl;
}
void operator+(int a, CStu& st)
{
cout << (st.nAge + a) << endl;
}
int main()
{
// 声明两个对象
CStu st1,
st2;
// +
int a = 13;
a + 13;
//st1 + st2;
st1 + 12;
12 + st2;
return 0;
}
5. 2 带函数返回值
#include <iostream>
using namespace std;
class CStu
{
public:
// 声明两个成员变量
int nAge;
double dScore;
// 创建构造函数
CStu()
{
nAge = 12;
dScore = 12.12;
}
};
//运算符重载
int operator+(CStu& st, int a)
{
return (st.nAge + a);
}
int main()
{
// 声明两个对象
CStu st1,
st2;
cout << (st1 + 12 + 13) << endl;
return 0;
}
5.3 类外重载
左边是左参数,右边是右参数
#include <iostream>
using namespace std;
class CStu
{
public:
int nAge;
double dScore;
CStu()
{
nAge = 12;
dScore = 12.12;
}
};
//运算符重载
int operator+(CStu& st, int a)
{
return (st.nAge + a);
}
int operator+(int a, CStu& st)
{
return (st.nAge + a);
}
CStu& operator+(CStu& st1, CStu& st)
{
st1.nAge += st.nAge;
return st1;
}
CStu& operator::(CStu& st1,int a)
{
st1.nAge += st.nAge;
return st1;
}
int main()
{
CStu st1, //对象1
st2;
// 输出结果
cout << (st1 + 12 + 13 + (st2 + st2) + 23);
return 0;
}
5.4 类内重载
默认左边是类的类型
#include <iostream>
using namespace std;
class CStu
{
public:
int nAge;
CStu()
{
nAge = 12;
}
// 类内重载
int operator+(int a)
{
return (this->nAge + a);
}
};
// 类外重载
int operator+(int a, CStu& st)
{
return (a - st.nAge);
}
int main()
{
CStu st;
cout << (st + 12) << endl;
cout << (13 + st) << endl;
return 0;
}
6 - 常见运算符
算术运算符
+ ,- ,* ,/ ,%
关系运算符
>= ,<= ,> ,< ,!= ,==
位运算符
>^ ,& ,I
逻辑运算符
>&&,||
6.1 二元运算符重载
#include <iostream>
using namespace std;
class CStu
{
public:
int nAge;
CStu(int age)
{
nAge = age;
}
int operator >= (CStu& st2)
{
return (nAge >= st2.nAge);
}
int operator & (CStu& st2)
{
return (nAge & st2.nAge);
}
int operator && ( CStu& st2)
{
return (nAge && st2.nAge);
}
};
int operator += (CStu& st1, CStu& st2)
{
return (st1.nAge && st2.nAge);
}
int main()
{
CStu st1(14); //st1.nage
CStu st2(13);
cout << (st1 >= st2);
cout << (st1 && st2);
system("pause");
return 0;
}
6.1 一元运算符重载
#include <iostream>
using namespace std;
class CStu
{
public:
int nAge;
CStu(int age)
{
nAge = age;
}
// 内重载
int operator-()
{
return (-nAge);
}
};
// 外重载
int operator!(CStu& st)
{
return (!st.nAge);
}
int main()
{
CStu st1(14); //st1.nage
cout << (!st1);
system("pause");
return 0;
}
6.3 输出运算符重载
特点:
- cout是一个对象
- 参数1是ostream引用,参数2是对象的常引用
- 返回值保证连续输出
- 必须是类外
- 类友元
#include <iostream>
using namespace std;
class CStu
{
private:
int nAge;
public:
CStu()
{
nAge = 12;
}
// 定义友元函数
friend ostream& operator << (ostream& os, const CStu& st);
};
ostream& operator << (ostream& os, const CStu& st)
{
os << st.nAge;
return os;
}
int main()
{
// 创建对象
CStu st;
// 输出对象
cout << st << st << "kobe";
return 0;
}
6.4 输入运算符重载
代码示例
#include <iostream>
using namespace std;
class CStu
{
private:
int nAge;
double dbHeight;
public:
CStu()
{
nAge = 0;
dbHeight = 0.0;
}
void Show()
{
cout << nAge << endl;
cout << dbHeight << endl;
}
friend istream& operator >> (istream& ist, CStu& st);
};
istream& operator >> (istream& ist, CStu& st)
{
ist >> st.nAge >> st.dbHeight;
// 需要输入检测是否失败
if (ist.fail())
{
st.nAge = 0;
st.dbHeight = 0;
}
return ist;
}
int main()
{
// 创建st对象
CStu st;
cin >> st;
st.Show();
return 0;
}
6.5 赋值运算符重载
#include <iostream>
using namespace std;
class CStu
{
private:
int nAge;
double dbHeight;
public:
CStu()
{
nAge = 0;
dbHeight = 0.0;
}
int operator=(int a)
{
nAge = a;
return nAge;
}
int& operator+=( int a)
{
nAge = nAge + a;
return nAge;
}
};
/*
// 类外重载
int& operator+=(CStu& st1, int a)
{
st1.nAge = st1.nAge + a;
return st1.nAge;
}
*/
int main()
{
// 创建对象
CStu st;
int a = 111;
cout << (st += a += 113);
return 0;
}
6.6 下标运算符重载
#include <iostream>
using namespace std;
// 下标运算符:[ ]特点 返回引用 只能类内
class CStu
{
public:
int a;
int b;
int c;
double d;
int nError;
CStu()
{
a = 12;
b = 23;
c = 34;
d = 45;
nError = -1;
}
//返回指针
int& operator[](int n)
{
switch(n)
{
case 0:
return &a;
case 1:
return &d;
}
return &nError;
}
};
int main()
{
CStu st; //st[1]
cout << *(int *)st[0];
cout << *(double *)st[1]; // void 没有确定的大小 // ==》void* ==》
*(double *)st[1] = 15;
cout << *(double *)st[1];
return 0;
}
6.7 自加自减运算符重载
#include <iostream>
using namespace std;
class CStu
{
public:
int nAge;
CStu()
{
nAge = 12;
}
int operator++()
{
nAge += 1;
return nAge;
}
int operator--()
{
this->nAge -= 1;
return nAge;
}
int operator++(int n) //int n 理解成一个标记
{
n = nAge; //
nAge += 1;
return n;
}
};
/*
// 类外++
int operator++(CStu& st)
{
st.nAge += 1;
return st.nAge;
}
*/
/*
// 类外 --
int operator--(CStu& st)
{
st.nAge -= 1;
return st.nAge;
}
*/
// 后置 ++
int operator++(CStu& st, int n) //int n 理解成一个标记
{
n = st.nAge; //
st.nAge += 1;
return n;
}
// 后置 --
int operator--(CStu& st, int n) //int n 理解成一个标记
{
n = st.nAge; //
st.nAge -= 1;
return n;
}
int main()
{
int a = 12;
//int b = ++a; // b == 13
//a++;
int b = a++; //b == 12, a == 13
CStu st;
//cout << ++st; //13
// cout << --st; //12
cout << st--;
cout << st.nAge;
return 0;
}
7- 重载类型转换
基本特点
- 没有显示返回类型,但是要写返回值。
- 没有参数。必须定义成类的成员函数。
- 不应该改变对象的内容,所以是const函数。避免过度使用。
#include <iostream>
using namespace std;
class CStu
{
public:
int a;
double b;
CStu()
{
a = 13;
b = 12.12;
}
// 类型转换没有返回值
operator int() const
{
return a;
}
operator double() const
{
return b;
}
};
int main()
{
// 创建对象
CStu st;
cout << int(st) << endl;
cout << (double)st << st.b << endl;
return 0;
}