钱能C++教材习题答案十二



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:50:50 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
 

第16章

//************
//***16.1*****
//*Daiyuebing*
//************
# include "account.h"
# include "savings.h"
# include "checking.h"
# include <iostream.h>

void main()
{

        Account *  p;
        Account *  q;
        unsigned accNo;
        float balan;
        cout<<"please input acneNumber and balance:"<<endl;
        cin>>accNo>>balan;
        while(accNo!=0)
        {
                int i;
                cout<<"please choice (1--储蓄    其它--结算):"<<endl;
                cin>>i;
                if(i=1)
                {
                       
                        p=(new Savings(accNo,balan));
                }
                else
                {
               
                        q=(new Checking(accNo,balan));
                }
                cout<<"please input acneNumber and balance:";
                cin>>accNo>>balan;
        }
        Account *  x=p->First();//此处的()表示成员函数,即为P所指对象的成员函数
        for(;x!=NULL;x=x->Next())
        {
                x->Display();
        }       
}


//************
//***16.2*****
//*Daiyuebing*
//************
//account.h
# include <iostream.h>
# ifndef account
# define account

class Account
{
        public:
                Account(unsigned accNo,float balan=0.0);
                ~Account()
                {
                        cout<<"Distroy..."<<endl;
                };
                unsigned AccountNo();
                float AcntBalan();
                static Account * First();
                Account * Next();
                static int NoAccounts();
                virtual void Display();//此处需要设置的虚函数
                void Desposit(float amount);
                virtual void Withdrawal(float amount);
        protected:
                static Account * pFirst;
                Account * pNext;
                static int count;
                unsigned acntNumber;
                float balance;
};

# endif

Account * Account::pFirst=0;
int Account::count=0;

Account::Account(unsigned accNo,float balan)
{
        acntNumber=accNo;
        balance=balan;
        pNext=0;
        count++;
        if(pFirst==0)
                pFirst=this;
        else
        {
                Account * pS=pFirst;
                for(;pS->pNext;pS=pS->pNext);
                pS->pNext=this;
        }
        pNext=0;
        cout<<"Construct..."<<endl;
}

unsigned 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:isplay()
{
        cout<<"Account number:"<<acntNumber
                <<"    balance:"<<balance<<endl;
}

void Account:esposit(float amount)
{
        balance+=amount;
}

void Account::Withdrawal(float amount)
{
        return;
}

// checking.h


enum REMIT{remitByPost,remitByCable,other};

class Checking:public Account
{
        public:
                Checking(unsigned accNo,float balan=0.0);
                virtual void Withdrawal(float amount);
                void SetRemit(REMIT re);
                void Display();//************************************
        protected:
                REMIT remittance;
};
void Checking:isplay()
{
        cout<<"Checking number:"<<acntNumber
                <<"    balance:"<<balance
                <<"    remittance:"<<remittance<<endl;
}
Checking::Checking(unsigned accNo,float balan):Account(accNo,balan)
{
        remittance=other;
}

void Checking::Withdrawal(float amount)
{
        float temp=amount;
        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;
}

void Checking::SetRemit(REMIT re)
{
        remittance=re;
}

 


class Savings:public Account
{
        public:
                Savings(unsigned accNo,float balan=0.0);
                virtual void Withdrawal(float amount);
                void Display();//********************************
        protected:
                static float minbalance;
};
float Savings::minbalance=500.0;
void Savings:isplay()
{
        cout<<"Savings number:"<<acntNumber
                <<"    balance:"<<balance
                <<"    minbalance:"<<minbalance<<endl;
}
Savings::Savings(unsigned accNo,float balan):Account(accNo,balan){}

void Savings::Withdrawal(float amount)
{
        if(balance+minbalance<amount)
                cout<<"Insufficient funds:balance"<<balance
                        <<",withdramal"<<amount<<endl;
        else
                balance-=amount;
}

 

Account a1(12,123);
Account a2(123,123);
Account a3(124,123);
Account a4(125,123);

Savings s1(12,123);
Savings s2(123,123);
Savings s3(124,123);
Savings s4(125,123);

Checking c1(12,123);
Checking c2(123,123);
Checking c3(124,123);
Checking c4(125,123);

void fn(Account * y)
{
        y->Display();
}

void main()
{
        Account *  x=s1.First();
        cout<<endl;
        for(;x!=0;x=x->Next())
        {
                fn(x);
        }       
}

//************
//***16.4*****
//*Daiyuebing*
//************
# include <iostream.h>
//****************************************************************************
class Account
{
        public:
                Account(unsigned accNo,float balan=0.0);
                unsigned AccountNo();
                float AcntBalan();
                static Account * First();
                Account * Next();
                static int NoAccounts();
                virtual void Display();
                void Desposit(float amount);
                virtual void Withdrawal(float amount);
        protected:
                static Account * pFirst;
                Account * pNext;
                static int count;
                unsigned acntNumber;
                float balance;
};
//****************************************************************************
Account * Account::pFirst=0;
int Account::count=0;



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文