C++运算符重载

这篇博客详细介绍了C++中运算符重载的几种常见方式,包括+、<<、++和==,并给出了相应的代码示例。此外,还展示了如何重载函数调用运算符()来实现仿函数的功能,以及深拷贝在赋值运算符重载中的应用。这些内容对于理解C++中类的行为和操作符的自定义有着重要作用。
摘要由CSDN通过智能技术生成

一、+号运算符重载

# include <iostream>

using namespace std;

class student
{
	public:
		int age;
		int weight;

        //第一种方法:成员函数 
//		student operator+ (student stu)
//		{
//			student stu1;
//			stu1.age=stu.age+age;
//			stu1.weight=stu.weight+weight;
//			return stu1;
//		}
};

//第二种方法:外部函数 
student operator+ (student& stu3,student& stu4)
{
	student stu5;
	stu5.age=stu3.age+stu4.age;
	stu5.weight=stu3.weight+stu4.weight;
	return stu5;
}

int main (void)
{
	student stu1;
	stu1.age=10;
	stu1.weight=10;

	student stu2;
	stu2.age=10;
	stu2.weight=10;

	student stu3;

	//真正的原理:
	//stu3=stu1.operator+(stu2); 
	//等价于: 
	stu3=stu1+stu2; 
	
	//真正的原理:
	//stu3=operator+(stu1,stu2); 
	//等价于:stu3=stu1+stu2; 

	cout<<stu3.age<<" "<<stu3.weight<<endl;
	return 0;
}

二、<<运算符重载

# include <iostream>
# include <string>

using namespace std;

class student
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age;
		}
		string name;
		int age;
};
//只能通过外部函数的方式实现,不能通过成员函数的方式实现 
ostream& operator<< (ostream& cout,student& stu1)//cout属于ostream类型,但只能有一个cout,所以只能采取引用的方式 
{
	cout<<"Name: "<<stu1.name<<" "<<"Age: "<<stu1.age;
	return cout;//返回值必须是ostream& 类型,否则无法使用cout<<a<<endl中的<<endl; 
}

int main (void)
{
	student stu("aaa",30);
	//此时的cout<<stu相当于将cout传给上面的 ostream& cout,stu传给 student& stu1
	cout<<stu<<endl;
	
	return 0;
}

三、++运算符重载

# include <iostream>

using namespace std;

class student 
{
	friend ostream& operator<< (ostream& cout,student stu);//添加友元类 
	public:
		student()
		{
			num=0;
		}
		//前置递增 
		student& operator++ ()//前置递增的返回值必须是引用,试试就知道了
		{
			++num;
			return *this; 
		}
		//后置递增
		student operator++ (int)//int为占位参数,用来区分前置递增和后置递增;后置递增的返回值是一个值 
		{
			student a=*this;
			num++;
			return a;
		} 
	private: 
		int num;
};
//这里第二个参数不是引用,不然后置递增错误,具体原因还未知
ostream& operator<< (ostream& cout,student stu)
{
	cout<<stu.num;
	return cout;
}

int main (void)
{
	student stu;
	cout<<stu++<<endl;
	cout<<stu<<endl;
	return 0; 
} 

四、==运算符重载

# include <iostream>
# include <string>
using namespace std;

class student
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age; 
		}
		bool operator== (student stu)
		{
			if(stu.age==this->age&&stu.name==this->name)
				return true;
			else
				return false;
		}
		string name;
		int age;
};

int main (void)
{
	student stu1("aaa",18);
	student stu2("aaa",18);
	if(stu1==stu2)
		cout<<"相等!"<<endl; 
	else
		cout<<"不相等!"<<endl; 
	return 0;
}

五、函数调用运算符重载(仿函数)

# include <iostream>

using namespace std;

class function
{
	public:
		int operator()(int a,int b)
		{
			return a+b;
		}
};

int main (void)
{
	function fun;
	int sum=fun(2,3);
	cout<<sum<<endl;
	int a=function()(2,3);//匿名函数对象 
	cout<<a<<endl;
	return 0;
} 

六、赋值运算符重载(就是深拷贝)

# include <iostream>

using namespace std;

class student
{
public:
    int * num;
    student(int a)
    {
        num=new int(a);
    }
    ~student()
    {
        if (num!=NULL)
        {
            delete num;
            num=NULL;
        }
    }
    student& operator=(student stu)//其实就是深拷贝
    {
        if (num!=NULL)
        {
            delete num;
            num=NULL;
        }
        num=new int(*stu.num);
        return *this;
    }
};

void test01()
{
    student stu1(10);
    student stu2(20);
    student stu3(30);
    stu1=stu2=stu3;
    cout<<*stu1.num<<"  "<<*stu2.num<<"  "<<*stu3.num<<endl;
}

int main(int argc, char const *argv[])
{
    test01();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值