C++_操作符重载

C++_操作符重载

1、类外实现:重载加法运算符、前加加运算符、后加加运算符、<<运算符
①前加加的时候返回引用代码效率会更高
注意:重载运算符时为了代码效率,重载函数的返回值有时候是返回引用类型,有时候则不能

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);  //重载加法
	friend Point& operator++(Point &p);            //重载后加加
	friend Point operator++(Point &p, int a);      //重载前加加
	friend ostream& operator<<(ostream &o, Point p);  //重载<<
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

ostream& operator<<(ostream &o, Point p)
{
	cout<<"("<<p.x<<", "<<p.y<<")";
	return o;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);

	Point sum = p1+p2;
	sum.printInfo();
	
	Point m, n;
	cout<<"begin"<<endl;
	m = ++p1;
	cout << "******************"<<endl;

	n = p1++;
	cout<<"end"<<endl;

	cout<<m<<" "<<n<<endl;

	return 0;
}

2、类内实现重载函数

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}

	Point operator+(Point &p)
	{
		cout<<"operator+"<<endl;
		Point n;
		n.x = this->x + p.x;
		n.y = this->y + p.y;
		return n;
	}
	
	/* Point p(1,2); ++p; */
	Point& operator++(void)
	{
		cout<<"operator++(void)"<<endl;
		this->x += 1;
		this->y += 1;
		return *this;
	}
	
	/* Point p(1,2); p++; */
	Point operator++(int a)
	{
		cout<<"operator++(int a)"<<endl;
		Point n;
		n = *this;
		this->x += 1;
		this->y += 1;
		return n;	
	}

	friend Point add(Point &p1, Point &p2);
	friend ostream& operator<<(ostream &o, Point p);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}


ostream& operator<<(ostream &o, Point p)
{
	cout<<"("<<p.x<<", "<<p.y<<")";
	return o;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);
	Point m, n;

	m = p1 + p2; /* m = p1.operator+(p2); */
	cout<<"add p1,p2 = "<<m<<endl;

	cout<<"begin"<<endl;
	m = ++p1;    /* m = p1.operator++(); */
	cout<<"m = "<<m<<" "<<"p1 = "<<p1<<endl;
	cout << "******************"<<endl;

	n = p1++;    /* m = p1.operator++(0); */
	cout<<"n = "<<n<<" "<<"p1 = "<<p1<<endl;
	cout<<"end"<<endl;

	return 0;
}

3、重载等于号


#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
	static int cnt;
	char *name;
	int age;
	char *work;

public:

	static int getCount(void); 

	Person() {//cout <<"Pserson()"<<endl;
		name = NULL;
		work = NULL;
		cnt++;
	}
	Person(char *name) 
	{
		//cout <<"Pserson(char *)"<<endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		this->work = NULL;
		cnt++;
	}

	Person(char *name, int age, char *work = "none") 
	{
		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
		this->age = age;

		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);

		this->work = new char[strlen(work) + 1];
		strcpy(this->work, work);
		cnt++;
	}

	Person(const Person &per) 
	{
		cout <<"Pserson(Person &)"<<endl;
		this->age = per.age;

		this->name = new char[strlen(per.name) + 1];
		strcpy(this->name, per.name);

		this->work = new char[strlen(per.work) + 1];
		strcpy(this->work, per.work);
		cnt++;
	}

	~Person()
	{
		cout << "~Person()"<<endl;
		if (this->name) {
			cout << "name = "<<name<<endl;
			delete this->name;
		}
		if (this->work) {
			cout << "work = "<<work<<endl;
			delete this->work;
		}
	}

	void setName(char *n)
	{
		name = n;
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	void printInfo(void) const
	{
		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
	}

	Person& operator=(const Person& p)
	{
		cout << "operator=(const Person& p)"<<endl;
	
		if (this == &p)
			return *this;
		this->age = p.age;

		if (this->name) {
			delete this->name;
		}
		if (this->work) {
			delete this->work;
		}

		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		
		this->work = new char[strlen(p.work) + 1];
		strcpy(this->work, p.work);

		return *this;
		
	}
	
};

int Person::cnt = 0; /* 定义和初始化 */

int Person::getCount(void) 
{ 
	return cnt; 
}

int main(int argc, char **argv)
{
	const Person p1("zhangsan", 10);

	cout<<"Person p2 = p1" <<endl;
	Person p2 = p1;  //会调用值拷贝构造函数
	
	Person p3;

	cout<<"p3=p1"<<endl;
	p3 = p1;  //会调用我们自己写的重载函数
	cout<<"end"<<endl;
	p1.printInfo();
	p2.printInfo();
	p3.printInfo();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值