我正在尝试调用此类中的函数 calculateInterest()
// account.h -- handles banking accounts
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
using std::string;
class Account
{
private:
double balance;
public:
Account(double b = 0.0);
double getBalance() {return balance;}
void credit(double c);
void debit(double d);
};
class SavingsAccount : public Account
{
private:
double rate;
public:
SavingsAccount(double r = 0.0);
double calculateInterest();
};
class CheckingAccount : public Account
{
private:
double fee;
public:
CheckingAccount(double f = 0.0);
void credit(double m);
void debit(double m);
};
#endif
到目前为止,我可以执行 Account joe(100);
然后使用 Account
类中的函数,所以我显然没有正确继承它。
最佳答案
class Account
{
private:
double balance;
public:
Account(double b = 0.0);
double getBalance() {return balance;}
void credit(double c);
void debit(double d);
// add this!
virtual double calculateInterest() = 0;
};
您需要在所有派生类中实现 calculateInterest
关于c++ - 在 C++ 类继承中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072084/