类运算符重载+友元函数(二)

有元函数简单学习
1. C++类私有部分访问的方法:类公有方法、有元(有元函数、友元类和有元成员函数)。

2. 运算符重载
   class Test{...};
   Test t, t1, t2;
   // --------------- 两个类对象的 + - * / % 等的运算符重载 -------------

   t = t1 + t2; // t = t1.operator+(t2); 原型为: 
                   Test operator+(const Test &t); <=>Test operator+(const Test *this, const Test &t);
   t = t1*t2;  // t = t1.operator*t2;

   // ------------------- 类对象和“非对象”的预算符重载-----------------
   
   t = t1*10; // t = t1.operator*(10); 左侧的操作数为调用对象,运算符重载的是成员函数--->原型为 Time operator*(const);
   t = 10*t1; // 左侧操作数为非对象怎么办???可采用非成员函数--->原型为 Time operator*(int i, const Test &t);
                 于是:t = operator*(10, t1);

   B U T 非成员函数不能调用私有数据,但friend函数可以----->friend Time operator*(int i, const Test &t);
   但是int i 可能不需要访问私有数据!看下面输出运算符重载:

   ostream& operator<<(ostream & out, const Test &t);
   其中,out为类输出流out对象,但还不是Test类对象,因此还不能直接访问Test类私有部分,怎么解决?
   --->声明为有元函数:friend ostream& operator<<(ostream & out, const Test &t);
   另外,输入运算符重载类似!

3. 重载运算符: 作为成员函数还是非成员函数
   Notes:① 非成员函数应该为有元函数,这样才能直接访问类的私有数据,原型比如:
          Test operator+(const Test &t) const; // 成员版本 <=> Test operator+(const Test *this, const Test &t);
		                                          一个操作数通过this指针隐式传递。
		  friend Test operator+(const Test &t1, const Test &t2) const; // 非成员版本,有元函数,无this指针,操作数显示标出。
		  ② 使用有元函数,须现在类内声明。


 
 

#include <iostream>
using namespace std;

/* 需要声明吗?有无此声明均可以,为什么?C语言如果main函数放在前面就需要函数声明!
class Complex;
Complex operator+(const Complex &c);
ostream& operator<<(ostream &out, const Complex &c);
istream& operator>>(istream &in, Complex &c);
Complex operator+(int i, const Complex &c);*/

// -------------------------------- 复数类 -----------------------------------------------------
class Complex
{
	/* 常用的友元:重载<<运算符(这是<<重载的第一种版本,返回输出流ostream类对象的引用),第二种版本参看C++ Premier Plus P393*/
	friend ostream& operator<<(ostream &out, const Complex &c);
	friend istream& operator>>(istream &in, Complex &c); // 友元非成员函数-->单纯友元函数
	friend Complex operator+(int i, const Complex &c);// 友元--->成员函数
public:
	Complex(int d = 0) :m_real(d), m_imag(d) {}
	Complex(int real, int imag)
	{
		m_real = real;
		m_imag = imag;
	}

	Complex operator+(const Complex &c)
	{
		return Complex(m_real + c.m_real, m_imag + c.m_imag);
	}

	/* c = 1 + c1; 
	Complex operator+(int i, const Complex &c) 提示参数太多?为什么在类外实现无此提示?
	它是友元函数,但也是类成员函数!
	Complex operator+(int i, const Complex &c)
	{
		return Complex(i + c.m_real, c.m_imag);
	}*/

	~Complex(){}
private:
	int m_real;
	int m_imag;
};
// -------------------------------- 方法实现 ---------------------------------------------------
// ostream 输出流,声明它为类友元函数,以访问类私有部分
// 友元函数也可放在类内实现,但为什么放在类内就提示operator<<(ostream &out, const Complex &c)中参数过多?
ostream& operator<<(ostream &out, const Complex &c)// 输出-->引用:提高效率;const:保护c不被修改
{
	out << "(" << c.m_real << "," << c.m_imag << ")" << endl;
	return out;
}

istream& operator>>(istream &in, Complex &c) // 就是要输入(修改)类对象c,所以不用const,只用引用提高效率
{
	in >> c.m_real >> c.m_imag;
	return in;
}

/* 放在类外怎么实现?参数列表是需要多加吗?
Complex operator+(const Complex &c)
{
	return Complex(m_real + c.m_real, m_imag + c.m_imag);
}*/

// 有元成员函数方法实现
Complex operator+(int i, const Complex &c)
{
	return Complex(i + c.m_real, c.m_imag);
}
// -------------------------------- 主函数 -----------------------------------------------------
void main()
{
	Complex c, c_input;
	Complex c1(1, 2);
	Complex c2(2, 3);

	// c = c1.operator+(c2)
	c = c1 + c2; // 类对象相加,须进行运算符重载
	// operator<<(cout, c)
	cout << "两类对象相加 "<< c << endl;// 输出对象,须进行输出运算符重载

	c = 1 + c1; // 
	cout << "数字和类对象相加,数字在前" << c << endl;

	// operator>>(cin, c_input)
	cin >> c_input;// 输入类对象,须进行输入预算符重载
	cout << c_input << endl;
	/* 思考:重载预算符参数列表个数!*/
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值