PageRenderTime 48ms CodeModel.GetById 9ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/ch12/ch02/main.cpp

https://github.com/jinjianxin/My-Program
C++ | 151 lines | 101 code | 28 blank | 22 comment | 0 complexity | 74d479ba2579e7a95dedb024c13cb8ec MD5 | raw file
  1#include <iostream>
  2
  3using namespace std;
  4
  5class Integer
  6{
  7	public:
  8		Integer(int i);
  9		~Integer();
 10		/*+*/
 11		Integer operator+(const Integer& in);
 12		Integer operator+(const Integer& in) const;
 13//		const Integer operator+(const Integer& in) const;
 14//		/*-*/
 15		Integer operator-(const Integer& in);
 16		Integer operator-(const Integer& in) const;
 17		/* * */
 18		Integer operator*(const Integer& in);
 19		Integer operator*(const Integer& in) const;
 20
 21		/* / */
 22		Integer operator/(const Integer& in);
 23		Integer operator/(const Integer& in) const;
 24	
 25		/*++*/
 26		Integer operator++();
 27		Integer operator++(int);
 28
 29		/*--*/
 30		Integer operator--();
 31		Integer operator--(int);
 32
 33		//Integer operator++() const; error
 34	
 35		int objectCount;
 36};
 37
 38Integer::Integer(int i):
 39	objectCount(i)
 40{
 41}
 42
 43Integer::~Integer()
 44{
 45}
 46
 47Integer Integer::operator+(const Integer& in)
 48{
 49	return Integer(objectCount+in.objectCount);
 50}
 51
 52Integer Integer::operator+(const Integer& in) const
 53{
 54	return Integer(objectCount+in.objectCount);
 55}
 56/*
 57const Integer Integer::operator+(const Integer& in) const
 58{
 59	return Integer(objectCount+in.objectCount);
 60}*/
 61
 62Integer Integer::operator-(const Integer& in)
 63{
 64	return Integer(objectCount-in.objectCount);
 65}
 66
 67Integer Integer::operator-(const Integer& in) const
 68{
 69	return Integer(objectCount-in.objectCount);
 70}
 71
 72/*
 73Integer& Integer::operator+(const Integer& in)
 74{
 75	Integer a(objectCount+in.objectCount);
 76	Integer& b =a;
 77	return b;
 78}
 79*/
 80
 81Integer Integer::operator*(const Integer& in)
 82{
 83	return Integer(objectCount*in.objectCount);
 84}
 85
 86Integer Integer::operator*(const Integer& in) const
 87{
 88	return Integer(objectCount*in.objectCount);
 89}
 90
 91Integer Integer::operator/(const Integer& in)
 92{
 93	return Integer(objectCount/in.objectCount);
 94}
 95
 96Integer Integer::operator/(const Integer& in) const
 97{
 98	return Integer(objectCount/in.objectCount);
 99}
100
101Integer Integer::operator++()
102{
103	++objectCount;
104	return *this;
105}
106
107Integer Integer::operator++(int)
108{
109	objectCount++;
110	return *this;
111}
112
113Integer Integer::operator--()
114{
115	--objectCount;
116	return *this;
117}
118
119Integer Integer::operator--(int)
120{
121	objectCount--;
122	return *this;
123}
124
125int main(int argc,char *argv[])
126{
127	Integer a(10);
128	Integer b(5);
129	
130
131	cout<<"operator +"<<"\ta+b="<<((a+b)+b).objectCount<<endl;
132	cout<<"operator -"<<"\ta-b="<<(a-b).objectCount<<endl;
133	cout<<"operator *"<<"\ta*b="<<(a*b).objectCount<<endl;
134	cout<<"operator /"<<"\ta/b="<<(a/b).objectCount<<endl;
135	cout<<"operator a++"<<"\ta++="<<(a++).objectCount<<endl;
136	cout<<"operator ++a"<<"\t++a="<<++a.objectCount<<endl;
137	cout<<"operator a--"<<"\ta--="<<a--.objectCount<<endl;
138	cout<<"operator --a"<<"\t--a="<<--a.objectCount<<endl;
139
140	cout<<endl;
141
142	/*const*/
143	const Integer d(3);
144	const Integer c(2);
145	
146	cout<<"operator +"<<"\tc+d="<<(c+d).objectCount<<endl;
147	cout<<"operator -"<<"\tc-d="<<(c-d).objectCount<<endl;	
148	cout<<"operator *"<<"\tc*d="<<(c*d).objectCount<<endl;
149	cout<<"operator /"<<"\tc/d="<<(c/d).objectCount<<endl;
150	return 0;
151}