C++ C++;继承的类未正确初始化
我有一个从另一个类(帐户)继承的类(SavingAccount)。不知何故,它没有正确初始化 ========================================================================C++ C++;继承的类未正确初始化,c++,constructor,C++,Constructor,我有一个从另一个类(帐户)继承的类(SavingAccount)。不知何故,它没有正确初始化 ======================================================================== class Account { private: double balance; public: // constructor Account() { balance = 0; } Accou
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;
}
};
当我跳进去时,acctSaving显示它的平衡为2000。然而,当它到达外部时,余额回到0
如果我更改此行:
Account::Account(b);
进入:
它正确返回对象的余额值为2000。出了什么问题?第一种方法怎么不正确呢?Account::Account(b)
并不像你想象的那样。它创建一个类型为Account
的未命名临时变量,用b
初始化,然后立即销毁
您可能正在寻找以下内容:
SavingAccount(double b, double r)
: Account(b >= 0 && r >= 0 ? b : 0) {
/* rest of code here */
}
另请参见:帐户::帐户(b)
并不像您想象的那样做。它创建一个类型为Account
的未命名临时变量,用b
初始化,然后立即销毁
您可能正在寻找以下内容:
SavingAccount(double b, double r)
: Account(b >= 0 && r >= 0 ? b : 0) {
/* rest of code here */
}
另请参见:不应直接调用父构造函数。但您可以指定在构造子构造函数时应使用哪个父构造函数 当您像函数调用一样直接调用父构造函数时,您将创建一个临时对象
// 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;
}
}
//构造函数
SavingAccount()//此处隐式调用默认父构造函数
{
利率=0;
}
SavingAccount(双b,双r):Account(b)//如果默认构造函数在此处无效,请在此处指定父构造函数
{
如果(b>=0&&r>=0)
{
酯=r;
}
其他的
{
利率=0;
cout不应直接调用父构造函数。但可以指定在构造子构造函数时应使用哪个父构造函数
当您像函数调用一样直接调用父构造函数时,您将创建一个临时对象
// 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;
}
}
//构造函数
SavingAccount()//此处隐式调用默认父构造函数
{
利率=0;
}
SavingAccount(双b,双r):Account(b)//如果默认构造函数在此处无效,请在此处指定父构造函数
{
如果(b>=0&&r>=0)
{
酯=r;
}
其他的
{
利率=0;
这才是我真正需要的。谢谢。这才是我真正需要的。谢谢。