constructor problem

Sep 19, 2008 at 12:48am
I have a lab due tomorrow and working on it for a while I get the error no appropriate default constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<iomanip>
#include "SavingsAccount.h"
using namespace std;

 double savingsAccount::annualInterestRate = 0.0;
 

savingsAccount::savingsAccount(double b)
{
	double savingsBalance = b;
	cout << "Please enter account balance" << endl;
	cin >> b;
  if (b < 0) 
	b = 0;
} // end savingsAccount constructor
savingsAccount::~savingsAccount()
{
	cout << "destructor called" << endl;
} // end destructor

void savingsAccount::modifyInterestRate(double r)
{
	static double annualInterestRate = r;
	cout << "Please enter interest rate" << endl;
 cin >> r;
 if (r == 1)
	 r = .01;
 if (r == 2)
	 r = .02;
 else
	 r = .03;
}// end mosify interest rate

void savingsAccount::calculateMonthlyInterest()
 {
   double interest =  annualInterestRate *  savingsBalance; 
 }// end calculateMonthlyInterest

 void savingsAccount::setSavingsBalance(double sb )
 {  
	 double savingsBalance = sb;
    sb =  interest + savingsBalance;
 }// end setSavingsBalance


void savingsAccount::printBalance()const
 {
    cout << setprecision(2) << "Savings Balance = $ " << savingsBalance;
 }// end print 


this is the first .cpp file

my main goes like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<iomanip>
#include "SavingsAccount.h"
using namespace std;

int main()
{
	savingsAccount a1;
	a1.setSavingsBalance(25);
	a1.modifyInterestRate(1);
		a1.printBalance();
system ("pause");
return 0;
}// end main 

I thought this would set the balance to 25 and interest rate to 1%
Sep 19, 2008 at 1:08am
No constructor of savingsAccount takes no parameters. By defining your own constructor, you undefined, so to speak, the default constructor.
Sep 19, 2008 at 1:17am
I have changed the code and included a default of no parameters now the program runs but my output is way off can i bypass the default constructor and just use the one I defined?
Sep 19, 2008 at 1:18am
You have a problem in line 43, sb should be savingsBalance

I think you may need to initialize some values in your constructor, try making the b in line 9 have a default value.
Sep 19, 2008 at 1:27am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<iomanip>
#include "SavingsAccount.h"
using namespace std;

 double savingsAccount::annualInterestRate = 0.0;
 
 savingsAccount::savingsAccount()
 {
	 double savingsBalance = 0;
	 cout << "Please enter account balance" << endl;
	 cin >> savingsBalance;
	 if (savingsBalance < 0)
		 savingsBalance = 0;
 }
savingsAccount::savingsAccount(double b=0)
{
	double savingsBalance = 0;
	cout << "Please enter account balance" << endl;
	cin >> b;
  if (b < 0) 
	b = 0;
} // end savingsAccount constructor
savingsAccount::~savingsAccount()
{
	cout << "destructor called" << endl;
} // end destructor

void savingsAccount::modifyInterestRate(double r)
{
	static double annualInterestRate = r;
	cout << "Please enter interest rate" << endl;
 cin >> r;
 if (r == 1)
	 r = 0.1;
 if (r == 2)
	 r = 0.2;
 else
	 r = 0.3;
}// end mosify interest rate

void savingsAccount::calculateMonthlyInterest()
 {
   double interest =  annualInterestRate *  savingsBalance; 
 }// end calculateMonthlyInterest

 void savingsAccount::setSavingsBalance(double sb )
 {  
	 double savingsBalance = sb;
    savingsBalance =  interest + savingsBalance;
 }// end setSavingsBalance

void savingsAccount::printBalance()const
 {
    cout << setprecision(2) << "Savings Balance = $" << savingsBalance << endl;
 }// end print 

I have changed the code but still get a bad output at the end I added a default constructor as well.
Sep 19, 2008 at 1:31am
I'm looking at your constructor and I don't know what it does.
Why do you input a value that is not used? Why do you even have input in a constructor? What's the point of that if?
There's also input in modifyInterestRate(). Methods should not interact with the user directly. Ever. (Unless of course the class is for I/O. But this is not the case here.) You have another pointless if in modifyInterestRate().
interest in calculateMonthlyInterest() is created, initialized, but its value is not used before going out of scope.
Line 43 is pointless.

Never mind. The entire class does nothing. You need to rewrite everything.
Last edited on Sep 19, 2008 at 1:32am
Sep 19, 2008 at 1:35am
I see what you are talking about now and am starting over
Sep 19, 2008 at 1:40am
Harsh, at least you have an idea of where to start, don't make the same mistakes as last time. Maybe try start with a abstract class account and savings account can inherit... or something
Sep 19, 2008 at 1:44am
abstract class account and savings account can inherit
Dear god, no.
Sep 19, 2008 at 1:59am
Whats wrong with that?
Sep 19, 2008 at 2:08am
Do you use a jackhammer for nailing? Then why use inheritance for this?
Sep 19, 2008 at 2:15am
Ok now im getting '=' left operand must be lvalue lines 22 and 25

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<iomanip>
#include "SavingsAccount.h"
using namespace std;

 double savingsAccount::annualInterestRate = 0.0;
 
 
savingsAccount::savingsAccount(double b)
{
  double savingsBalance = b;
  if (b < 0) 
	b = 0;
} // end savingsAccount constructor
savingsAccount::~savingsAccount()
{
	cout << "destructor called" << endl;
} // end destructor

void savingsAccount::modifyInterestRate(double i)
{
 if(i = 1 || i = 2)
	  i = annualInterestRate;
 else
	   3 = annualInterestRate;

}// end mosify interest rate

void savingsAccount::calculateMonthlyInterest()
 {
    savingsBalance = (savingsBalance + ((annualInterestRate * savingsBalance) /12));
 }// end calculateMonthlyInterest

void savingsAccount::printBalance()const
 {
    cout << setprecision(2) << "Savings Balance = $" << savingsBalance << endl;
 }// end print 
Sep 19, 2008 at 2:16am
found the one @ 25 sorry maybe a little dyslexic
Sep 19, 2008 at 2:17am
Well I just thought inheritance was a nice feature for anything really. I made a simple account program before and inheritance was good just in case I wanted to make another type of account or something... is a jackhammer a pneumatic drill, or a sledge hammer lol
Sep 19, 2008 at 2:18am
@grow79

Just before you rewrite the whole program, a couple of friendly tips, to read the concept and understand of a C++ class again.

As I see it, you have missed some thing of understanding a class.

One, in every method of the class you are declaring a "local" variable and inputting/changing its value. Since they are not part/members of the class, you would not see them in other method of the class thus not an accurate outcome.

Two, you are chaging the values of passed-in parameters rather than using them to either copying to members or calculating some value. The calculated outcome would be passed to other method either.

Three, you have initialized a static variable outside the class as you would normally do for a static "member" of the class. The annualInterestRate is a static "local" variable to the modifyAnnualRate() method, but not of the savingsAccount class. A local static variable does not require outside the class initialization, that is for a static class member.

If I were you to start at, the starting point would look like:

1
2
3
4
5
6
7
8
9
10
11
12
class savingsAccount 
{
   private:
     //here all your data members you want to use, like
    double savingsBalance, annualInterestRate, interestRate;

     // the members declared here accessible across the class methods
     // fulfilling your purpose like, one method calculates and other prints 
   public:
      // all your public methods like changeAnnualRate, etc 
};


And I would avoid giving input statements in a constructor, rather, pass the required parameters and use them for member initialization in the class.

You still are not clear, then you better read a C++ reference again to have a clear understanding of it. Try it out again and come back if you need any further help.

Good luck :)
Sep 19, 2008 at 2:20am
ok got the second one now im getting something about missing ; before || and empty control statement warning any ideas?
Sep 19, 2008 at 2:23am
my .h file i think is on track
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Class SavingsAccount interface
#ifndef SAVINGS_ACCOUNT_H
#define SAVINGS_ACCOUNT_H
class savingsAccount // class name
{
public: //public members
	savingsAccount();
	savingsAccount(double);
	~savingsAccount();
	void calculateMonthlyInterest();
	static void modifyInterestRate(double);
	 void printBalance() const;
private: // private members
	double savingsBalance;
	static double annualInterestRate;
};// end class
#endif // end savingsAccount 
Sep 19, 2008 at 2:25am
line 25 in your code says put annualInterest into 3.
Sep 19, 2008 at 2:32am
sorry I have only been doing this for about 3 months classes I am still a bit unsure of. my naming conventions are ok but implementing is something I need some work on.
Sep 19, 2008 at 2:53am
Thank you all for the help im off for the night
Topic archived. No new replies allowed.