C++::实现Int类 - 高小调博客

C++::实现Int类

编写一个C++程序实现Int类

Int.h

/*
*本代码版权归高小调博客所有 
*作者:高小调
*日期:2016-10-10
*文件功能:Int类的原型声明
*集成开发环境:Microsoft Visual Studio 2010 
*/
#ifndef __Int_H__
#define __Int_H__
#include<iostream>
using std::ostream;
using std::istream;
class Int
{
	friend ostream& operator<<(ostream& _cout, const Int& tmp);
	friend istream& operator>>(istream& _cin, Int& tmp);
public:
	Int(int value);
	Int& operator=(const Int& tmp);
	Int operator+(const Int& tmp);
	Int operator-(const Int& tmp);
	Int operator*(const Int& tmp);
	Int operator/(const Int& tmp);
	bool operator==(const Int& tmp);
	bool operator!=(const Int& tmp);
	bool operator>(const Int& tmp);
	bool operator<(const Int& tmp);
	bool operator>=(const Int& tmp);
	bool operator<=(const Int& tmp);
	bool operator||(const Int& tmp);
	bool operator&&(const Int& tmp);
	Int& operator++();
	Int operator++(int);
	Int& operator--();
	Int operator--(int);
private:
	int _value;
};
#endif

Int.cpp

/*
*本代码版权归高小调博客所有 
*作者:高小调
*日期:2016-10-10
*文件功能:Int类成员函数的实现
*集成开发环境:Microsoft Visual Studio 2010 
*/
#include"Int.h"
//构造函数
Int::Int(int value)
	:_value(value){
	//构造函数逻辑代码
}
//赋值运算符重载
Int& Int::operator=(const Int& tmp){
	if(this!=&tmp){
		_value = tmp._value;
	}
	return *this;
}
//加法运算符重载
Int Int::operator+(const Int& tmp){
	Int ret(*this);
	ret._value += tmp._value;
	return ret;
}
//减法运算符重载
Int Int::operator-(const Int& tmp){
	Int ret(*this);
	ret._value -= tmp._value;
	return ret;
}
//乘法运算符重载
Int Int::operator*(const Int& tmp){
	Int ret(*this);
	ret._value *= tmp._value;
	return ret;
}
//除法运算符重载
Int Int::operator/(const Int& tmp){
	if(tmp._value!=0){
		Int ret(*this);
		ret._value /= tmp._value;
		return ret;
	}else{
		return Int(0);
	}
}
//判断运算符重载
bool Int::operator==(const Int& tmp){
	if(_value==tmp._value){
		return true;
	}
	return false;
}
//判断运算符重载
bool Int::operator!=(const Int& tmp){
	if(_value!=tmp._value){
		return true;
	}
	return false;
}
//大于运算符重载
bool Int::operator>(const Int& tmp){
	if(_value>tmp._value){
		return true;
	}
	return false;
}
//小于运算符重载
bool Int::operator<(const Int& tmp){
	if(_value<tmp._value){
		return true;
	}
	return false;
}
//大于等于运算符重载
bool Int::operator>=(const Int& tmp){
	if(_value>=tmp._value){
		return true;
	}
	return false;
}
//大于等于运算符重载
bool Int::operator<=(const Int& tmp){
	if(_value<=tmp._value){
		return true;
	}
	return false;
}
//逻辑或重载
bool Int::operator||(const Int& tmp){
	if(_value){
		return true;
	}
	if(tmp._value){
		return true;
	}
	return false;
}
//逻辑与重载
bool Int::operator&&(const Int& tmp){
	if(!_value){
		return false;
	}
	if(!tmp._value){
		return false;
	}
	return true;
}
//前置++重载
Int& Int::operator++(){
	++_value;
	return *this;
}
//后置++重载
Int Int::operator++(int){
	Int ret(*this);
	_value++;
	return ret;
}

//前置--重载
Int& Int::operator--(){
	--_value;
	return *this;
}
//后置--重载
Int Int::operator--(int){
	Int ret(*this);
	_value--;
	return ret;
}
//输出运算符重载
ostream& operator<<(ostream& _cout, const Int& tmp){
	_cout<<tmp._value;
	return _cout;
}
//输入运算符重载
istream& operator>>(istream& _cin, Int& tmp){
	_cin>>tmp._value;
	return _cin;
}

Test.cpp

/*
*本代码版权归高小调博客所有 
*作者:高小调
*日期:2016-10-10
*文件功能:测试Int类
*集成开发环境:Microsoft Visual Studio 2010 
*/
#include"Int.h"
using namespace std;
//测试构造函数、输入、输出
void Test_1(){
	Int a(0),b(10),c(20);
	cout<<"测试1:构造函数、输入及输出"<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",c = "<<c<<endl;
	cout<<"请输入一个数修改a:>";
	cin>>a;
	cout<<"测试结果:"<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",c = "<<c<<endl<<endl;
}
//测试加、减、乘、除、赋值运算符重载
void Test_2(){
	Int a(10),b(20);
	cout<<"测试2:加、减、乘、除、赋值运算符重载"<<endl;
	cout<<"a = "<<a<<",b = "<<b<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a+b="<<a+b<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a-b="<<a-b<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a*b="<<a*b<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a/b="<<a/b<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a、b全部清零"<<endl;
	a = 0;
	b = 0;
	cout<<"a = "<<a<<",b = "<<b<<endl<<endl;
}
//测试逻辑与、逻辑或、逻辑等、逻辑不等运算符的重载
void Test_3(){
	Int a(0),b(1);
	cout<<"测试3:逻辑与、逻辑或、相等、不等运算符的重载"<<endl;
	cout<<"a = "<<a<<",a&&a = "<<(a&&a)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a&&b = "<<(a&&b)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b&&a = "<<(b&&a)<<endl;
	cout<<"b = "<<b<<",b&&b = "<<(b&&b)<<endl<<endl;

	cout<<"a = "<<a<<",a||a = "<<(a||a)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a||b = "<<(a||b)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b||a = "<<(b||a)<<endl;
	cout<<"b = "<<b<<",b||b = "<<(b||b)<<endl<<endl;

	cout<<"a = "<<a<<",a==a = "<<(a==a)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a==b = "<<(a==b)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b==a = "<<(b==a)<<endl;
	cout<<"b = "<<b<<",b==b = "<<(b==b)<<endl<<endl;

	cout<<"a = "<<a<<",a!=a = "<<(a!=a)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a!=b = "<<(a!=b)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b!=a = "<<(b!=a)<<endl;
	cout<<"b = "<<b<<",b!=b = "<<(b!=b)<<endl<<endl;

}
//测试大于、小于、大于等于、小于等于
void Test_4(){
	Int a(0),b(1),c(0);
	cout<<"a = "<<a<<",b = "<<b<<",a>b = "<<(a>b)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a>=b = "<<(a>=b)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a<b = "<<(a<b)<<endl;
	cout<<"a = "<<a<<",b = "<<b<<",a<=b = "<<(a<=b)<<endl;
	cout<<"a = "<<a<<",a>=a = "<<(a>=a)<<endl;
	cout<<"a = "<<a<<",a<=a = "<<(a<=a)<<endl<<endl;

	cout<<"b = "<<b<<",a = "<<a<<",b>a = "<<(b>a)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b<a = "<<(b<a)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b>=a = "<<(b>=a)<<endl;
	cout<<"b = "<<b<<",a = "<<a<<",b<=a = "<<(b<=a)<<endl;
	cout<<"b = "<<b<<",b>=b = "<<(b>=b)<<endl;
	cout<<"b = "<<b<<",b>=b = "<<(b<=b)<<endl<<endl;
}
int main(){
	Test_1();
	Test_2();
	Test_3();
	Test_4();
	return 0;
}

PS:最近太懒,很久没写博客了,攒了一堆的东西....

(未完待续...)

上一篇:
下一篇: