一 函数重载
函数重载写法有两种
1、类成员函数重载
2、类全局函数重载
类成员函数重载 转到 类全局函数重载 参数增加一个
类全局函数重载 转到 类成员函数重载 参数减少一个
#include<iostream>
using namespace std;
class Complex
{
private:
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
friend Complex operator+(Complex & c1, Complex &c2);
friend Complex & operator++(Complex & c1);
friend Complex operator++(Complex & c1, int);
//成员函数方法
Complex operator-(Complex & c2)
{
Complex temp(this->a - c2.a,this->b-c2.b);
return temp;
}
//前置--
Complex & operator--()
{
this->a--;
this->b--;
return *this;
}
Complex operator--(int)
{
Complex temp = *this;
this->a--;
this->b--;
return temp;
}
};
//全局函数法实现+运算符重载
Complex operator+(Complex & c1, Complex &c2)
{
Complex temp(c1.a + c2.a,c1.b+c2.b);
return temp;
}
Complex & operator++(Complex & c1)
{
c1.a++;
c1.b++;
return c1;
}
Complex operator++(Complex & c1,int) //后置++用占位符
{
Complex temp = c1;
// return c1;
c1.a++;
c1.b++;
return temp;
}
int main121()
{
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
c3.printCom();
Complex c4 = c1-c2 ;
c4.printCom();
//前置++ 全局实现重载
++c1;
c1.printCom();
// 前置--,成员函数实现重载
--c2;
c2.printCom();
//后置-- 全局实现重载
c1++;
c1.printCom();
//后置-- 成员函数实现重载
c2--;
c2.printCom();
cout << "hello" << endl;
while (1);
return 0;
}
二、重载的类成员函数正规写法
#include<iostream>
using namespace std;
class Complex
{
private:
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
//成员函数方法
// 重载 -
Complex operator-(Complex & c2)
{
Complex temp(this->a - c2.a, this->b - c2.b);
return temp;
}
//前置--
Complex & operator--()
{
this->a--;
this->b--;
return *this;
}
//后置--
Complex operator--(int)
{
Complex temp = *this;
this->a--;
this->b--;
return temp;
}
//+运算符重载
Complex operator+( Complex &c2)
{
Complex temp(a + c2.a, b + c2.b);
return temp;
}
//前置++
Complex & operator++( )
{
a++;
b++;
return *this;
}
//后置++
Complex operator++(int) //后置++用占位符
{
Complex temp = *this;
// return c1;
a++;
b++;
return temp;
}
};
int main()
{
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
c3.printCom();
Complex c4 = c1 - c2;
c4.printCom();
//前置++ 全局实现重载
++c1;
c1.printCom();
// 前置--,成员函数实现重载
--c2;
c2.printCom();
//后置-- 全局实现重载
c1++;
c1.printCom();
//后置-- 成员函数实现重载
c2--;
c2.printCom();
cout << "hello" << endl;
while (1);
return 0;
}
三、全局成员函数重载应用场景
istream 和 ostream是 C++的预定义流类
cin 是istream的对象,cout 是ostream的对象
运算符<< 由ostream重载为插入操作,用于输出基本类型数据
运算符 >> 由istream重载为提取操作,用于输入基本类型数据
用友元函数重载 >>和<< ,输入和输出用户自定义的数据类型
#include<iostream>
using namespace std;
class Complex
{
private:
int a;
int b;
friend void operator<<(ostream & out, Complex &c1);
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
};
//成员函数方法
//全局函数重写<<
void operator<<(ostream & out, Complex &c1)
{
out << "<<被重载:" << endl;
out << c1.a << "+" << c1.b << "i" << endl;
}
int main()
{
Complex c1(1, 2), c2(3, 4);
//重载 <<
cout << c1;
cout << "hello" << endl;
while (1);
return 0;
}