首页 新闻 会员 周边 捐助

c++关于虚函数

0
悬赏园豆:5 [待解决问题]

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) { }

virtual void Withdraw(double amt);
virtual void ViewAcct()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);

virtual void ViewActt()const;
virtual void Withdraw(double amt);

void ResetMax(double m) { maxLoan = m; }
void ResetRate(double r) { rate = r; }
void ResetOwes() { 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::string temp;
long tempnum;
double tempbal;
char kind;

for (int i = 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] = new Brass(temp, tempnum, tempbal);
    else
    {
        double tmax, trate;
        cout << "enter thr overdraft limit:$";
        cin >> tmax;
        cout << "enter the interest rate"
            << "as a decimal fraction";
        cin >> trate;
        p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
    }
    while (cin.get() != '\n')
        continue;
}
cout << endl;
for (int i = 0; i < CLIENTS; i++)
{
    p_clients[i]->ViewAcct();
    cout << endl;
}
for (int i = 0; i < CLIENTS; i++)
{
    delete p_clients[i];
}
cout << "done\n";
return 0;

}

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

c++
不知道的存在、的主页 不知道的存在、 | 初学一级 | 园豆:197
提问于:2019-01-10 20:01
< >
分享
所有回答(1)
0

全部敲了一边,发现代码里存在命名错误,Acct类的ViewAcct虚函数与BrassPlus类里的ViewActt命名不一样,这一般眼劲都看不出来,修改以下BrassPlus里的函数名就可以了。

ColdplayFan | 园豆:215 (菜鸟二级) | 2019-10-10 14:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册