多态

//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;
}
原文地址:https://www.cnblogs.com/steady/p/1851131.html