[英]C++ inherited class not initialized properly
I have a class (SavingAccount) inherited from another class (Account). 我有一个从另一个类(Account)继承的类(SavingAccount)。 Somehow, it does not initialize properly. 不知何故,它无法正确初始化。
======================================================================== ================================================== ======================
class Account
{
private:
double balance;
public:
// constructor
Account()
{
balance = 0;
}
Account(double b)
{
if (b >= 0)
balance = b;
else
{
balance = 0;
cout << "Balance cannot be negative." << endl;
}
}
// destructor
~Account()
{
// nothing
}
// required functions
bool credit(double a)
{
if (a > 0)
{
balance += a;
return true;
}
else
{
cout << "Credit amount must be greater than 0." << endl;
return false;
}
}
bool debit(double a)
{
if (a > 0 && a <= balance)
{
balance -= a;
return true;
}
else if (a > balance)
{
cout << "Debit amount exceeded account balance." << endl;
return false;
}
}
double getBalance()
{
return balance;
}
void setBalance(double b)
{
balance = b;
}
};
======================================================================== ================================================== ======================
class SavingAccount : public Account
{
private:
double interestRate; // percentage
public:
// constructor
SavingAccount()
{
Account::Account();
interestRate = 0;
}
SavingAccount(double b, double r)
{
if (b >= 0 && r >= 0)
{
Account::Account(b);
interestRate = r;
}
else
{
Account::Account();
interestRate = 0;
cout << "Balance or rate cannot be negative." << endl;
}
}
// destructor
~SavingAccount()
{
// nothing
}
// required functions
double calculateInterest()
{
return Account::getBalance() * interestRate / 100;
}
};
======================================================================== ================================================== ======================
I intialize as follow: 我初始化如下:
SavingAccount acctSaving(2000, 5);
acctSaving shows its balance as 2000 when I jump inside it. 当我跳进acctSaving时,其余额显示为2000。 However, when it gets outside, the balance is back to 0. 但是,当它到达外面时,余额将回到0。
If I change this line: 如果我更改此行:
Account::Account(b);
into: 变成:
setBalance(b);
It returns the object properly with the balance value of 2000. What goes wrong? 它会正确返回余额值为2000的对象。出了什么问题? How is the first method not right? 第一种方法不正确吗?
Account::Account(b)
doesn't do what you seem to think it does. Account::Account(b)
并没有您认为的那样。 It creates an unnamed temporary variable of type Account
, initialized with b
, and then immediately destroyed. 它创建一个类型为Account
的未命名临时变量,使用b
初始化,然后立即销毁。
What you are likely looking for is something like this: 您可能正在寻找的是这样的:
SavingAccount(double b, double r)
: Account(b >= 0 && r >= 0 ? b : 0) {
/* rest of code here */
}
See also: member initializer list 另请参阅: 成员初始化器列表
You should not call parent constructors directly. 您不应直接调用父构造函数。 But you can specify which parent constructor should be used when a child is being constructed. 但是,您可以指定在构造子级时应使用哪个父级构造函数。
When you call a parent constructor directly like a function call, you create a temporary object. 当像函数调用一样直接调用父构造函数时,会创建一个临时对象。
// constructor
SavingAccount() // Default parent constructor is called here implicitly
{
interestRate = 0;
}
SavingAccount(double b, double r): Account(b) // Specify here parent constructor if default one is useless here
{
if (b >= 0 && r >= 0)
{
interestRate = r;
}
else
{
interestRate = 0;
cout << "Balance or rate cannot be negative." << endl;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.