C++ 之函数重载

一 函数重载
函数重载写法有两种
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值