运算符重载

一.基本运算符重载
1.不能重载的运算符:. :: ?: sizeof
2.返回值类型 operator+(){}
3.因为这个运算符重载是在类中,this指针要占一个参数
4."+"重载

int operator+()
{
#include <iostream>
using namespace std;
class student
{
	int a;
public:
	student()
	{
		a = 1;
	}
	int operator+(const student&stu)
	{
		return this->a + stu.a;
	}
};
int main()
{
	student stu1, stu2;
	int c = stu1 + stu2;//stu1是调用者,stu2是传入者
	cout << c << endl;
	system("pause");
	return 0;
}

}

5."*"重载 功能是输出一句话

	void operator*()
	{
		cout << "你是猪吗" << endl;
	}
	*stu1;

4.显示调用:int c=stu1.operator+(stu2);

二.前后++重载
1.前置++重载

	student& operator++()
	{
		this->a++;
		return *this;
	}
		student stu;
	    ++stu;

2.后置++重载

student operator++(int)
	{
		student stu =*this;
		this->a++;
		return stu;
	}
	student stu1;
	stu1++;
	如果有指针,需要写拷贝构造

三.左移右移重载
在类外重载


#include <iostream>
using namespace std;
class student
{
	int a;
public:
	student()
	{
		a = 1;
	}
	friend ostream&operator<<(ostream &cout,student &stu);
	friend istream&operator>>(istream &cin, student &stu);
};
ostream&operator<<(ostream &cout, student &stu)
{
	cout << stu.a;
	return cout;
}
istream&operator>>(istream&cin, student &stu)
{
	cin >> stu.a;
	return cin;
}
int main()
{
	student stu;
	cout << stu << endl;
	cin >> stu;
	system("pause");
	return 0;
}

四."="重载
1.重载"="

#include <iostream>
using namespace std;
class student
{
	char* name;
public:
	student(char* name)
	{
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
	}
	~student()
	{
		if (name != NULL)
		{
			delete[]name;
			name = NULL;
		}
	}
	student& operator=(student& stu)
	{
		if (this->name != NULL)
		{
			delete[]name;
			name = NULL;
		}
		this->name = new char[strlen(stu.name) + 1];
		strcpy(this->name, stu.name);
		return *this;
	}

};
int main()
{
	student stu1("你好");
	student stu2("我很好");
	student stu3("好吗");
	stu1 = stu2 = stu3; //stu1是调用者,stu2是一个传入者

	system("pause");
	return 0;
}

2.重载()

	void operator()(int a,int b,int c)
	{
		cout << a << '\t' << b << '\t' << c << endl;
	}
	void operator()()
	{
		cout << "你是猪吗" << endl;
	}
	stu1(1,2,3);
	stu1();

五.智能指针
*作用:*用来托管new出来的对象,让对象自动释放。

#include <iostream>
using namespace std;
class student
{
	int a;
public:
	student(int a)
	{
		this->a = a;
	}
	~student()
	{
		cout << "指针析构了" << endl;
	}
	void fun()
	{
		cout << a << '\n';
	}
};
class smartPointer
{
	student* stu;//要托管的指针
public:
	smartPointer(student* p)
	{
		stu = p;
	}
	~smartPointer()
	{
		cout << "智能指针析构了" << endl;
		if (stu != NULL)
		{
			delete stu;
			stu = NULL;
		}
	}
	student* operator->()
	{
		return this->stu;
	}
	student& operator*()
	{
		return *this->stu;
	}
	student& operator[](int a)
	{
		return *this->stu;
	}
};
void text()
{
	
	smartPointer sp(new student(1));
	sp->fun();
	(*sp).fun();
	sp[2].fun();
}
int main()
{
	text();
	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值