Snc
Snc

Reputation: 11

Inherited overridden method not being called

Why does the depositmoney() method within the operation method of the account class call the depositmoney() of the account class and not the one from the Sav_account class? The direct call to depositmoney() method calls method of the sub-class which is obvious. But can't understand why the indirect call from operations() does not give the expected result.

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int temp = 0;

class account
{
protected:
    string name;
    double balance;
    int AccNo;
public:
    void operations()
    {
        int r = 0;
        do{
            cout << "1.DEPOSIT" << endl;
            cout << "2.WITHDRAW" << endl;
            cout << "3.CHECK BALANCE" << endl;
            cout << "0.EXIT" << endl;
            cin >> r;
            switch(r)
            {
            case 1:this->depositmoney();
                break;
            case 2:this->withdraw();
                break;
            case 3:this->displaybalance();
                break;
            case 0:cout << "Exiting" << endl;
                break;
            default:
                break;
            }
        }
        while(r != 0);
    }
    account(string Name, double bal)
    {
        name = Name;
        balance = bal;
        AccNo = temp++;
    }
    void displaybalance()
    {
        cout << "name :" << name << endl;
        cout << "A/C" << AccNo << endl;
        cout << "your balance is " << balance << endl;
    }
    void depositmoney()
    {
        float deposit;
        cout << "enter deposit" << endl;
        cin >> deposit;
        balance += deposit;
    }
protected:
    void withdraw()
    {
        float withdrawal;
        cout << "enter witdrawal amount" << endl;
        cin >> withdrawal;
        if(balance >= withdrawal)
        {
            balance -= withdrawal;
        }
        else
        {
            cout << "insufficient funds" << endl;
        }
    }
};

class Sav_account :public account
{
private:
    double const CI = 5;
    void depositinterest()
    {
        balance += balance*CI / 100;
    }
public:
    Sav_account(string Name, double bal) :account(Name, bal)
    {
        AccType = 0;
    }
    void depositmoney()
    {
        account::depositmoney();
        depositinterest();
    }
};

void main()
{
    Sav_account *account1 = new Sav_account("Shaw", 50000);
    account1->operations();// DEPOSIT RESULTS IN NO INTEREST
    account1->displaybalance();
    account1->depositmoney(); // **DEPOSIT FUNCTION OF Sav_account CALLS                      interest function**
    account1->displaybalance();
    _getch();
}

Upvotes: 0

Views: 95

Answers (2)

M.M
M.M

Reputation: 141598

Your class is not polymorphic. You have no polymorphic functions and have not overridden anything. (You have hidden functions instead). As such, calling depositmoney(); from a function of account will call account::depositmoney().

To make a class polymorphic you need to use virtual functions. Any function that you want to have polymorphic behaviour needs to be declared as virtual in the base class, e.g. in account here:

virtual void depositmoney() {

In the derived class, since C++11, you can write:

void depositmoney() override
                    ^^^^^^^^

which will cause a compiler error if you accidentally hide a function when you are trying to override it. (Although I'm guessing that you are using a compiler that doesn't support C++11, since most of those will also reject void main).


Also, any polymorphic base class should have a virtual destructor, i.e. virtual ~account() {} otherwise the code account *x = new Sav_account("Shaw", 50000); delete x; would cause undefined behaviour.

void main is illegal, main must have a return type of int.

Upvotes: 1

user1084944
user1084944

Reputation:

There aren't any overridden methods in your classes; you haven't made any of them virtual.

Upvotes: 1

Related Questions