码迷,mamicode.com
首页 > 编程语言 > 详细

【c++】实现运算符的重载

时间:2015-05-08 18:19:54      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:c++   运算符重载   

// 实现运算符的重载

#include <iostream>
using namespace std;

class Int
{
	public:
		Int(int i = 0) :m(i)
		{
			cout << "constructed function" << endl;
		}
		~Int()
		{
			cout << "destructor" << endl;
		}
	public:
		Int operator+(const Int &d);
		Int operator-(const Int &d);
		Int operator*(const Int &d);
		Int operator/(const Int &d);
		Int operator|(const Int &d);
		Int operator&(const Int &d);
		Int operator^(const Int &d);
		Int operator~();
		Int operator++();
		Int operator++(int);
		Int operator--();
		Int operator--(int);
		void print();
	private:
		int m;
};

Int Int ::operator+(const Int &d)
{
	return Int(m + d.m);
}

Int Int::operator-(const Int &d)
{
	return Int(m - d.m);
}

Int Int::operator*(const Int &d)
{
	return Int(m * d.m);
}

Int Int::operator/(const Int &d)
{
	return Int(m / d.m);
}

Int Int::operator|(const Int &d)
{
	return Int(m | d.m);
}

Int Int::operator&(const Int &d)
{
	return Int(m & d.m);
}

Int Int::operator^(const Int &d)
{
	return Int(m ^ d.m);
}

Int Int::operator~()
{
	return Int(~m);
}

Int Int::operator++()
{
	return Int(++m);
}

//后置++
Int Int::operator++(int)
{
	return Int(m++);
}

Int Int::operator--()
{
	return Int(--m);
}

//后置--
Int Int::operator--(int)
{
	return Int(m--);
}

void Int::print()
{
	cout << m << endl;
}


int main()
{
	Int i(2);
	i = i + 1;
	i.print();
	i = i - 1;
	i.print();
	
	Int a, b(3), c(1);
	a = b + c;
	a.print();
	a = b - c;
	a.print();
	a = b * c;
	a.print();
	a = b / c;
	a.print();
	a = b | c;
	a.print();
	a = b & c;
	a.print();
	a = b ^ c;
	a.print();
	a = ~b;
	a.print();
	a = ++b; 
	a.print();//4
	b.print();//4
	a = b++;
	a.print();//4
	b.print();//5
	a = --b;
	a.print();//4
	b.print();//4
	a = b--;
	a.print();//4
	b.print();//3
	return 0;
}// 实现运算符的重载

#include <iostream>
using namespace std;

class Int
{
	public:
		Int(int i = 0) :m(i)
		{
			cout << "constructed function" << endl;
		}
		~Int()
		{
			cout << "destructor" << endl;
		}
	public:
		Int operator+(const Int &d);
		Int operator-(const Int &d);
		Int operator*(const Int &d);
		Int operator/(const Int &d);
		Int operator|(const Int &d);
		Int operator&(const Int &d);
		Int operator^(const Int &d);
		Int operator~();
		Int operator++();
		Int operator++(int);
		Int operator--();
		Int operator--(int);
		void print();
	private:
		int m;
};

Int Int ::operator+(const Int &d)
{
	return Int(m + d.m);
}

Int Int::operator-(const Int &d)
{
	return Int(m - d.m);
}

Int Int::operator*(const Int &d)
{
	return Int(m * d.m);
}

Int Int::operator/(const Int &d)
{
	return Int(m / d.m);
}

Int Int::operator|(const Int &d)
{
	return Int(m | d.m);
}

Int Int::operator&(const Int &d)
{
	return Int(m & d.m);
}

Int Int::operator^(const Int &d)
{
	return Int(m ^ d.m);
}

Int Int::operator~()
{
	return Int(~m);
}

Int Int::operator++()
{
	return Int(++m);
}

//后置++
Int Int::operator++(int)
{
	return Int(m++);
}

Int Int::operator--()
{
	return Int(--m);
}

//后置--
Int Int::operator--(int)
{
	return Int(m--);
}

void Int::print()
{
	cout << m << endl;
}


int main()
{
	Int i(2);
	i = i + 1;
	i.print();
	i = i - 1;
	i.print();
	
	Int a, b(3), c(1);
	a = b + c;
	a.print();
	a = b - c;
	a.print();
	a = b * c;
	a.print();
	a = b / c;
	a.print();
	a = b | c;
	a.print();
	a = b & c;
	a.print();
	a = b ^ c;
	a.print();
	a = ~b;
	a.print();
	a = ++b; 
	a.print();//4
	b.print();//4
	a = b++;
	a.print();//4
	b.print();//5
	a = --b;
	a.print();//4
	b.print();//4
	a = b--;
	a.print();//4
	b.print();//3
	return 0;
}

<img src="http://img.blog.csdn.net/20150508161939603?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

<img src="http://img.blog.csdn.net/20150508161752132?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

【c++】实现运算符的重载

标签:c++   运算符重载   

原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/45581727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!