C++操作符重载

C++中基础数据类型编译器已经知道了如何进行加减这些运算,但是对于类、用户自定义数据类型这些数据类型 C++编译器 是不知道如何进行运算。对此,c++编译器给我们程序员提供了一种机制,让自定义数据类型 有机会 进行 运算符操作 ====> 运算符重载机制
运算符重载的本质是函数调用

class Complex
{
public:
	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;
	}
};

对于上述类,重载+和-操作符,有两种方法:

//方法1:
//全局函数法 实现 + 运算符重载
class Complex
{
public:
	//全局函数 重载+运算符
	friend Complex operator+(Complex &c1, Complex &c2);
}
Complex operator+(Complex &c1, Complex &c2)
{
	Complex tmp(c1.a + c2.a, c1.b + c2.b);
	return tmp;
}
//方法2:
class Complex
{
public:
//成员函数法 实现 -运算符重载
	 Complex operator-(Complex &c2)
	{
		Complex tmp(this->a - c2.a, this->b - c2.b);
		return tmp;
	}
}

常用的操作符重载代码如下:
需要注意,重载操作符时,函数参数的第一个参数是操作符的左操作数,第二个参数是操作符的右操作数

class Complex
{
private:
	int a;
	int b;
	//friend void operator<<(ostream &out, Complex &c1);
	friend ostream& 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;
	}
public:
	//实现 + 运算符重载
	Complex operator+(Complex &c2)
	{
		Complex tmp(a + c2.a, b + c2.b);
		return tmp;
	}
	//前置++
	Complex& operator++()
	{
		a++;
		b++;
		return *this;
	}
	//后置++
	Complex operator++(int)
	{
		//先使用 在让c1加加
		Complex tmp = *this;
		this->a ++;
		this->b ++;
		return tmp;
	}
	//成员函数法 实现 -运算符重载
	 Complex operator-(Complex &c2)
	{
		Complex tmp(this->a - c2.a, this->b - c2.b);
		return tmp;
	}
	 //前置--
	Complex& operator--()
	{
		this->a --;
		this->b --;
		return *this;
	}
	//后置--
	Complex operator--(int)
	{
		Complex tmp = *this;
		this->a--;
		this->b--;
		return tmp;
	}
};
void main31()
{
	Complex c1(1, 2), c2(3, 4);
	//1	全局函数法 实现 + 运算符重载
	// Complex operator+(Complex &c1, Complex &c2);
	Complex c3 = c1 + c2;
	c3.printCom();
	//2 成员函数 法 实现 -运算符重载
	//c1.operator-(c2);
	//Complex operator-(Complex &c2)
	Complex c4 = c1 - c2;
	c4.printCom();
	//前置++操作符 用全局函数实现
	++c1;
	c1.printCom();
	//前置--操作符 成员函数方法
	--c1;
	c1.printCom();
	//Complex& operator++(Complex &c1)
	//c1.operator--();
	//后置++操作符 用全局函数实现
	c1++;
	c1.printCom();
	//后置--操作符 用成员函数实现
	c1--;
	c1.printCom();
	//c1.operator--()
	cout<<"hello..."<<endl;
	system("pause");
	return ;
}
/*
void operator<<(ostream &out, Complex &c1)
{
	out<<c1.a << " + " << c1.b << "i" << endl;
}
*/
ostream& operator<<(ostream &out, Complex &c1)
{
	out<<c1.a << " + " << c1.b << "i" << endl;
	return out;
}
void main()
{
	int a = 10;
	Complex c1(1, 2), c2(3, 4);
	cout<<a<<endl; //按照数据类型 
	//1
	cout << c1 ;
	//2 ostream 类中 添加 成员函数 .operator<<
	//ostream
	//cout.operator<<(c1);
	//2 函数返回值当左值 需要返回一个引用
	cout << c1  << "aaddddd";
	//cout.operator<<(c1) .operator<<("aaddddd");
	//void.operator<<("aaddddd");
	system("pause");
}
//类中如果有char类型的指针,则重载=操作符时需要注意浅拷贝问题
class  Name
{
public:
	Name(const char *myp)
	{
		m_len = strlen(myp);
		m_p =(char *) malloc(m_len + 1); //
		strcpy(m_p, myp);
	}

	//Name obj2 = obj1;
	//解决方案: 手工的编写拷贝构造函数 使用深copy
	Name(const Name& obj1)
	{
		m_len = obj1.m_len;
		m_p = (char *)malloc(m_len + 1);
		strcpy(m_p, obj1.m_p);
	}

	//obj3 = obj1;  // C++编译器提供的 等号操作 也属 浅拷贝
	//obj3.operator=(obj1)

	Name& operator=(Name &obj1)
	{
		//先释放旧的内存
		if (this->m_p != NULL)
		{
			delete[] m_p;
			m_len = 0;
		}
		//2 根据obj1分配内存大小
		this->m_len = obj1.m_len;
		this->m_p = new char [m_len+1];
		
		//把obj1赋值
		strcpy(m_p, obj1.m_p);
		return *this;
	}
	
	~Name()
	{
		if (m_p != NULL)
		{
			free(m_p);
			m_p = NULL;
			m_len = 0;
		}
	}
protected:
private:
	char *m_p ;
	int m_len; 
};

//对象析构的时候 出现coredump
void objplaymain()
{
	Name obj1("abcdefg");
	Name obj2 = obj1;  //C++编译器提供的 默认的copy构造函数  浅拷贝
	Name obj3("obj3");

	obj3 = obj1;  // C++编译器提供的 等号操作 也属 浅拷贝
	//obj3.operator=(obj1)
	//operato=(Name &obj1)

	obj1 = obj2 = obj3;
	//obj2.operator=(obj3);
	//obj1 = void;
}

void main()
{
	objplaymain();
	cout<<"hello..."<<endl;
	system("pause");
	return ;
}
//注意:在C++中,在函数后面加上const关键字
void  OpVar( int a, int b) const   //==>void  OpVar(const Test *this, int a, int b)  
		 //==>void  OpVar( const Test *const this, int a, int b)  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值