-3

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!

christina
  • 29
  • 2
  • 5
    `virtual bool withdraw(double money)=0;` -- you should remove the `=0` from the overriding function. Also, [`override`](http://en.cppreference.com/w/cpp/language/override) is your friend :) – Quentin Jul 04 '17 at 21:27
  • @Quentin that should be an (accepted) answer! – Gian Paolo Jul 04 '17 at 21:29
  • @GianPaolo SO discourages building up Q&A's around typos -- OP will now be on her merry way but this won't ever help anyone else. In fact, it's a close vote reason. – Quentin Jul 04 '17 at 21:31
  • right, @Quentin, thinking more I agree with you – Gian Paolo Jul 04 '17 at 21:32
  • As @Quentin says, you are implementing a function which is anyway declared as pure virtual, so your `MoneyMarketAccount` is abstract anyway. To me, it sounds weird that compiler allows the implementation of a pure virtual function, but that is. See [this answer](https://stackoverflow.com/a/2089176/3276027) for more info – Gian Paolo Jul 04 '17 at 21:35
  • @Gian Paolo - Implementing a pure virtual can make perfect sense. Consider for example that you have a base class with a function that you want to force derived classes to implement but you also want to provide a base implementation that they can call and then expand upon (or not). You can do that by making it pure in the base and also provode an implementation that derived classes can call as base::foo(). – Jesper Juhl Jul 04 '17 at 21:39
  • @JesperJuhl, I understand it can make sense (in some quite particular scenario), but still sounds weird to me. Quoting the answer already quoted above, _Note that even though it's permitted by the language, it's not something that I see commonly used (and the fact that it can be done seems to surprise most C++ programmers, even experienced ones)._ – Gian Paolo Jul 04 '17 at 21:47
  • @Quentin Thank you, that worked perfectly! – christina Jul 04 '17 at 21:47
  • @Quentin "OP will now be on her merry way but this won't ever help anyone else" - Having just been helped by this post, I do disagree with this notion. – antipattern Jul 27 '21 at 12:17

1 Answers1

0

This:

virtual bool withdraw(double money)=0;

(In MoneyMarketAccount - the derived class) declares the function as pure (just as it was pure in the base class). Even though you implement it (so it can be called - yes, pure virtual functions can have implementations and it does make sense sometimes), this still makes the MoneyMarketAccount abstract - and you cannot instantiate a abstract class.

You probably wanted to declare the function as

bool withdraw(double money) override;

in MoneyMarketAccount. This will make the class concrete since there are no longer any pure (=0) functions and will also let the compiler help you in the future if the base class signature changes so you no longer override the pure virtual withdraw from it (the override bit - which makes the redundant virtual in the derived class extra redundant).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70