Blazy
Blazy

Reputation: 23

Confusion regarding inheritance

I was testing out a small program on hierarchical inheritance when I encountered this problem. This program contains a parent class Bank, and two child classes Withdraw & Deposit.

#include<iostream>
#include<conio.h>
#include<stdlib.h>

//Hierarchical Inheritance

using namespace std;

class bank{
        protected:

            char name[20];
            int age, id;
            float bal;


        public:
            void getData(){
                cout <<"Enter your name, age, bank ID and balance" << endl;
                cin >> name >> age >> id >> bal;
            }
            void display(){
                cout <<"Name: " << name << endl << "ID: " << id << endl;
                cout <<"Age: " << age <<endl <<"Balance: " << bal << endl;
            }
            void check(){
                cout <<"Your balance is " << bal << endl;
            }
};

class withdraw : public bank{

        float wd;
    public:
        void withdrawMoney(){
            cout << "Enter the amount to withdraw" << endl;
            cin >> wd;

            if(wd > bal)
                cout << "You cannot withdraw that much. Your balance is only " << bal << endl;
            else{
                bal = bal-wd;
                cout << "You successfully withdrew " << wd << ". Your remaining balance is " << bal << endl;
            }
        }

};

class deposit : public bank{

        float dp;
    public:
        void depo(){
            cout <<"Enter the amount to deposit" << endl;
            cin >> dp;
            bal = bal+dp;
            cout <<"You successfully deposited " << dp << ". Your balance is now " << bal << "." << endl;
        }
};


int main()
{
    int c;

    bank b;
    deposit d;
    withdraw w;

    b.getData();

    do{
        cout <<"***The Dank Bank***" << endl;
        cout <<"What do you want to do?\n 1)Withdraw\n 2)Deposit\n 3)Check Balance\n 4)Display all details\n 5)Exit\n" << endl;
        cin >> c;


          if(c == 1)
                w.withdrawMoney();
          else if (c == 2)
                d.depo();
          else if(c == 3)
                b.check();
          else if(c == 4)
                b.display();
          else if(c == 5)
                exit(0);
          else
                cout <<"Wrong choice." << endl;

          cout<<"Press any key to continue" << endl;
          getch();

    }
     while(1);

    getch();
    return 0;
    }

When carrying out the withdraw function, I get this output:

You cannot withdraw that much. Your balance is only 6.03937e-039

When using the deposit function, the output shows the deposited amount instead of the actual balance.

You successfully deposited 1000. Your balance is now 1000.

The only variable used by both the child classes was bal, so I decided to declare it globally like this.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
float bal;

The program worked without any flaws. But doing this defeats the whole purpose of using inheritance.

I'm confused. Why is this happening?

Upvotes: 0

Views: 127

Answers (1)

Tommy
Tommy

Reputation: 100632

Classes are descriptions of how an object should look and behave. You've three of them. One that describes what you call a bank, one a deposit and one a withdraw.

Objects are instances of classes. So an object is something that has the storage and the behaviour that the class said it should have. You've three objects: b, d and w.

Each object has its own independent storage. If you want an object to know about another object, you need to tell it. w can't just find b. What if you had, say, b_barclays, b_natwest and b_hsbc? Which would you expect w to find? Why?

I think what you've done is conflate classes, which tell the compiler how to create an object, with objects themselves. One class inheriting from another simply says that the subclass will include the behaviour and storage of the parent. So it says that if you create objects of both types then the child will have a superset of the abilities of the parent. It doesn't create any sharing of storage; each has completely independent storage, laid out as defined by the class.

Upvotes: 2

Related Questions