基类和派生类私有成员变量用法

时间:2023-01-15 15:27:05

I've developed a compiling bank system of different accounts. My base class is Account, and derived classes are Checking, Savings, MoneyMarket. The latter three inherit private member variable 'balance' from Account. All four accounts need to maintain and modify their own 'balance'.

我开发了一个不同账户的编制银行系统。我的基类是Account,派生类是Checking,Savings,MoneyMarket。后三者从Account继承私有成员变量'balance'。所有四个账户都需要维护和修改自己的“余额”。

However, I'm confused about the relation between Account's balance and derived class's 'balance'.

但是,我对账户余额与派生类的“余额”之间的关系感到困惑。

As you can see in Checking's getBalance(), it is forced to use Account's getBalance() due to the private variable, and the code only works when it displays Account::balance. This seems very strange, that it should call Account's balance to display it's own.

正如您在Checking的getBalance()中所看到的,由于私有变量,它被强制使用Account的getBalance(),并且代码仅在显示Account :: balance时才有效。这看起来很奇怪,它应该调用Account的余额来显示它自己的。

Please note that that all of Account's public methods are virtual to allow override.

请注意,所有Account的公共方法都是虚拟的,以允许覆盖。

Why does this work the way it is? Shouldn't the derived classes call their own copy of 'balance'?

为什么这样的工作方式呢?派生类不应该调用自己的“余额”副本吗?

Note: this code works and correctly displays the exact modified balance for each object. Below is Checking.cpp:

注意:此代码可以正常显示每个对象的精确修改余额。以下是Checking.cpp:

#include <iostream>
#include <iomanip>
#include "Checking.h"
using namespace std;

Checking::Checking() {setBalance(500); }

Checking::~Checking() {setBalance(0);}

void Checking::Withdrawal(double p_withdrawal){
    setBalance( getBalance(0) - p_withdrawal);
    cout << setprecision(2) << fixed;
    cout<<"\nWithdrawal from Checking leaves balance: "<<getBalance(0);
}

double Checking::getBalance(bool print){
    if (print==1)
        cout<<"\nBalance of Checking:"<< Account::getBalance(0);
    return Account::getBalance(1);
}

And for Account.h:

对于Account.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
using namespace std;

class Account{
public:
    Account();
    ~Account();

    virtual double getBalance(bool);
    virtual void setBalance(double);

    virtual void Deposit(double);
    virtual void Withdrawal(double);

    virtual void Transfer(Account&, Account&, double); 

private:
    double balance;

};

#endif 

1 个解决方案

#1


1  

private means non accessible from outside the class scope it is defined in, not even in children classes, if you want to directly access balance within your children classes make it protected not private.

私有意味着无法从其定义的类范围之外访问,即使在子类中也是如此,如果您希望直接访问子类中的平衡,则将其保护为非私有。

Also note that inheritance (at least publicinheritance) means that your child class IS A parent class from the outside world and is useable as a parent class. Thus there is no "copy" of balance in your child class, there is one and only one balance in each instance. The balance field belongs to the Account class, whether this class is extended by inheritance or not does not change that.

另请注意,继承(至少是publicinheritance)意味着您的子类是来自外部世界的父类,可用作父类。因此,您的子类中没有“副本”余额,每个实例中只有一个余额。 balance字段属于Account类,无论此类是否通过继承扩展都不会更改。

The logic is : Checking IS AN Account (from an outside the class point of view), Account has a balance thus Checking has a balance. There is only one balance which is privately controlled by the Account part of the Checking.

逻辑是:检查IS AN帐户(从类外的角度来看),帐户有一个余额,因此检查有余额。只有一个余额由支票的账户部分私下控制。

#1


1  

private means non accessible from outside the class scope it is defined in, not even in children classes, if you want to directly access balance within your children classes make it protected not private.

私有意味着无法从其定义的类范围之外访问,即使在子类中也是如此,如果您希望直接访问子类中的平衡,则将其保护为非私有。

Also note that inheritance (at least publicinheritance) means that your child class IS A parent class from the outside world and is useable as a parent class. Thus there is no "copy" of balance in your child class, there is one and only one balance in each instance. The balance field belongs to the Account class, whether this class is extended by inheritance or not does not change that.

另请注意,继承(至少是publicinheritance)意味着您的子类是来自外部世界的父类,可用作父类。因此,您的子类中没有“副本”余额,每个实例中只有一个余额。 balance字段属于Account类,无论此类是否通过继承扩展都不会更改。

The logic is : Checking IS AN Account (from an outside the class point of view), Account has a balance thus Checking has a balance. There is only one balance which is privately controlled by the Account part of the Checking.

逻辑是:检查IS AN帐户(从类外的角度来看),帐户有一个余额,因此检查有余额。只有一个余额由支票的账户部分私下控制。