ITPub博客

首页 > 应用开发 > > c++关于虚函数

c++关于虚函数

作者:founderwzh 时间:2019-08-13 14:30:01 0 删除 编辑

ifndef HEAD

define HEAD

pragma once

pragma warning(disable:4996)

include "pch.h"

include <iostream>

include<string>

include<time.h>

include<vector>

include<array>

include<cctype>

include<fstream>

include<cstdlib>

include<cmath>

include<array>

include<new>

include<cctype>

include<cstdlib>

include<ctime>

include<cstring>

class Acct

{

private:

std::string fullName;

long acctNum;

double balance;

protected:

struct Formatting

{

std::ios_base::fmtflags flag;

std::streamsize pr;

};

const std::string&FullName()const { return fullName; }

long AcctNum()const { return acctNum; }

Formatting SetFormat()const;

void Restore(Formatting &f)const;

public:

Acct(const std::string & s = "nullbody", long an = -1, double bal = 0.0);

void Deposit(double amt);

virtual void Withdraw(double amt) = 0;

double Balance() const { return balance; };

virtual void ViewAcct()const = 0;

virtual ~Acct() {}

};

class Brass :public Acct

{

public:

Brass(const std::string & s = "nullbody", long an = -1, double bal = 0.0) :Acct(s, an, bal) { }

virtualvoidWithdraw(doubleamt);virtualvoidViewAcct()const;virtual~Brass() { }

};

class BrassPlus :public Acct

{

private:

double maxLoan;

double rate;

double owesBank;

public:

BrassPlus(const std::string & s = "nullbody", long an = -1, double bal = 0.0, double m1 = 500, double r = 0.10);

BrassPlus(const Brass& ba, double m1 = 500, double r = 0.1);

virtualvoidViewActt()const;virtualvoidWithdraw(doubleamt);voidResetMax(doublem){ maxLoan = m; }voidResetRate(doubler){ rate = r; }voidResetOwes(){ owesBank =0; }

};

endif

include "pch.h"

include "head.h"

using std::cout;

using std::cin;

using std::endl;

using std::string;

using std::ios_base;

BrassPlus::BrassPlus(const string& s, long an, double bal, double m1, double r) :Acct(s, an, bal)

{

maxLoan = m1;

owesBank = 0.0;

rate = r;

}

BrassPlus::BrassPlus(const Brass& ba, double m1, double r) :Acct(ba)

{

maxLoan = m1;

owesBank = 0.0;

rate = r;

}

void BrassPlus::ViewActt()const

{

Formatting f = SetFormat();

cout << "brass client:" << FullName() << endl;

cout << "amount number:" << AcctNum() << endl;

cout << "Balance $" << Balance() << endl;

cout << "maximum loan:$" << maxLoan << endl;

cout << "owed to bank:" << owesBank << endl;

cout.precision(3);

cout << "loan rate :" << 100 * rate << "%\n";

Restore(f);

}

void Brass::Withdraw(double amt)

{

if (amt < 0)

cout << "withdrawl amount must be positive"

<< "withdral canceled.\n";

else if (amt <= Balance())

Acct::Withdraw(amt);

else

cout << "withdrawal amount of $" << amt

<< "exceeds your balance.\n"

<< "withdrawl canceled.\n";

}

void BrassPlus::Withdraw(double amt)

{

Formatting f = SetFormat();

double bal = Balance();

if (amt <= bal)

Acct::Withdraw(amt);

else if (amt <= bal + maxLoan - owesBank)

{

double advance = amt - bal;

owesBank += advance * (1.0 + rate);

cout << "bank advance" << advance << endl;

cout << "finace charge:" << advance * rate << endl;

Deposit(advance);

Acct::Withdraw(amt);

}

}

void Brass::ViewAcct()const

{

Formatting f = SetFormat();

cout << "brass client:" << FullName() << endl;

cout << "amount number:" << AcctNum() << endl;

cout << "Balance $" << Balance() << endl;

Restore(f);

}

Acct::Acct(const std::string & s, long an, double bal)

{

fullName = s;

acctNum = an;

balance = bal;

}

void Acct::Deposit(double amt)

{

if (amt < 0)

cout << "negative deposit not allowed:"

<< "deposit is cancelled.\n";

else

balance += amt;

}

void Acct::Withdraw(double amt)

{

balance -= amt;

}

Acct::Formatting Acct::SetFormat()const

{

Formatting f;

f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);

f.pr = cout.precision(2);

return f;

}

void Acct::Restore(Formatting &f)const

{

cout.setf(ios_base::fixed, ios_base::floatfield);

cout.precision(f.pr);

}

// TEST.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

include"pch.h"

include"head.h"

const int CLIENTS = 4;

int main()

{

using std::cin;

using std::cout;

using std::endl;

Acct * p_clients[CLIENTS];std::stringtemp;longtempnum;doubletempbal;charkind;for(inti =0; i < CLIENTS; i++){cout<<"enter client's name:"; getline(cin, temp);cout<<"enter client's account number";cin>> tempnum;cout<<"enter opening balance :$";cin>> tempbal;cout<<"enter 1 for brass account or"<<"2 for brassplus account";while(cin>> kind && (kind !='1'&&kind !='2'))cout<<"enter either 1 or 2";if(kind =='1') p_clients[i] =newBrass(temp, tempnum, tempbal);else{doubletmax, trate;cout<<"enter thr overdraft limit:$";cin>> tmax;cout<<"enter the interest rate"<<"as a decimal fraction";cin>> trate; p_clients[i] =newBrassPlus(temp, tempnum, tempbal, tmax, trate); }while(cin.get() !='\n')continue;}cout<ViewAcct();cout<

}

这个brassplus 的view方法我实现了,怎么还显示为纯虚拟没有替代项

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4605/viewspace-3122889/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    159
  • 访问量
    738