尝试创建此类的实例时,此代码存在问题,似乎是一个错误
not allowed to use the abstracted class "SavingAccount"
我不知道该怎么办。我按照edx Microsoft Intermediate C ++给出的步骤进行操作。
银行账户
#pragma once
#include <string>
class BankAccount
{
protected:
double balance;
public:
BankAccount(double initialBanlance);
virtual ~BankAccount();
double getBalance() const;
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual std::string getTermAndConditions() = 0;
virtual double getGuaranteeLimit() = 0;};
银行账户
#include "BankAccount.h"
BankAccount::BankAccount(double initialBanlance)
:balance(initialBanlance)
{}
BankAccount::~BankAccount()
{}
double BankAccount::getBalance() const
{return balance;}
void BankAccount::deposit(double amount)
{
balance += amount;
}
void BankAccount::withdraw(double amount)
{balance -= amount;}
Freezable.h
#pragma once
//Pure virtual class ,representing the "freeable" capability
class Freezable {
public:
virtual void freeze()=0;
virtual void unfreeze()=0;
};
可记录的
#pragma once
#include <string>
//Pure virtual class, representing the "loggable"'
class loggable{
public:
virtual void log(const std::string& message)const = 0;};
SavingAccount.h
#pragma once
#include "BankAccount.h"
#include "Freezable.h"
#include "Loggable.h"
#include <list>
class SavingAccount :public BankAccount, public Freezable, public loggable
{
private:
double interestRate;
bool frozen;
public:
SavingAccount(double initialBalance, double interestRate = 0.0);
virtual ~SavingAccount();
void earnInterest();
virtual void deposit(double amount);
virtual void withdraw(double amount);
//implement pure virtual function from BankAccount class.
virtual std::string getTermAndConditions();
virtual double getGuarranteeLimit();
//Implement pure virtual from Freezable
virtual void freeze();
virtual void unfreeze();
//Implement pure virtual from Loggable class
virtual void log(const std::string & message)const;
};
SavingAccount.cpp
#include "SavingAccount.h"
#include <iostream>
SavingAccount::SavingAccount(double initialBalance, double interestRate)
:BankAccount(initialBalance), interestRate(interestRate), frozen(false)
{}
SavingAccount::~SavingAccount() {}
void SavingAccount::earnInterest()
{
if (!frozen) {
double interest = balance * (interestRate / 100);
deposit(interest);
}
}
void SavingAccount::deposit(double amount) {
if (!frozen) {
BankAccount::deposit(amount);
log("Deposit:" + std::to_string(amount));
}
}
void SavingAccount::withdraw(double amount) {
if (!frozen && amount <= balance) {
BankAccount::withdraw(amount);
log("withdrwal:" + std::to_string(amount));
}
}
std::string SavingAccount::getTermAndConditions() {
return "This is a savings account, You cannot go overdrawn.You earn interest.";
}
double SavingAccount::getGuarranteeLimit() { return 1000000; }
void SavingAccount::freeze() { frozen = true; }
void SavingAccount::unfreeze() { frozen = false; }
void SavingAccount::log(const std::string & message) const
{ std::cout << message << std::endl; }
最佳答案
你有错字基类BankAccount
是纯抽象类,它具有一个称为的虚拟成员函数
virtual double getGuaranteeLimit() = 0;
^^
并在您的
SavingAccount
类(派生的)中实现了virtual double getGuarranteeLimit();
^^
这与基类不同。因此,您永远不会在派生类中覆盖该函数。
这就是为什么您需要练习override specifier的原因,如果在基类中找不到匹配的函数,这将产生编译器错误。
例如,参见使用Clag 7.0进行编译显然会产生编译器错误:
prog.cc:76:17: error: 'getGuarranteeLimit' marked 'override' but does not override any member functions
virtual double getGuarranteeLimit()override;
关于c++ - 无法创建抽象类的实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53138297/