I have to make a trivial class that has a few subclasses and one of those subclasses has it's own sublcass.
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 开发者_StackOverflow中文版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
I keep getting errors that default constructors are defined as well as all the variables not existing...
Help!
Edit:
Fixed the preprocessor ifndef's
Also, this was solved. Thanks @Coincoin!
You didn't define your default constructors, but they're being called by default when you derive new classes. It looks like TimeAccount will be the culprit of that issue since it uses the default constructor of SavingsAccount. They all however, should be defined and not just declared.
There are multiple errors in the way you defined your constructors.
- Some of them don't have a body defined
- The intialisation lists are syntaxicaly not at the right place
- You try to define two default constructor by using default valued parameters.
The initialisation list goes between the signature and the body.
Like so:
SavingsAccount(double bal = 0.0, int pct = 0.0)
:Account(bal)
{
balance = bal; rate = pct;
}
Also, you already have a default constructor yet you have parameter list with all parameters with default values, which means you are trying to define two default constructors. You either need to remove the default value for the bal parameter or remove the default constructor and use the entirely default valued constructor as the default constructor.
Like so:
Account(){balance = 0.0;};
Account(double bal){ balance = bal;}
or
Account(double bal=0.0){ balance = bal;}
These:
SavingsAccount();
CheckingAccount();
TimeAccount();
need to have definitions so:
SavingsAccount(){};
CheckingAccount(){};
TimeAccount(){};
should work
If you have defaults for all the constructor's arguments, how does the compiler know which to call when you write:
TimeAccount t;
You are probably seeing "ambiguous function call" type errors. Also, why is pct
an int in TimeAccount()
? The default argument is a double
and it stores that value into a field of type double
.
You need to write default constructors for everything. Plus make sure you obey the rule of three if you need to define copy constructor/destructor/assignment operator.
TimeAccount():
bal(0),
pct(5.0)
{
}
TimeAccount(double bal, int pct)
{
balance = bal;
rate = pct;
}
精彩评论