正如标题所说,我在子类构造函数中调用基类构造函数时遇到了一些问题
基地:
account.h
Account(double, Customer*)
account.cpp
Account::Account(double b, Customer *cu)
{
balance = b;
cust = *cu;
}
子类:
savings.h
Savings(double);
savings.cpp
Savings::Savings(double intRate) : Account(b, cu)
{
interestRate = intRate;
}
我收到的错误是 b 和 cu 未定义。
感谢帮助
想想您如何创建一个SavingsAccount
。
你能创建一个使用吗
SavingsAccount ac1(0.01);
如果您这样做,该对象的余额是多少?谁将成为该对象的客户
。
创建 SavingsAccount
时,您需要提供余额以及 Customer
。像这样的东西:
Customer* cu = new Customer; // Or get the customer based on some other data
SavingsAccount ac1(100.0, cu, 0.01);
有道理。您正在提供 SavingsAccount
所需的所有数据。要创建这样的对象,您需要适本地定义 SavingsAccount
的构造函数。
Savings::Savings(double b, Customer *cu, double intRate);
这可以通过以下方式正确实现:
Savings::Savings(double b,
Customer *cu,
double intRate) : Account(b, cu), interestRate(intRate) {}