简体   繁体   English

关于继承的困惑

[英]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. 该程序包含一个父类的Bank和两个子类的取款和存款。

#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 您的余额仅为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. 您已成功存入1000。现在余额为1000。

The only variable used by both the child classes was bal , so I decided to declare it globally like this. 两个子类都使用的唯一变量是bal ,因此我决定像这样全局声明它。

#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? 为什么会这样呢?

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 . 一种描述你所谓的bank ,一种描述deposit ,一种描述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 . 您有三个对象: bdw

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 . w不能仅仅找到b What if you had, say, b_barclays , b_natwest and b_hsbc ? 如果您有b_barclaysb_natwestb_hsbc怎么b_hsbc Which would you expect w to find? 您希望w找到哪个? 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. 每个都具有完全独立的存储,按类的定义进行布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM