zoukankan      html  css  js  c++  java
  • 多态

    //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;
    }
    
  • 相关阅读:
    EdgeX Foundry初体验(五)-- Web Console图形界面(v1.0.0)
    第十九节:SQLServer通过发布订阅实现主从同步(读写分离)详解
    第六节:Ocelot之自身负载、网关限流、缓存和熔断机制
    第十九节:SQLServer通过发布订阅实现主从同步(读写分离)详解
    第十七节:分区、分表、分库以及基于EFCore实现读写分离
    第六节:IdentityServer4设备流授权模式和扫码登录(应用于IOT)
    第五节:IdentityServer4的Pkce机制、令牌刷新机制、混合授权模式
    第十一节:IdentityServer4授权码模式介绍和代码实操演练
    第十二节:Ocelot集成IDS4认证授权-微服务主体架构完成
    第十节:IdentityServer4隐式模式介绍和代码实操演练
  • 原文地址:https://www.cnblogs.com/steady/p/1851131.html
Copyright © 2011-2022 走看看