Joh*_* He 0 c++ inheritance class c++11
当我尝试创建此类的实例时,我遇到此代码的问题,似乎出现错误
not allowed to use the abstracted class "SavingAccount"
Run Code Online (Sandbox Code Playgroud)
我不知道我能做什么.我按照edx Microsoft Intermediate C++给出的步骤进行操作.
BankAccount.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;};
Run Code Online (Sandbox Code Playgroud)
BankAccount.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;}
Run Code Online (Sandbox Code Playgroud)
Freezable.h
#pragma once
//Pure virtual class ,representing the "freeable" capability
class Freezable {
public:
virtual void freeze()=0;
virtual void unfreeze()=0;
};
Run Code Online (Sandbox Code Playgroud)
Loggable.h
#pragma once
#include <string>
//Pure virtual class, representing the "loggable"'
class loggable{
public:
virtual void log(const std::string& message)const = 0;};
Run Code Online (Sandbox Code Playgroud)
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;
};
Run Code Online (Sandbox Code Playgroud)
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; }
Run Code Online (Sandbox Code Playgroud)
你有一个错字.基类BankAccount是纯抽象类,它具有一个名为的虚拟成员函数
virtual double getGuaranteeLimit() = 0;
^^
Run Code Online (Sandbox Code Playgroud)
在你的SavingAccount班级(派生的)中你实施了
virtual double getGuarranteeLimit();
^^
Run Code Online (Sandbox Code Playgroud)
这与基类不同.因此,您永远不会覆盖派生类中的函数.这就是为什么你需要练习覆盖说明符,如果在基类中找不到匹配的函数,它将产生编译器错误.
例如,请参阅使用Clag 7.0进行编译,清楚地给出了编译器错误:
prog.cc:76:17: error: 'getGuarranteeLimit' marked 'override' but does not override any member functions
virtual double getGuarranteeLimit()override;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |