cpp随笔2——继承实战——账户
题目如下:
根据16.12节报定义的Account类、Savings类和Checking类,编写一个应用程序,它读入一系列账号和存款,创建若干储蓄和结算账户,直到碰到一个标志结束符号。并输出所有账号的存款数据
Account类: class Account { public: Account(unsigned accNo, float balan=0.0); // unsigned AccountNo(); float AcntBalan(); static Account*& First(); Account* Next(); static int NoAccounts(); void Display(); void Deposit(float amount); virtual void Withdrawal(float amount); protected: static Account* pFirst; Account* pNext; static int count; unsigned acntNumber; float balance; }; Saving类: class Savings :public Account { public: Savings(unsigned accNo, float balan=0.0); virtual void Withdrawal(float amount); protected: static float minbalance; }; Checking类: class Checking :public Account { public: Checking(unsigned accNo, float balan=0.0); virtual void Withdrawal(float amount); void SetRemit(REMIT re); protected: REMIT remittance; }; 其中,enum REMIT{remitByPost,remitByCable,other}; //无,信汇,电汇
//account.h
#ifndef account #define account//防止反复调用 class Account { public: Account(unsigned accNo,float balan =0.0); unsigned AccountNo(); float AcntBalan(); static Account * First(); Account * Next(); static int NoAccounts(); void Display(); void Deposit(float amount); virtual void Withdrawal(float amount); protected: static Account *pFirst; Account *pNext; static int count; unsigned acntNumber; float balance; }; #endif
1.我们可以看到,account类中有两个指针,一个是Account *First 一个是Account *Next,我们可以认为两个函数返回的就是这两个指针。我们可以认为这是要实现一个链表结构来存放一系列数据
2,在account.h中,Account *First前有一个static关键词,很明显,链表的开头只能有一个,因此,我们需要定义其为static,表明这个变量是和类本身联系在一起的,而跟变量没有联系。这是什么意思呢,意思是两个Account变量的First是一个值,比如对象A的static int s=1,那么同一类的对象B的static int s=1,且两者是同时变化的
//account.cpp
#include <iostream> #include "account.h" using namespace std; 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;//this 表示本变量的地址 else { Account * pS=pFirst; for(;pS->pNext;pS=pS->pNext);//pS指向已有链表的最后一节 pS->pNext=this; } pNext=0; } 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::Display() { cout<<"Account number:"<<acntNumber<<endl; cout<<"balance = "<<balance <<endl; } void Account::Deposit(float amount) { balance+=amount; } void Account::Withdrawal(float amount) { return; }
注意在链表最后添加元素的方法与分类讨论
注意this 的用法(在构造函数中将对象添加在链表最后)
//savings.h
#ifndef savings #define savings #include "account.h" class Savings:public Account { public: Savings(unsigned accNo,float balan=0.0); virtual void Withdrawal(float amount); }; #endif
//savings.cpp
#include <iostream> #include "account.h" #include "savings.h" using namespace std; Savings::Savings(unsigned accNo,float balan) :Account(accNo,balan){} void Savings::Withdrawal(float amount) { if(balance<amount) cout<<"Insufficient funds:balance"<<balance <<",withdrawal"<<amount<<endl; else balance-=amount; } //savings.cpp
可以看到这里使用了一个virtual,这是用来实现函数的多态性。基类中也有一个virtual函数,用来提示编译器在编译的时候判断调用的是哪边的函数
//checking.h
#ifndef checking #define checkint #include "account.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); protected: REMIT remittance; }; #endif
//checking.cpp
#include <iostream> #include "account.h" #include "checking.h" using namespace std; 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; }
注意checking中的withdraw,根据汇款方式的不同加收费用(虽然和题干没有什么关系就是了)
#include <iostream> #include "savings.h" #include "account.h" #include "checking.h" using namespace std; int main() { /*输入*/ cout<<"Input the accountNo and saving"<<endl; Savings*pS; pS=NULL; unsigned aN; float val; cin>>aN>>val; while(aN) { pS=new Savings(aN,val); cout<<"Input the accountNo and saving"<<endl; cin>>aN>>val; } /*输出*/ for(Savings*p=(Savings*)pS->First();p;p=(Savings*)p->Next())//注意First返回的是Account* 指针,因此需要进行强制转换 cout<<p->AccountNo()<<" "<<p->AcntBalan()<<endl; /*释放内存*/ for(Savings*p=(Savings*)pS->First();p;) { Savings*t=p; p=(Savings*)p->Next(); delete t; } /*输入*/ Checking *pC; pC=NULL; cout<<"Input the accountNo and checking"<<endl; cin>>aN>>val; while(aN) { pC=new Checking(aN,val); cout<<"Input the accountNo and checking"<<endl; cin>>aN>>val; } /*输出*/ for(Checking*p=(Checking*)pC->First();p;p=(Checking*)p->Next()) cout<<p->AccountNo()<<" "<<p->AcntBalan()<<endl; /*释放内存*/ for(Checking*p=(Checking*)pC->First();p;) { Checking*t=p; p=(Checking*)p->Next(); delete t; } }
最后就是主函数了,注意指针类型的强制转换和内存的new、delete
posted on 2020-04-29 16:39 crazyplayer 阅读(528) 评论(0) 编辑 收藏 举报