I'm writing code using inheritance, with a base class called BankAccount and a child class called MoneyMarket Account, and I'm receiving the following error for my code
hw7main.cpp: In function ‘int main()’:
hw7main.cpp:9: error: cannot declare variable ‘account1’ to be of abstract type ‘MoneyMarketAccount’
hw7.h:36: note: because the following virtual functions are pure within ‘MoneyMarketAccount’:
hw7.h:41: note: virtual bool MoneyMarketAccount::withdraw(double)
Based on other the answers to similar questions, I tried to override the virtual function, withdraw, but I'm still getting the same error.
Here is my base class interface (file name: hw7base.h):
#ifndef HW7BASE_H
#define HW7BASE_H
#include <iostream>
#include <string>
using namespace std;
class BankAccount
{
public:
//constructors
BankAccount();
//member functions
bool deposit(double money);
virtual bool withdraw (double money)=0;
//accessor functions
void getName(BankAccount* account);
void getBalance(BankAccount* account);
//transfer function
//virtual void transfer (BankAccount* deposit, BankAccount* withdrawal, double money);
//private:
string name;
double balance;
};
#endif
Here is my subclass interface (file name: hw7derived1.h):
#ifndef HW7DERIVED1_H
#define HW7DERIVED1_H
#include <iostream>
#include "hw7base.h"
using namespace std;
class MoneyMarketAccount: public BankAccount
{
public:
//constructor
MoneyMarketAccount();
//override function
virtual bool withdraw(double money)=0;
int number_of_withdrawals;
};
#endif
And here is my subclass implementation (file name: hw7derived1.cpp):
#include "hw7derived1.h"
using namespace std;
//consturctor
MoneyMarketAccount::MoneyMarketAccount()
{
number_of_withdrawals = 0;
}
bool MoneyMarketAccount::withdraw(double money)
{
if (money<=0)
{
cout<<"Failed.\n";
return false;
}
else
{
balance-=money;
if(number_of_withdrawals>=2)
{
balance-=1.5;
if (balance<0)
{
balance=balance+1.5+money;
cout<<"Failed.\n";
return false;
}
else
{
number_of_withdrawals++;
cout<<"Success.\n";
return true;
}
}
else
{
if (balance<0)
{
balance+=money;
cout<<"Failed.\n";
return false;
}
else
{
number_of_withdrawals++;
cout<<"Success.\n";
return true;
}
}
}
}
Any help/insight is appreciated, thanks!