| | |
Inheritance errors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I have made a base class and a derived class and I am having a couple compile errors.
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
derived class
main program
#include "SavingsAccount.hpp"
•
•
•
•
SavingsAccount.hpp(9) : error C2512: 'Account' : no appropiate default constructor available
Base class
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Account { friend class SavingsAccount; //friend class CheckingAccount; double deposit; //double withdrawal; public: Account(double newBalance) {//, double deposit, double withdrawal) { currentBalance = newBalance; //newBalance >= 0 ? currentBalance += newBalance : cout << "Initial balance invalid"; }; ~Account() { }; int getBalance() { return currentBalance; } void credit() { currentBalance += deposit; } //void debit() { currentBalance >= withdrawal ? currentBalance -= withdrawal : cout << "Debit amount exceeded account balance"; } private: double currentBalance; };
derived class
C++ Syntax (Toggle Plain Text)
#include "Account.hpp" #include <string> class SavingsAccount : public Account { double Balance; public: SavingsAccount(double initBalance, double initInterestRate) { InterestRate = initInterestRate; Balance = initBalance; }; ~SavingsAccount() { }; void CalculateInterest(Account& ac, double InterestRate) { ac.getBalance *= InterestRate; }; private: double InterestRate; };
main program
#include "SavingsAccount.hpp"
C++ Syntax (Toggle Plain Text)
int main() { Account account(75.00); SavingsAccount savings(100.00, 2.5); cout << "Current Balance:" << account.getBalance(); return 0; }
Last edited by AdRock; Jan 17th, 2009 at 9:14 am.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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
•
•
•
•
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
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
something like
c++ Syntax (Toggle Plain Text)
SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) { InterestRate = initInterestRate; Balance = initBalance; };
thanks
-chandra
-chandra
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.
•
•
Join Date: May 2008
Posts: 583
Reputation:
Solved Threads: 93
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):
(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.
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)
Account(double newBalance) { currentBalance = newBalance; }; Account() { currentBalance = 0; };
(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.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
•
•
•
•
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)
SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) { InterestRate = initInterestRate; Balance = initBalance; };
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
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include "SavingsAccount.hpp" int main() { Account account(75.00); SavingsAccount savings(100.00, 2.5); cout << "Current Balance:" << account.getBalance(); return 0; }
![]() |
Similar Threads
- Need help with.........inheritance.........i can resolve the errors (C++)
- What is the proper way to combine C++ files (with g++) to avoid link (ld) errors? (C++)
- Class/Inheritance problem (C++)
- Trouble with inheritance (C++)
- Ask a problem,about inheritance :) (C++)
- inheritance (C++)
- virtual methods and inheritance (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Couple of questions
- Next Thread: cannot figure out storing character into character array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






