c++ - 无法创建抽象类的实例
问题描述
当我尝试创建此类的实例时,此代码出现问题,错误似乎是
not allowed to use the abstracted class "SavingAccount"
我不知道我能做什么。我按照 edx Microsoft Intermediate C++ 给出的步骤进行操作。
银行账户.h
#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;};
银行账户.cpp
#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;}
可冻结的.h
#pragma once
//Pure virtual class ,representing the "freeable" capability
class Freezable {
public:
virtual void freeze()=0;
virtual void unfreeze()=0;
};
可记录的.h
#pragma once
#include <string>
//Pure virtual class, representing the "loggable"'
class loggable{
public:
virtual void log(const std::string& message)const = 0;};
储蓄账户.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;
};
储蓄账户.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();
^^
这与基类不同。因此,您永远不会覆盖派生类中的函数。这就是为什么您需要练习使用覆盖说明符的原因,如果在基类中找不到匹配的函数,它将产生编译器错误。
例如,使用Clag 7.0进行编译显然会产生编译器错误:
prog.cc:76:17: error: 'getGuarranteeLimit' marked 'override' but does not override any member functions
virtual double getGuarranteeLimit()override;
推荐阅读
- mysql - 更新专用于特定订单的列
- php - 单击电子邮件中的激活链接后的 USER 更新
- json - Json 模式验证器,如何验证键是否不是静态的
- javascript - 仅当 display=none(显示 div)时验证输入检查
- ssl - https 重定向有时只有效
- git - 我可以关闭 git checkout 的“你的分支在 N 次提交之前的'origin/whatever'”消息吗?
- javascript - 如何使用ajax将javascript二维数组传输到asp mvc中的控制器?
- c++ - 中心化公平 RW 自旋锁
- python - 将在 python 中创建的表导入到 docx
- android - 在 Android 应用程序中添加“应用程序链接按钮”的正确方法是什么