C++运算符重载实例(调试环境 Visual Studio 2019)

最近看了菜鸟教程里的C++教程
遇到很多运算符重载,为了方便我的学习,我把这些总结了一下
如有错误(包括之前的博文)请评论留言,谢谢!

由于代码里注释的很清楚,我就不做过多的解释了。
下面是这次总结的头文件,用来放置一些类和方法。

//C++运算符重载实例.h
#pragma once
#include <iostream>
using namespace std;

class chongzai
{
private:
	int i, j, k;
public:
	chongzai()
	{
		i = 0;
		j = 0;
		k = 0;
	}
	chongzai(int a, int b, int c)
	{
		i = a;
		j = b;
		k = c;
	}
	//以下分别为A1,A2,A3的显示函数
	void display1()
	{
		cout << "A1:" << endl;
		cout << "i=" << i << endl;
		cout << "j=" << j << endl;
		cout << "k=" << k << endl;
		cout << "------------------" << endl;
	}
	void display2()
	{
		cout << "A2:" << endl;
		cout << "i=" << i << endl;
		cout << "j=" << j << endl;
		cout << "k=" << k << endl;
		cout << "------------------" << endl;
	}
	void display3()
	{
		cout << "A3:" << endl;
		cout << "i=" << i << endl;
		cout << "j=" << j << endl;
		cout << "k=" << k << endl;
		cout << "------------------" << endl;
	}
	/*************************************************************************************************/
	/*
	一元运算符重载:
	递增运算符( ++ )和递减运算符( -- )
	一元减运算符,即负号( - )
	逻辑非运算符( ! )
	*/
	chongzai operator-()						//以负号( - )为例
	{
		i = -i;
		j = -j;
		k = -k;
		return chongzai(i, j, k);
	}
	/*************************************************************************************************/
	/*
	二元运算符重载:
	二元运算符需要两个参数
	加运算符( + ),减运算符( - ),乘运算符( * )和除运算符( / )
	*/
	chongzai operator+(const chongzai& n)		//以加号( + )为例
	{
		chongzai A;
		A.i = this->i + n.i;
		A.j = this->j + n.j;
		A.k = this->k + n.k;
		return A;
	}
	/**********************************************************************************************/
	/*
	关系运算符重载:
	大于( > ),小于( < ),大于等于( >= ),小于等于( <= ),等于( = )等
	*/
	bool operator<(const chongzai& n)			//以小于号( < )为例
	{
		if (i < n.i)
			return true;
		if (i >= n.i)
			return false;
		return false;
	}
	/**********************************************************************************************/
	/*
	输入输出运算符重载:
	流提取运算符 >> 和流插入运算符 <<
	使用友元函数无需设置对象,而且符合人们cout<<和cin>>的书写习惯
	*/
	friend ostream& operator<<(ostream& output, const chongzai& A)
	{
		output << "i:" << A.i << endl;
		output << "j:" << A.j << endl;
		output << "k:" << A.k << endl;
		output << "------------------" << endl;
		return output;
	}
	friend istream& operator>>(istream& input, chongzai& A)
	{
		input >> A.i >> A.j >> A.k;
		return input;
	}
	/*************************************************************************************************/
	/*
	++和--运算符重载:
	包括前置和后置
	*/
	chongzai operator++()						//以前置++为例
	{
		++i;
		++j;
		++k;
		return chongzai(i, j, k);
	}
	chongzai operator++(int)					//后置++的特殊格式
	{
		i++;
		j++;
		k++;
		return chongzai(i, j, k);
	}
	/*************************************************************************************************/
	/*
	赋值运算符重载:
	赋值运算符( = ),比如拷贝构造函数
	*/
	void operator=(const chongzai& A)			//以拷贝构造函数为例
	{
		i = A.i;
		j = A.j;
		k = A.k;
	}
	/*************************************************************************************************/
	/*
	函数调用运算符重载:
	函数调用运算符 () 可以被重载用于类的对象。
	当重载 () 时,您不是创造了一种新的调用函数的方式,
	相反地,这是创建一个可以传递任意数目参数的运算符函数。
	*/
	chongzai operator()(int a, int b, int c)
	{
		chongzai A;
		//利用()里的参数进行各种运算
		A.i = a + b;
		A.j = b + c;
		A.k = a + c;
		return A;
	}
};

class chongzai2
{
private:
	int arr[5];
public:
	int n;
	chongzai2()
	{
		for (n = 0; n < 5; n++)
		{
			arr[n] = n;
		}
	}
	/*************************************************************************************************/
	/*
	下标运算符[]重载:
	*/
	int& operator[](int n)
	{
		if (n >= 5)
		{
			cout << "索引超过最大值" << endl;
			return arr[0];
		}
		return arr[n];
	}
};

然后这是主程序

//C++运算符重载实例.cpp
#include "标头.h"
#include <iostream>
using namespace std;

int main()
{
	chongzai A1(10, 20, 30), A2(100, 200, 300), A3;
	-A1;										//一元运算符重载
	A1.display1();
	A3 = A1 + A2;								//二元运算符重载
	A3.display3();
	if (A1 < A2)								//关系运算符重载
		cout << "D1<D2" << endl << endl;
	else
		cout << "D1>=D2" << endl << endl;
	cout << "请输入A3的各项参数:" << endl;		//输入输出元算符重载
	cin >> A3;
	cout << "A3的各项参数为:" << endl;
	cout << A3;
	++A1;										//前置++运算符重载
	A1.display1();
	A1++;										//后置++运算符重载
	A1.display1();
	A1 = A2;									//赋值运算符重载
	A1.display1();
	A2 = A3(2, 4, 6);							//函数调用运算符()重载
	A2.display2();
	chongzai2 B;
	cout << "B[4]的值为:" << B[4] << endl;
	cout << "B[5]的值为:" << B[5] << endl;		//下标运算符[]重载
	return 0;
}

在输入A3的时候,举个例子,输入1 2 3
输出结果为

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/pineapple_C/article/details/104282259