蒟蒻求助重载运算符
  • 板块学术版
  • 楼主linxuanrui
  • 当前回复9
  • 已保存回复9
  • 发布时间2023/1/21 22:28
  • 上次更新2023/10/24 03:23:25
查看原帖
蒟蒻求助重载运算符
857323
linxuanrui楼主2023/1/21 22:28

本蒟蒻刚学了运算符重载,但return 3221225125。

#pragma GCC optmize(2)
#include<bits/stdc++.h>
using namespace std;

class Int{
	private:
		int n;
	public:
		Int(int x = 0){
			n = x;
		}
		friend istream& operator>>(istream& is,const Int &a);
		friend ostream& operator<<(ostream& os,const Int &a);
		friend bool operator<(const Int &a,const Int &b);
		friend bool operator<=(const Int &a,const Int &b);
		friend bool operator>(const Int &a,const Int &b);
		friend bool operator>=(const Int &a,const Int &b);
		friend bool operator==(const Int &a,const Int &b);
		friend Int operator+(const Int &a,const Int &b);
		friend Int operator-(const Int &a,const Int &b);
		friend Int operator*(const Int &a,const Int &b);
		friend Int operator/(const Int &a,const Int &b);
		friend Int operator%(const Int &a,const Int &b);
};

istream& operator>>(istream& is,const Int &a){
	is >> a;
	return is;
}

ostream& operator<<(ostream& os,const Int &a){
	os << a;
	return os;
}

Int operator+(const Int &a,const Int &b){
	return Int(a.n + b.n);
}

Int operator-(const Int &a,const Int &b){
	return Int(a.n - b.n);
}

Int operator*(const Int &a,const Int &b){
	return Int(a.n * b.n);
}

Int operator/(const Int &a,const Int &b){
	return Int(a.n / b.n);
}

Int operator%(const Int &a,const Int &b){
	return Int(a.n % b.n);
}

bool operator<(const Int &a,const Int &b){
	return a.n < b.n;
}

bool operator<=(const Int &a,const Int &b){
	return a.n <= b.n;
}

bool operator>(const Int &a,const Int &b){
	return a.n > b.n;
}

bool operator>=(const Int &a,const Int &b){
	return a.n >= b.n;
}

bool operator==(const Int &a,const Int &b){
	return a.n == b.n;
}

signed main(){
	Int a,b;
	cin >> a;
	cin >> b;
	Int c = a + b;
	cout << c;
}
2023/1/21 22:28
加载中...