c++:利用成员函数和全局函数实现对运算符的重载

1、首先,我们要明确成员函数与全局函数的区别

***简单来说,全局变量可以在整个程序里使用;而局部变量只可以在一个函数或一段代码中使用。

(1)类成员函数是面向对象,全局函数是面向过程

(2)类成员函数 => 全局函数:增加一个参数,增加的这个参数代替this指针

(3)全局函数 => 类成员函数:减少一个参数,减少的这个参数由this指针隐藏

2、其次,明确运算符重载的概念

***重载,就是赋予新的含义。函数重载可以让一个函数名有多重功能,在不同情况下进行不同的操作

格式如下:

返回值类型 operator 运算符名称 (形参表列)

{
}

例如:Complex operator +(Complex &c);   //Complex是返回值类型 ;‘+’是运算符;括号内是形参列表

注意:部分运算符不能重载

成员选择符        域解析运算符::            对象选择符.*             条件运算符: ?            长度运算符sizeof

3、利用成员函数实现运算符的重载

*在这里实现 ‘+’ 运算符和 ‘<<’ 运算符的重载。

值得注意的是,‘+’ 用全局函数或成员函数都能实现重载;但 ‘<<’ 只能用全局函数实现重载

#include<iostream>
using namespace std; 

class Complex
{
    friend Complex operator +(Complex &c1 , Complex &c2);    //友元函数
    friend ostream &operator <<(ostream &out, Complex &c);
private:
    int m_a;
    int m_b;
public:
    Complex(int a,int b);         //有参构造函数
    Complex();                    //无参构造函数
    void print();                 //输出复数函数
};

Complex::Complex()
{
    m_a = 1;
    m_b = 1;
}

Complex::Complex(int a,int b)
{
    m_a=a;
    m_b=b;
}


//利用全局函数来重载 +
Complex operator +(Complex &c1 , Complex &c2)
{
    Complex tmp;          //不能返回引用 只能返回对象
    
    tmp.m_a = c1.m_a + c2.m_a;       //不能直接用私有成员变量 使用友元函数
    tmp.m_b = c1.m_b + c2.m_b;       
    
    return tmp;
}

//利用全局函数来重载 <<
ostream &operator <<(ostream &out, Complex &c)    //全局函数实现<<重载  一定返回引用
{
    out<< c.m_a << "+" <<c.m_b << "i";
    return out;
}


void Complex::print()
{
    cout<< m_a << " + " << m_b << "i" <<endl;
}    

int main()
{
    Complex c1(1,2);
    Complex c2(3,4);
    Complex c3;
    
    
    operator +(c1,c2);                //与下面的 c1+c2 等价 
    ( operator +(c1,c2) ).print();
    cout<< "-------------" <<endl;
    
    c1+c2;
    (c1+c2).print();
    cout<< "-------------" <<endl;
    
    
    c3 = c1+c2;
    c3.print();
    cout<< "--------------" <<endl;
    
    return 0;
}

编译结果如下:

4、利用成员函数实现运算符的重载

#include<iostream>
using namespace std; 

class Complex
{
    friend ostream &operator <<(ostream &out,const Complex &c);
private:	
	int m_a;
	int m_b;
public:
	Complex(int a,int b);
	Complex();
	void print();
	Complex operator +(Complex &c);          //成员函数
	ostream &operator <<(ostream &out);
};

Complex::Complex()
{
	m_a = 1;
	m_b = 1;
}

Complex::Complex(int a,int b)
{
	m_a=a;
	m_b=b;
}

//main函数里面需要用到 '<<' 的重载
ostream &operator <<(ostream &out,const Complex &c)    //成员函数
{
	out<< c.m_a << " + " <<c.m_b << "i";
	return out;
}

//成员函数实现 '+' 重载   比全局函数少一个参数
Complex Complex::operator +(Complex &c)   
{
	Complex tmp;
	tmp.m_a = this->m_a + c.m_a;       //返回对象本身 而不是返回引用 因为 + 不能作为左值
	tmp.m_b = this->m_b + c.m_b;       //若保持c1 c2不变 需要放入另一个tmp里
	return tmp;  

}

/*
//利用成员函数实现 '+' 的重载 ,有两种方式
Complex Complex::operator +(Complex &c)   
{
	this->m_a = this->m_a + c.m_a;     //返回对象本身 而不是返回引用 因为 + 不能作为左值
	this->m_b = this->m_b + c.m_b;     //此时值都存放于c1中 
	return *this;
} 
*/

/*
//因为不能正常编译通过   所以 << 不能通过成员函数重载
ostream &Complex::operator <<(ostream &out)  
{
	out<< this->m_a << " + " <<this->m_b << "i";
	return out;
}
*/

void Complex::print()
{
	cout<< m_a << " + " << m_b << "i" <<endl;
}	

int main()
{
	Complex c1(1,2);
	Complex c2(3,4);
	Complex c3;
	
	c1.operator + (c2);        //返回c1里面 变量-->c2
	c1.print();
	cout<< "-------------" <<endl;
	
	c3 = c1+c2;
	cout<< c3 <<endl;          //cout一般输出int型、字符串型等,不能输出对象,所以对'<<'重载
    cout<< "------------------" <<endl;

    /*
    //如果用成员函数进行对 << 的重载,就无法编译通过

	c3.operator <<( cout);    //这里强制用函数把cout作为参数,仅仅编译这句可以实现输出

	cout << c3 <<endl;        //编译这句时,就不能编译通过

	cout<< "----------------" <<endl;
    */
	
	c1+c2 = c3;
	c1.print();               //在成员函数中利用tmp可以保持c1 c2的初始值不变
	
	return 0;
}

编译结果如下:

5、注意看代码里的注释

猜你喜欢

转载自blog.csdn.net/xutong98/article/details/81219709