运算符重载:
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载: 作用:实现两个自定义数据类型相加的运算
1.成员函数实现 + 号运算符重载
#include <iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
//1 成员函数实现 + 号运算符重载
Person operator+(Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
cout << "p3.m_A = " << p3.m_A<< endl;
cout << "p3.m_B = " << p3.m_B<< endl;
}
int main() {
test01();
system("pause");
return 0;
}
2.全局函数重载:
class Person
{
public:
//1 成员函数实现 + 号运算符重载
/*Person operator+(Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}*/
int m_A;
int m_B;
};
//2.全局函数重载+号
Person operator+(Person& p1, const Person& p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//3 运算符重载 可以发生函数重载
Person operator+(Person& p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数方式
Person p3 = p2 + p1; //相当于 p2.operaor+(p1)
Person p3 = p1 + p2;
//全局函数重载本质调用:
Person p3 = operator+(p1, p2);
Person p3 = p1 + p2;
//
cout << "p3.m_A = " << p3.m_A<< endl;
cout << "p3.m_B = " << p3.m_B<< endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2.左移运算符重载
作用:可以输出自定义数据类型
#include <iostream>
using namespace std;
//左移运算符重载
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
//成员函数 实现不了 p << cout 不是我们想要的效果
//void operator<<(Person& p){
//}
int m_A;
int m_B;
};
//全局函数实现左移重载
ostream& operator<<(ostream& cout, Person &p) //本质 operator<<(cout,p) 简化cout<<p
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
return cout;
}
void test01()
{
Person p(10,10);
//p.m_A = 10;
//p.m_B = 10;
cout << p<<"hello world"<<endl; //链式编程 输出一个对象就能打印他的属性
}
int main()
{
test01();
system("pause");
return 0;
}
总结:重载左移运算符配合友元可以实现输出自定义数据类型
3.递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据
总结:前置递增返回引用,后置递增返回值
先写一个输出整形数据的:
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num << endl; //全局函数想要访问私有属性
return cout;
}
void test01()
{
MyInteger myint;
cout << myint << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出结果: 0
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
//重载前置++运算符 返回引用为了一直对一个数据进行递增操作
MyInteger& operator++()
{
//先++
m_Num++;
//再返回
return *this;
}
//重载后置++运算符
//int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int)
{
//先记录当时结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num << endl; //全局函数想要访问私有属性
return cout;
}
void test01()
{
MyInteger myint;
cout << ++myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}