C++运算符重载
什么是运算符重载
赋予运算符具有操作自定义类型数据功能
运算符重载的实质是什么?
运算符重载的实质本身就是函数调用
运算符重载函数的写法
函数返回值 函数名(函数参数)
函数返回值 :运算完成后的值决定的 Complex
函数名 : operator 加上重载运算符组成函数名 operator+
参数 :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
函数体 : 写运算符具体想要的操作
友元函数重载运算符
类成员函数重载运算符
<< 运算符重载就是在 成员函数或者友元函数 中实现 你想在外面对 类对象实现的操作。
#include <iostream>
using namespace std;
class overload
{
public:
// 构造函数
overload(int a = 0, int b = 0) :a(a), b(b) {}
void print()
{
cout << a <<" " << b << endl;
}
friend overload operator+ (overload one, overload two);
//类成员函数重载,参数个数等于操作减一
bool operator> (overload object)
{
if (this->a > object.a)
{
return true;
}
else if (this->a == object.a && this->b > object.b)
{
return true;
}
else
{
return false;
}
}
protected:
int a;
int b;
};
//友元重载: 参数个数就是操作数据
overload operator+ (overload one, overload two)
{
// 在函数中实现 对象的成员相加
return overload(one.a + two.a, one.b + two.b);
}
int main()
{
overload one(3, 3);
overload two(2, 7);
overload three;
three = one + two; //overload 重载函数的隐式调用
three.print(); // 3+2 3+7
//显式调用
overload result;
result = operator+(one+one, two+one);// 3+3 3+3, 3+2 3+7
result.print(); // 6+5,4+10
if (one > two) //one > two 是bool
{
cout << "one 比较大" << endl;
}
//对象可以表示一个数据,所以参数应该少一个
if (one.operator>(two))
{
cout << "one比较大" << endl;
}
return 0;
}
特殊运算符重载
- 流运算符重载
- cin类型 : istream类的对象
- cout类型:ostream类的对象
- 流运算符 >> <<
必须采用友元函数形式重载
- 他运算符
- = () -> [] 只能采用类的成员函数形式重载
- 流重载采用友元方式
+. .* ?: :: 不能重载
#include <iostream>
#include <string>
using namespace std;
class Boy
{
public:
// constraction fun
Boy(string name = "", int age = 18) :name(name), age(age) {}
friend istream& operator>>(istream& in, Boy& mm);
friend ostream& operator<<(ostream& out, Boy& mm);
protected:
string name;
int age;
};
istream& operator>>(istream& in, Boy& mm)
{
in >> mm.name >> mm.age;
return in;
}
ostream& operator<<(ostream& out, Boy& mm)
{
out << mm.name << "\t" << mm.age << endl;
return out;
}
int main()
{
string str;
cout << "inter a string:";
cin >> str;
cout << str << endl;
Boy bb; // 创建对象
// 对对象直接输入 输出
cout << "inter a person name and age:";
cin >> bb; //void operator>>(istream& in,Boy& bb)
cout <<bb; //void operator<<(ostream& out,Boy& bb)
cout << "inter a string and a name age:";
cin >> str >> bb;
cout << str << endl << bb;
return 0;
}
+ ++ --运算符重载
- 问题: 前置和后置怎么区分?
- 解决办法:
++ 增加无用参数 int去表示当前运算符重载是后置操作
#include <iostream>
#include <string>
using namespace std;
class Frend
{
public:
Frend(string name, int age) :name(name), age(age) {}
friend ostream& operator<<(ostream& out, Frend& object)
{
out << object.name << "\t" << object.age << endl;
return out;
}
Frend operator++(int) //需要一个无用参数 充当标记
{
int num = age;
age++;
return Frend(name, num);
//上面三行等效下面一行
//return MM(name,age++);
}
Frend operator++()
{
return Frend(name, ++age);
}
//类的对象的隐式转换 operator
operator int()
{
return age;
}
protected:
string name;
int age;
};
int main()
{
Frend fr("威少", 19);
cout << fr << endl;
int num = 1;
int result = num++; //result=1 num=2
cout << result << "\t" << num << endl;
result = ++num; //result=3 num=3
cout << result << "\t" << num << endl;
// object 由 fr 来初始化
Frend object = fr++;
cout << "Frend object = fr++;" << object << fr; //age=18 fr: 19
object = ++fr; //age:20 fr: 20
cout <<"object = ++fr;"<< object << fr;
//this_thread::sleep_for(3s);
cout << "3s结束" << endl;
Frend boy("weishao", 20);
int boyAge = boy; //类的对象的隐式转换 operator
cout << boyAge << endl;
return 0;
}
- 文本重载 (新标准中的,稍微落后一点开发工具不适用)
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
//文本重载
unsigned long long operator"" _h(unsigned long long num)
{
return 60 * 60 * num;
}
unsigned long long operator"" _min(unsigned long long num)
{
return 60 * num;
}
int main()
{
//this_thread::sleep_for(7s);
cout << "7s结束" << endl;
int second = 1_h;
cout << "小时数:1";
cout << second << "s" << endl;
cout << "1_h + 18_min + 30=";
int sum = 1_h + 18_min + 30;
cout << sum << "s" << endl;
return 0;
}
重载实战: 封装一个Array类,实现定长数组的操作
#include <iostream>
using namespace std;
class Array
{
public:
Array() {}
Array(int arraySize)
{
this->arraySize = arraySize;
memory = new int[arraySize];
}
int& operator[](int index)
{
return memory[index];
}
int size()
{
return arraySize;
}
friend ostream& operator<<(ostream& out, Array& object)
{
for (int i = 0; i < object.size(); i++)
{
out << object.memory[i] << " ";
}
out << endl;
return out;
}
friend istream& operator>>(istream& in, Array& object)
{
for (int i = 0; i < object.size(); i++)
{
in >> object.memory[i];
}
return in;
}
Array& operator+(Array& object)
{
Array* temp=new Array(this->size() + object.size());
int count = 0;
for (int i = 0; i < this->size(); i++)
{
temp->memory[count++] = this->memory[i];
}
for (int i = 0; i < object.size(); i++)
{
temp->memory[count++] = object.memory[i];
}
return *temp;
}
Array(Array& object)
{
this->arraySize = object.arraySize;
this->memory = new int[this->arraySize];
for (int i = 0; i < object.size(); i++)
{
this->memory[i] = object.memory[i];
}
}
void operator=(Array& object)
{
this->arraySize = object.arraySize;
this->memory = new int[this->arraySize];
for (int i = 0; i < object.size(); i++)
{
this->memory[i] = object.memory[i];
}
}
~Array()
{
delete[] memory;
}
protected:
int arraySize;
int* memory; //memory[i]
};
int main()
{
//以下测试代码要能够成功运行
Array array(4);
for (int i = 0; i < array.size(); i++)
{
cin >> array[i]; //array[i] 最终返回一个memory[i]
}
for (int i = 0; i < array.size(); i++)
{
cout << array[i]; array[i] 最终返回一个memory[i]
}
cout << endl;
//实现数组的连接
Array one(3); //输入1 2 3
cin >> one;
Array two(4); //输入2 3 4 5
cin >> two;
Array sum = one+two;
cout << sum ; //打印1 2 3 2 3 4
Array num;
num = sum;
cout << num ;
return 0;
}