RMBの強い通貨タイプを実現
3992 ワード
#ifndef RMB_H_
#define RMB_H_
typedef double Yuan;
typedef unsigned long Cent;
class RMB {
public:
RMB();
RMB(Cent cent);
RMB(Yuan yuan);
RMB(RMB& that);
~RMB();
const char* toString();
Yuan GetYuan()const;
Cent GetCent()const;
RMB& operator=(RMB& that);
RMB& operator=(Cent cent);
RMB& operator=(Yuan yuan);
bool operator==(RMB& that)const;
bool operator> (RMB& that)const;
bool operator< (RMB& that)const;
bool operator>=(RMB& that)const;
bool operator<=(RMB& that)const;
bool operator!=(RMB& that)const;
RMB& operator++();
RMB operator++(int);
RMB& operator--();
RMB operator--(int);
RMB& operator+=(RMB& that);
RMB& operator-=(RMB& that);
private:
Cent m_Base;
};
#endif /* RMB_H_ */
インプリメンテーション
#include "RMB.h"
#include <stdio.h> //for sprintf
RMB::RMB()
:m_Base(0)
{
}
RMB::RMB(Cent cent)
{
m_Base = cent;
}
RMB::RMB(Yuan yuan)
{
m_Base = yuan*100;
}
RMB::RMB(RMB& that)
{
this->m_Base = that.m_Base;
}
RMB::~RMB()
{
}
Yuan
RMB::GetYuan()const
{
return m_Base*0.01;
}
Cent
RMB::GetCent()const
{
return m_Base;
}
const char*
RMB::toString()
{
static char str[256]={0};
sprintf(str,"%f yuan(Cent:%lu)
",m_Base*0.01,m_Base);
return str;
}
RMB&
RMB::operator=(RMB& that)
{
if(this != &that)
{
this->m_Base = that.m_Base;
}
return *this;
}
RMB&
RMB::operator=(Cent cent)
{
this->m_Base = cent;
return *this;
}
RMB&
RMB::operator=(Yuan yuan)
{
this->m_Base = yuan*100;
return *this;
}
bool
RMB::operator==(RMB& that)const
{
return this->m_Base == that.m_Base;
}
bool
RMB::operator> (RMB& that)const
{
return this->m_Base > that.m_Base;
}
bool
RMB::operator< (RMB& that)const
{
return this->m_Base < that.m_Base;
}
bool
RMB::operator>=(RMB& that)const
{
return *this > that || * this == that;
}
bool
RMB::operator<=(RMB& that)const
{
return *this < that || * this == that;
}
bool
RMB::operator!=(RMB& that)const
{
return ! (*this==that);
}
RMB&
RMB::operator++()
{
++this->m_Base;
return *this;
}
RMB
RMB::operator++(int)
{
RMB tmp(*this);
++(*this);
return tmp;
}
RMB&
RMB::operator--()
{
--this->m_Base;
return *this;
}
RMB
RMB::operator--(int)
{
RMB tmp(*this);
--(*this);
return tmp;
}
RMB&
RMB::operator+=(RMB& that)
{
this->m_Base+=that.m_Base;
return *this;
}
RMB&
RMB::operator-=(RMB& that)
{
this->m_Base-=that.m_Base;
return *this;
}
テストコード
#include <iostream>
#include "RMB.h"
using namespace std;
int main()
{
RMB m;
cout<< m.toString() <<endl;
++m;
RMB m1(m);//
cout<< m1.toString() <<endl;
RMB m2 = m1;//
cout<< m2.toString() <<endl;
//RMB n(10);
RMB n( static_cast<Yuan>(10) );
cout<< n.toString() <<endl;
RMB n1( static_cast<Cent>(10) );
cout<< n1.toString() <<endl;
n1=n;//
cout<< n1.toString() <<endl;
if(m==n)
cout << "m==n" <<endl;
if(m!=n)
cout << "m!=n" <<endl;
if(m>n)
cout << "m>n" <<endl;
if(m<n)
cout << "m<n" <<endl;
if(m>=n)
cout << "m>=n" <<endl;
if(m<=n)
cout << "m<=n" <<endl;
//m = 200;
m = static_cast<Yuan>(200);
cout<< m.toString() <<endl;
n = static_cast<Cent>(200);
cout<< n.toString() <<endl;
return 0;
}
実行結果
0.000000 yuan(Cent:0)
0.010000 yuan(Cent:1)
0.010000 yuan(Cent:1)
10.000000 yuan(Cent:1000)
0.100000 yuan(Cent:10)
10.000000 yuan(Cent:1000)
m!=n
m
200.000000 yuan(Cent:20000)
2.000000 yuan(Cent:200)