jlz

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 13 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

 

#include<iostream>
#include<cstring>
using std::cout;
using std::ios_base;
using std::endl;
using std::cin;


#ifndef BRASS_H_
#define BRASS_H_
class Brass
{
private:
 enum {MAX = 35};
 char fullname[MAX];
 long accNum;
 double balance;
public:
 Brass(const char* s="Nullbody",long  an = -1, double  bal=0.0);
 void Deposit(double amt);
 virtual void WithDraw(double amt);
 double Balance() const;
 virtual void ViewAcct() const;
 virtual ~Brass(){}
};


class BrassPlus :public Brass
{
private:
 double maxLoan;
 double rate;
 double owesBank;
public:
 BrassPlus(const char* s="Nullbody", long an=-1,double bal = 0.0,double ml = 500,double r=0.10);
    BrassPlus(const Brass &ba, double ml = 500 ,double r= 0.1);
 virtual void ViewAcct() const;
 virtual void WithDraw(double amt);
 void ResetMax(double m) { maxLoan = m;}
 void ResetRate(double r) { rate = r;}
 void ResetOwes() { owesBank = 0;}

};

#endif

 

Brass::Brass(const char* s,long an, double bal)
{
 std::strncpy(fullname,s,MAX-1);
 fullname[MAX-1] = '\0';
 accNum = an;
 balance = bal;
}

void Brass::Deposit(double amt)
{
 if(amt < 0 )
  cout<<"Negative desposit not allowed:"
   <<"desposit is cancelled.\n";
 else
  balance +=amt;
}

void Brass::WithDraw(double amt)
{
  if(amt < 0 )
  cout<<"Negative desposit not allowed:"
   <<"desposit is cancelled.\n";
  else if(amt <=balance)
   balance -= amt;
  else
   cout<<"withdrawal amount of $"<<amt
   <<"exceeds your balance.\n"
   <<"withdrawal canceled.\n";

}

double Brass::Balance() const
{
 return balance;
}

// 非构造函数不能使用成员初始化列表,但派生类方法可以调用公有的基类方法
void Brass::ViewAcct() const
{
 //set up  ###.## format
 ios_base::fmtflags initialState = cout.setf(ios_base::fixed,ios_base::floatfield);
 cout.setf(ios_base::showpoint);
 cout.precision(2);
 cout<<"Client:"<<fullname<<endl;
 cout<<"Account Numver:"<<accNum<<endl;
 cout<<"Balance:$"<<balance <<endl;

 cout.setf(initialState);

}

BrassPlus::BrassPlus(const char* s,long an ,double bal,double ml ,double r):Brass(s,an,bal)
{
 maxLoan  =ml;
 owesBank  = 0.0;
 rate = r;
}

BrassPlus::BrassPlus(const Brass &ba,double ml,double r):Brass(ba)
{
 maxLoan = ml;
 owesBank = 0.0;
 rate = r;
}

void BrassPlus::ViewAcct() const
{
 ios_base::fmtflags initialState = cout.setf(ios_base::fixed,ios_base::floatfield);
 cout.setf(ios_base::showpoint);
 Brass::ViewAcct(); //dispaly base portion
 cout<<"Maxinum loam:$"<<maxLoan<<endl;
 cout<<"Owed to Bank:$"<<owesBank<<endl;
 cout<<"Loan Rate:"<<100 * rate <<"%\n";
 cout.setf(initialState);

}
void BrassPlus::WithDraw(double amt)
{
 //set up ###.## format
 ios_base::fmtflags initialState = cout.setf(ios_base::fixed,ios_base::floatfield);
 cout.setf(ios_base::showpoint);
 cout.precision(2);
 double bal = Balance();
 if(amt <=bal)
  Brass::WithDraw(amt);  // 代码必须使用作用域解析操作符
 else if(amt <= bal + maxLoan - owesBank)
 {
  double advance  = amt - bal;
  owesBank += advance *(1.0 + rate);
  cout<<"Bank advance:$"<<advance<<endl;
  cout<<"Finance charge:$"<<advance * rate <<endl;
  Deposit(advance);
  Brass::WithDraw(amt);

 }
 else
 {
  cout<<"Credit limit exceeded. transaction cancelled.\n";
  
 }
 cout.setf(initialState);

 

}


//创建指向Brass 对象的指针数组,这样可以指向Brass也可以指向BrassPlus
// 使用一个数组来表示多种类型的对象,这就是多态

const int CLIENTS = 4;
const int LEN = 40;
int main()
{
  Brass *p_client[CLIENTS];
  int i;

  for(i=0; i <CLIENTS;i++)
  {
   char temp[LEN];
    long tempnum;
    double tempbal;
    char kind;
    cout<<"Enter client's name:";
    cin.getline(temp, LEN);

    cout<<"enter client's account number:";
    cin>>tempnum;

    cout<<"enter opening balance :$";
    cin>>tempbal;

    cout<<" enter 1 for blass account or "
     <<" 2 for brassplus account:";

    while(cin>>kind &&(kind != '1' && kind !='2'))
     cout<<"enter either 1 or 2:";
    if(kind == '1')
     p_client[i] = new Brass(temp,tempnum,tempbal);
    else
    {
     double tmax, trate;
     cout<<"enter the owerdraft limit:$";
     cin >> tmax;
     cout<<"enter the interest rate "
      <<"as a decimal fraction:";
     cin >> trate;

     p_client[i]  = new BrassPlus(temp,tempnum,tempbal,tmax,trate);


    }


    while(cin.get() !='\n')
     continue;

  }

  cout<<endl;
  for( i= 0 ;i<CLIENTS;i++)
  {
   p_client[i]->ViewAcct();
   cout<<endl;
  }

  for(i=0;i<CLIENTS;i++)
  {
   delete p_client[i];

  }

  cout<<"done.\n";
  return 0;

}

posted on 2008-09-21 23:33 jz 阅读(111) 评论(0)  编辑 收藏 引用 所属分类: c++ primer plus 读书笔记