首页 > 解决方案 > 无法创建抽象类的实例

问题描述

当我尝试创建此类的实例时,此代码出现问题,错误似乎是

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; }

标签: c++classc++11inheritance

解决方案


你有一个错字。基类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;

推荐阅读