943,952 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1610
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 17th, 2009
0

Inheritance errors

Expand Post »
I have made a base class and a derived class and I am having a couple compile errors.

Quote ...
SavingsAccount.hpp(9) : error C2512: 'Account' : no appropiate default constructor available
What i want to do is be able to give the savings account an inital balance and it changes the currentBalance variable in the account class.

Base class
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Account {
  5.  
  6. friend class SavingsAccount;
  7. //friend class CheckingAccount;
  8.  
  9. double deposit;
  10. //double withdrawal;
  11.  
  12. public:
  13. Account(double newBalance) {//, double deposit, double withdrawal) {
  14. currentBalance = newBalance;
  15. //newBalance >= 0 ? currentBalance += newBalance : cout << "Initial balance invalid";
  16. };
  17.  
  18. ~Account() { };
  19.  
  20. int getBalance() { return currentBalance; }
  21.  
  22. void credit() { currentBalance += deposit; }
  23.  
  24. //void debit() { currentBalance >= withdrawal ? currentBalance -= withdrawal : cout << "Debit amount exceeded account balance"; }
  25.  
  26. private:
  27. double currentBalance;
  28.  
  29. };

derived class
C++ Syntax (Toggle Plain Text)
  1. #include "Account.hpp"
  2. #include <string>
  3.  
  4. class SavingsAccount : public Account {
  5.  
  6. double Balance;
  7.  
  8. public:
  9. SavingsAccount(double initBalance, double initInterestRate) {
  10. InterestRate = initInterestRate;
  11. Balance = initBalance;
  12. };
  13.  
  14. ~SavingsAccount() { };
  15.  
  16. void CalculateInterest(Account& ac, double InterestRate) {
  17. ac.getBalance *= InterestRate;
  18. };
  19.  
  20. private:
  21. double InterestRate;
  22.  
  23. };

main program
#include "SavingsAccount.hpp"

C++ Syntax (Toggle Plain Text)
  1. int main() {
  2.  
  3. Account account(75.00);
  4. SavingsAccount savings(100.00, 2.5);
  5.  
  6. cout << "Current Balance:" << account.getBalance();
  7.  
  8. return 0;
  9. }
Last edited by AdRock; Jan 17th, 2009 at 9:14 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 17th, 2009
0

Re: Inheritance errors

>>SavingsAccount.hpp(9) : error C2512: 'Account' : no appropiate default constructor available

That means Account needs a constructor that takes no parameters.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 17th, 2009
0

Re: Inheritance errors

I did what you said and changed the constructor so it takes no values but i still get errors

Quote ...
TestAccount.cpp
TestAccount.cpp(5) : error C2664: 'Account::Account(const Account &)' : cannot convert parameter 1 from 'double' to 'const Account &'
Reason: cannot convert from 'double' to 'const Account'
No constructor could take the source type, or constructooverload resolution was ambiguous
Also i need the constructor to take an initial value
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 17th, 2009
0

Re: Inheritance errors

well when you create the object of the child class the base class ctor gets called and since you have declared your own ctor the compiler doesn't give you a default ctor. In the definition of the child class ctor you should pass the required value to the base class ctor.

something like
c++ Syntax (Toggle Plain Text)
  1. SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) {
  2. InterestRate = initInterestRate;
  3. Balance = initBalance;
  4. };
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jan 17th, 2009
0

Re: Inheritance errors

Click to Expand / Collapse  Quote originally posted by AdRock ...
I did what you said and changed the constructor so it takes no values but i still get errors



Also i need the constructor to take an initial value
you can have as many constructors as you like.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 17th, 2009
0

Re: Inheritance errors

I was going to give you Agni's answer (lucky I saw it when I switched to Advanced).

Also, there is no requirement that a class have only one constructor.

You could actually declare both (not that you want to in this instance):
c++ Syntax (Toggle Plain Text)
  1. Account(double newBalance) {
  2. currentBalance = newBalance;
  3. };
  4. Account() {
  5. currentBalance = 0;
  6. };

(Arg! I must type slow, Dragon said this already)

Design Comment:

I'm not overly fond of adding friends to a class (I do it when I need to, but try to avoid it when possible). You should not need to make derived classes a friend of the parent. If you have things that you want derived classes to be able to see / modify, make them protected instead of private, or provide protected accessors if that works better for you.
Last edited by Murtan; Jan 17th, 2009 at 1:56 pm.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 17th, 2009
0

Re: Inheritance errors

Click to Expand / Collapse  Quote originally posted by Agni ...
well when you create the object of the child class the base class ctor gets called and since you have declared your own ctor the compiler doesn't give you a default ctor. In the definition of the child class ctor you should pass the required value to the base class ctor.

something like
c++ Syntax (Toggle Plain Text)
  1. SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) {
  2. InterestRate = initInterestRate;
  3. Balance = initBalance;
  4. };

I did this but still get errors
Quote ...
TestAccount.cpp
SavingsAccount.hpp(9) : error C2664: 'Account::Account(const Account &)' : cannot convert parameter 1 from 'double' to 'const Account &'
Reason: cannot convert from 'double' to 'const Account'
No constructor could take the source type, or constructor overload resolution was ambiguous

TestAccount.cpp(5) : error C2664: 'Account::Account(const Account &)' : cannot convert parameter 1 from 'double' to 'const Account &'
Reason: cannot convert from 'double' to 'const Account'
No constructor could take the source type, or constructor overload resolution was ambiguous
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 17th, 2009
1

Re: Inheritance errors

where's the code for TestAccount.cpp?
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jan 17th, 2009
0

Re: Inheritance errors

C++ Syntax (Toggle Plain Text)
  1. #include "SavingsAccount.hpp"
  2.  
  3. int main() {
  4.  
  5. Account account(75.00);
  6. SavingsAccount savings(100.00, 2.5);
  7.  
  8. cout << "Current Balance:" << account.getBalance();
  9.  
  10. return 0;
  11. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 17th, 2009
0

Re: Inheritance errors

To use Agni's code, Account needs the Account(double newBalance) constructor.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Couple of questions
Next Thread in C++ Forum Timeline: cannot figure out storing character into character array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC