欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

类和继承(c++primer plus 学习笔记13章)

程序员文章站 2023-12-22 20:04:58
...

1.派生类和基类是一种is-a的关系,即派生类对象也是一个基类对象,可以对基类对象执行任何操作。

2.多态:同一个方法(c++中称为函数)在派生类和基类中的行为方式是不同的,换句话说就是方法的行为处决于调用该方法的对象。例如一个称为clean的方法,有的类执行扫地,有的类执行擦窗,有的类执行倒垃圾......但都是调用这个clean的方法。

两个重要的机制可以实现多态

(1).在派生类中重新定义基类的方法。

(2).使用虚方法。如果是通过引用或指针调用,程序将根据引用或指针指向的对象类型来选择方法。

3.派生类构造函数在初始化基类私有数据时,采用的是成员初始化列表语法

BrassPlus::BrassPlus(const std::string &s, long an, double bal, double ml, double r):Brass(s,an,bal)

brass 源码

1.brass.h 头文件

#ifndef BRASS_H
#define BRASS_H
#include<string>
 
class Brass
{
private:
    std::string fullName;
    long acctNum;
    double balance;
public:
    Brass(const std::string &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(){}
};
 
#endif // BRASS_H

1.brass.h 源文件

#include "brass.h"
#include<iostream>
using namespace std;
//确定小数格式及精度
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f,precis p);
Brass::Brass(const std::string &s, long an, double bal)
{
  fullName=s;
  acctNum=an;
  balance=bal;
}
//存款
void Brass::Deposit(double amt)
{
  if(amt<0)
      cout<<"Negative deposit not allowed; "
         <<"deposit is cancelled.\n";
  else
      balance +=amt;
}
//取款
void Brass::Withdraw(double amt)
{
   format initialState = setFormat();
   precis prec=cout.precision(2); //precision 准确
   //positive 正数
   if (amt<0)
       cout<<"Withdrawal amout must be positive;"
          <<"withdrawal canceled.\n";
   else if (amt<=balance)//取款数小于余额
       balance-=amt;
   else
       cout<<"Withdrawal amout of $"<<amt
          <<"exceeds your balance.\n"
         <<"withdrawal canceled.\n";
           restore(initialState,prec);
}
 
double Brass::Balance() const
{
  return balance;
 
}
 
void Brass::ViewAcct() const
{
    format initialState = setFormat();
        precis prec = cout.precision(2);
        cout << "Client: " << fullName << endl;
        cout << "Account Number: " << acctNum << endl;
        cout << "Balance: $" << balance << endl;
        restore(initialState, prec); // Restore original format
}
format setFormat()
{
    // set up ###.## format 设置显示格式
    return cout.setf(std::ios_base::fixed,
                std::ios_base::floatfield);
}
 
void restore(format f, precis p)
{
    cout.setf(f, std::ios_base::floatfield);
    cout.precision(p);//精度
}

brassplus 头文件

#ifndef BRASSPLUS_H
#define BRASSPLUS_H
#include"brass.h"
#include<iostream>
using namespace std;
class BrassPlus : public Brass
{
private:
    double maxLoan;
    double rate;
    double owesBank;
public:
    BrassPlus(const std::string &s="Nullbody",long an=-1,
              double bal=0.0,double ml=500,double r=0.11125);
    BrassPlus(const Brass &ba,double ml=500, double r=0.11125);
    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 // BRASSPLUS_H
brassplus 源文件
#include "brassplus.h"
#include"brass.h"
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f, precis p);
BrassPlus::BrassPlus(const std::string &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
{
    format initialState = setFormat();
        precis prec = cout.precision(2);
 
        Brass::ViewAcct();   // display base portion  如果没写Brass::制定域将会无穷递归调用
        cout << "Maximum loan: $" << maxLoan << endl;
        cout << "Owed to bank: $" << owesBank << endl;
        cout.precision(3);  // ###.### format
        cout << "Loan Rate: " << 100 * rate << "%\n";
        restore(initialState, prec);
}
 
void BrassPlus::Withdraw(double amt)
{
    format initialState = setFormat();
        precis prec = 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";//Credit贷款
        restore(initialState, prec);
}
 

上一篇:

下一篇: