gpt4 book ai didi

c++ - 继承... C++ 语法

转载 作者:行者123 更新时间:2023-11-30 02:07:37 24 4
gpt4 key购买 nike

我必须创建一个简单的类,它有几个子类,其中一个子类有它自己的子类。

AccountClass.h

#ifndef Account_h
#define Account_h

class Account{

public:
//Account(){balance = 0.0;}; Here's where the problem was
Account(double bal=0.0){ balance = bal;};
double getBal(){return balance;};

protected:
double balance;
};
#endif

SavingsAccount.h

#ifndef SavingsAccount_h
#define SavingsAccount_h
#include "AccountClass.h"
class SavingsAccount : public Account{

public:
//SavingsAccount(); And here
SavingsAccount(double bal = 0.0, int pct = 0.0){ balance = bal; rate = pct; } : Account(bal);
void compound(){ balance *= rate; };
void withdraw(double amt){balance -= amt;};

protected:
double rate;
};

#endif

CheckingAccount.h

#ifndef CheckingAccount_h
#define CheckingAccount_h
#include "AccountClass.h"
class CheckingAccount : public Account{

public:
//CheckingAccount(); Here also
CheckingAccount(double bal = 0.0, double lim = 500.0, double chg = 0.5){ balance = bal; limit = lim; charge = chg;} : Account(bal);
void cash_check(double amt){ balance -= amt;};

protected:
double limit;
double charge;
};

#endif

TimeAccount.h

#ifndef TimeAccount_h
#define TimeAccount_h
#include "SavingsAccount.h"
class TimeAccount : public SavingsAccount{

public:
//TimeAccount(); ;)
TimeAccount(double bal = 0.0, int pct = 5.0){ balance = bal; rate = pct;} : SavingsAccount(bal,pct);
void compound(){ funds_avail += (balance * rate); };
void withdraw(double amt){funds_avail -= amt;};

protected:
double funds_avail;
};

#endif

我不断收到错误消息,指出已定义默认构造函数以及所有不存在的变量...

帮助!

编辑:

修复了预处理器 ifndef 的

另外,这个问题已经解决了。谢谢@Coincoin!

最佳答案

您没有定义默认构造函数,但是当您派生新类时默认会调用它们。看起来 TimeAccount 将是该问题的罪魁祸首,因为它使用 SavingsAccount 的默认构造函数。然而,它们都应该被定义,而不仅仅是声明。

关于c++ - 继承... C++ 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7612072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com