Inheritance errors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Inheritance errors

 
0
  #1
Jan 17th, 2009
I have made a base class and a derived class and I am having a couple compile errors.

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
  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
  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"

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,472
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inheritance errors

 
0
  #2
Jan 17th, 2009
>>SavingsAccount.hpp(9) : error C2512: 'Account' : no appropiate default constructor available

That means Account needs a constructor that takes no parameters.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: Inheritance errors

 
0
  #3
Jan 17th, 2009
I did what you said and changed the constructor so it takes no values but i still get errors

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Inheritance errors

 
0
  #4
Jan 17th, 2009
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
  1. SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) {
  2. InterestRate = initInterestRate;
  3. Balance = initBalance;
  4. };
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,472
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inheritance errors

 
0
  #5
Jan 17th, 2009
Originally Posted by AdRock View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 583
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: Inheritance errors

 
0
  #6
Jan 17th, 2009
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):
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: Inheritance errors

 
0
  #7
Jan 17th, 2009
Originally Posted by Agni View Post
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
  1. SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) {
  2. InterestRate = initInterestRate;
  3. Balance = initBalance;
  4. };

I did this but still get errors
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Inheritance errors

 
1
  #8
Jan 17th, 2009
where's the code for TestAccount.cpp?
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: Inheritance errors

 
0
  #9
Jan 17th, 2009
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 583
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: Inheritance errors

 
0
  #10
Jan 17th, 2009
To use Agni's code, Account needs the Account(double newBalance) constructor.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC