
程序代码:
#include <windows.h>
#include <iostream>
using namespace std;
const LPSTR Msg = "系统提示信息...";
CHAR buffer[20];
class Account
{
public:
VOID SaveMoney(UINT money);//存款
BOOL DrawMoney(UINT money);//取款
virtual VOID Query();//查询
virtual DOUBLE Interest() = 0;//计算存款利息
protected:
LPSTR m_ID;//账号
LPSTR m_Name;//姓名
UINT m_ReSum;//余额
};
VOID Account::SaveMoney(UINT money)
{
m_ReSum += money;
ZeroMemory(buffer, sizeof(CHAR)*20);
wsprintf(buffer, "存款成功! 存款金额为: %u", money);
MessageBox(NULL, buffer, Msg, MB_OK);
}
BOOL Account::DrawMoney(UINT money)
{
ZeroMemory(buffer, sizeof(CHAR)*20);
if (money > m_ReSum)
{
MessageBox(NULL, "余额不足", Msg, MB_OK);
return FALSE;
}
m_ReSum -= money;
wsprintf(buffer, "取款成功! 取款金额为: %u", money);
MessageBox(NULL, buffer, Msg, MB_OK);
return TRUE;
}
VOID Account::Query()
{
cout << "\t账号: " << m_ID << endl;
cout << "\t姓名: " << m_Name << endl;
cout << "\t余额: " << m_ReSum << endl;
}
class FixAccount:public Account
{
public:
FixAccount(LPSTR id, LPSTR name, UINT money, UINT months);
~FixAccount();
VOID Query();//查询
DOUBLE Interest();//计算存款利息
protected:
UINT m_Month;
};
FixAccount::FixAccount(LPSTR id, LPSTR name, UINT money, UINT months)
{
HANDLE heap = GetProcessHeap();
m_ID = (LPSTR) HeapAlloc (heap, HEAP_ZERO_MEMORY, lstrlen(id)+1);
CopyMemory(m_ID, id, lstrlen(id));
m_Name = (LPSTR) HeapAlloc (heap, HEAP_ZERO_MEMORY, lstrlen(name)+1);
CopyMemory(m_Name, name, lstrlen(name));
m_ReSum = money;
m_Month = months;
}
FixAccount::~FixAccount()
{
HANDLE heap = GetProcessHeap();
HeapFree( heap, HEAP_NO_SERIALIZE, m_ID);
m_ID = NULL;
HeapFree( heap, HEAP_NO_SERIALIZE, m_Name);
m_Name = NULL;
}
VOID FixAccount::Query()
{
Account::Query();
cout << "\t时间: " << m_Month << endl;
cout << "\t利息: " << Interest() << endl;
}
DOUBLE FixAccount::Interest()
{
if (m_Month >= 60)
{
return 0.0525;
}
else if (m_Month >= 36)
{
return 0.0475;
}
else if (m_Month >= 24)
{
return 0.0415;
}
else if (m_Month >= 12)
{
return 0.0325;
}
else if (m_Month >= 6)
{
return 0.0305;
}
else if (m_Month >= 3)
{
return 0.0285;
}
else
{
return 0;
}
}
class CurrAccount:public Account
{
public:
CurrAccount(LPSTR id, LPSTR name, UINT money);
~CurrAccount();
VOID Query();//查询
DOUBLE Interest();//计算存款利息
};
CurrAccount::CurrAccount(LPSTR id, LPSTR name, UINT money)
{
HANDLE heap = GetProcessHeap();
m_ID = (LPSTR) HeapAlloc (heap, HEAP_ZERO_MEMORY, lstrlen(id)+1);
CopyMemory(m_ID, id, lstrlen(id));
m_Name = (LPSTR) HeapAlloc (heap, HEAP_ZERO_MEMORY, lstrlen(name)+1);
CopyMemory(m_Name, name, lstrlen(name));
m_ReSum = money;
}
CurrAccount::~CurrAccount()
{
HANDLE heap = GetProcessHeap();
HeapFree( heap, HEAP_NO_SERIALIZE, m_ID);
m_ID = NULL;
HeapFree( heap, HEAP_NO_SERIALIZE, m_Name);
m_Name = NULL;
}
VOID CurrAccount::Query()
{
Account::Query();
cout << "\t利息: " << Interest() << endl;
}
DOUBLE CurrAccount::Interest()
{
return 0.005;
}
VOID Factory(Account *account)
{
account->Query();
// account->SaveMoney(1);
// account->DrawMoney(1);
cout << endl;
}
INT main(VOID)
{
// FixAccount *fix=NULL;
// CurrAccount *cur=NULL;
Factory(new FixAccount("54321", "红色的吉他", 1000, 64));
Factory(new CurrAccount("9958", "蓝色的外套", 1110));
return 0;
}