c++学习(运算符重载)

运算符重载

1.加号运算符重载

#include<iostream>
#include<string>
using namespace std;
class persion{
public:
  persion(){
    m_a=10;
    m_b=20;
  }
  //1.成员函数实现运算符重载
  // persion operator+(persion &p){     
  //   persion temp;
  //   temp.m_a+=p.m_a;
  //   temp.m_b+=p.m_b;
  //   return temp;
  // }

  /*
   persion persionADDpersion(persion &p){运算符重载的实质就是成员函数名的更换
    persion temp;
    temp.m_a+=p.m_a;
    temp.m_b+=p.m_b;
    return temp;
  }
  */

  int m_a;
  int m_b;
};

// 2.全局函数的运算符重载
persion operator+(persion p1,persion p2){
  persion temp;
  temp.m_a=p1.m_a+p2.m_a;
  temp.m_b=p1.m_b+p2.m_b;
  return temp;
}

persion operator+(persion p1,int num){
  persion temp;
  temp.m_a=p1.m_a+num;
  temp.m_b=p1.m_b+num;
  return temp;
}

int main()
{
  persion p1;
  persion p2;
  persion p3=p1+p2;

  //成员函数实现运算符重载的实质
  //persion p3=p1.operator+(p2);

  //全局函数实现运算符重载的实质
  //persion p3=operator+(p1,p2);

  //运算符重载实质就是函数,因此可以发生函数重载
  persion p4=p1+100;

}



2.左移运算符重载

#include<iostream>
#include<string>
using namespace std;
class persion{
  friend ostream& operator<<(ostream& cout,persion &p1);//采用友元的方式访问私有权限数据
public:
  persion(){
    m_a=10;
    m_b=20;
  }

private:
  int m_a;
  int m_b;
};
//只能利用全局函数实现左移运算符的重载
ostream& operator<<(ostream& cout,persion &p1){//由于ostream这个类对象只能有一个,
                                                //不能创建一个新的对象,因此全部采用引用的方式来进行传递
      cout<<p1.m_a<<" "<<p1.m_b;
      return cout;
}

int main()
{
  persion p1;
  cout<<p1<<endl;

}



3递增运算符重载

#include<iostream>
using namespace std;


class MyInteger{
	friend ostream& operator<<(ostream& cout, MyInteger myinteger);
public:
	MyInteger() {
		m_a = 0;
	}
	//前置运算符重载
	MyInteger& operator++() {
		m_a++;
		return *this;
	}

	//后置运算符重载
	MyInteger operator++(int) {//利用int占位符来区分前置和后置,而且只能用int来区分
		
		//先存储之前的结果
		MyInteger temp;
		temp = *this;//*this就是其本身
		//++
		m_a++;
		//返回之前的结果
		return temp;
	}

private:
	int m_a;	
};

ostream& operator<<(ostream& cout, MyInteger myinteger) {
	cout << myinteger.m_a;
	return cout;
}

int main() {
	MyInteger a;
	
	cout << a << endl;
	cout << ++a << endl;
	cout << a++ << endl;
	cout << a << endl;


}

4赋值运算符重载(深拷贝,浅拷贝问题)

#include<iostream>
using namespace std;

class persion {

public:
	persion(int age) {
		m_age = new int(age);
	}

	~persion() {
		delete m_age;
	}
	persion& operator=(persion& p){//返回值为persion&,实现程序可以连=操作,
		//先判断是否有数据开辟在堆空间
		if (m_age != NULL) {
			delete m_age;
		}
		m_age = new int(*p.m_age);
		return *this;
	}
	int* m_age;
};


int main() {
	persion p1(10);
	persion p2(20);
	persion p3(30);



	p3=p2 = p1;
	cout << *p1.m_age << endl;
	cout << *p2.m_age << endl;
	cout << *p3.m_age << endl;


}

关系运算符重载和上述差不多

函数调用重载(仿函数)

#include<iostream>
using namespace std;

class myprintf {
public:
	void operator()(string str) {
		cout << str << endl;
	}
};
class myadd {
public:
	int operator()(int num1,int num2) {
		return num1 + num2;
	}
};


int main() {
	myadd madd;
	cout << madd(10, 20) << endl;//仿函数和普通函数调用差不多

	cout << myadd()(30, 40) << endl;//匿名函数调用,匿名函数对象创建(使用完一次就释放内存,对象不存在)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值