• 多态


    //account.h
    
    #pragma once
    
    #include <iostream>
    using namespace std;
    class Account
    {
    public:
    	Account(void);
    	~Account(void);
    	Account(unsigned accNo,float balan = 0.0);
    	int AccountNo();
    	float AcntBalan();
    	static Account* First();
    	Account* Next();
    	static int NoAccounts();
    	void Display();
    	void Deposit(float amount);
    
    	//pure virtual function
    	virtual void Withdrawal(float amount) = 0;
    
    protected:
    	static Account *pFirst;
    	Account *pNext;
    	static int count;
    	unsigned acntNumber;
    	float balance;
    };
    
    
    //account.cpp
    
    #include "Account.h"
    
    Account* Account::pFirst = (Account *)0;
    int Account::count = 0;
    
    Account::Account(void)
    {
    }
    
    Account::~Account(void)
    {
    	count--;
    	if (this == pFirst)
    	{
    		pFirst = this->pNext;
    		return ;
    	}
    	for (Account *pS = pFirst;pS;pS = pS->pNext)
    	{
    		if (pS->pNext == this)
    		{
    			pS->pNext = this->pNext;
    			return ;
    		}
    	}
    }
    
    Account ::Account(unsigned accNo,float balan /* = 0.0 */)
    {
    	acntNumber = accNo;
    	balance = balan;
    	pNext = (Account *)0;
    
    	count++;
    
    	if (pFirst == NULL)
    	{
    		pFirst = this;//insert to empty link list
    	}
    	else
    	{
    		Account *pS = pFirst;
    		while (pS->pNext)//move to the last node
    		{
    			pS = pS->pNext;
    		}
    		pS->pNext = this;//inert this node
    	}
    	pNext = (Account *)0;//this node as the last node
    }
    
    int Account ::AccountNo()
    {
    	return acntNumber;
    }
    
    float Account::AcntBalan()
    {
    	return balance;
    }
    
    
    Account* Account::First()
    {
    	return pFirst;
    }
    
    Account* Account::Next()
    {
    	return pNext;
    }
    
    int Account::NoAccounts()
    {
    	return count;
    }
    
    void Account::Display()
    {
    	cout<<"Account number: "<<acntNumber
    		<<" = "<<balance<<endl;
    }
    
    
    void Account::Deposit(float amount)
    {
    	balance += amount;
    }
    
    
    
    //checking.h
    
    #pragma once
    #include "account.h"
    
    
    enum REMIT{remitByPost, remitByCable, other};
    
    class Checking : public Account
    {
    public:
    	Checking(void);
    	~Checking(void);
    	Checking(unsigned accNo, float balan = 0.0);
    	void SetRemit(REMIT re);
    	virtual void Withdrawal(float amount);
    protected:
    	REMIT remittance;
    };
    
    
    
    //checking.cpp
    #include "Checking.h"
    
    Checking::Checking(void)
    {
    }
    
    Checking::~Checking(void)
    {
    }
    
    Checking::Checking(unsigned accNo, float balan /* = 0.0 */):Account(accNo, balan)
    {
    	remittance = other;
    }
    
    void Checking::SetRemit(REMIT re)
    {
    	remittance = re;
    }
    
    void Checking::Withdrawal(float amount)
    {
    	cout<<"call Checking fun"<<endl;
    	float temp = 0.0;
    	if (remittance == remitByPost)
    	{
    		temp = amount + 30;
    	}
    	else if (remittance == remitByCable)
    	{
    		temp = amount + 60;
    	}
    
    	if (balance < temp)
    	{
    		cout<<"Insufficient funds : balance "<<balance
    			<<", withdrawal "<<temp<<endl;
    	}
    	else
    		balance -= temp;
    }
    
    
    //saving.h
    #pragma once
    #include "account.h"
    
    class Savings :
    	public Account
    {
    public:
    	Savings(void);
    	~Savings(void);
    	Savings(unsigned accNo, float balan = 0.0);
    	virtual void Withdrawal(float amount);
    
    protected:
    	static float minBalance;
    };
    
    
    
    
    //savings.cpp
    #include "Savings.h"
    
    
    float Savings::minBalance = 500.0;
    
    
    Savings::Savings(void)
    {
    }
    
    Savings::~Savings(void)
    {
    }
    
    
    Savings::Savings(unsigned int accNo, float balan):Account(accNo, balan){}
    
    void Savings::Withdrawal(float amount)
    {
    	cout<<"call Saving func"<<endl;
    	if (balance + minBalance < amount)
    	{
    		cout<<"Insufficient funds : balance "<<balance
    			<<", withdrawal "				<<amount<<endl;
    	}
    	else
    		balance -= amount;
    }
    
    
    
    
    
    //main.cpp
    #include "Checking.h"
    #include "Savings.h"
    
    void Func(Account* pA)
    {
    	pA->Withdrawal(300.0);
    }
    
    int main()
    {
    	Savings s(123,200.0);
    	Checking c(1234,200.0);
    	Func(&s);
    	Func(&c);
    	return 0;
    }
    
  • 相关阅读:
    Mac OS X下Maven的安装与配置
    [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
    IOS基础:深入理解Objective-c中@class的含义
    NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
    真机测试时的错误:No matching provisioning profiles found
    转帖Jmeter中的几个重要测试指标释义
    Spring集成log4j日志管理
    Log4J日志配置详解
    使用Redis的理由
  • 原文地址:https://www.cnblogs.com/steady/p/1851131.html
Copyright © 2020-2023  润新知