really lost

Jul 25, 2014 at 12:59am
I'm having a really difficult time in my current c++ class. I've exhausted myself for the last couple of days asking my professor for help, and maybe he doesn't get what I'm asking. I can't find any resources online.

Basically my issue is my program this week has to cin to a variable, we have to take that variable and tuck it into a class as a private variable, and then be able to change, and access the variable again.
the program is a mess and i'm confused on a lot of other stuff too, but this is the first.

this is the section in main I have to use (was provided by class)
there is a lot of other stuff going on around this but this is where im starting, because i really have no clue where to start and i've been asking the prof for help all week.


1
2
3
4
5
6
7
#include SavingsAccount.h  

SavingsAccount account2;

cout << "Set savings account interest rate" << endl;
cin >> newInterestRate;
account2.setInterestRate(newInterestRate);


so to do this in the class header file i have this (again, amongst other stuff i have trimmed for clarity)
1
2
3
4
5
6
7
8
9
10
11
12
13
class SavingsAccount: public BankAccount
{
public:
void setInterestRate(double newInterestRate)
	{
		interestRate = newInterestRate;
	};
	double getInterestRate()
	{
		return interestRate;
	}	;
private:
double interestRate(double newInterestRate);

I use the assignment operator above in setInterestRate and it tells me interestRate must be a 'modifiable lvalue'

my professor keeps pointing me to bucky's c++ tutorials on youtube but the one referencing this (13) has only done me more harm. someone point me in the right direction?
I pretty much missed last week while i was on vacation so i'm playing catch up and really regretting going on vacation.
Jul 25, 2014 at 2:11am
Where do you define your interestRate variable in the getInterestRate() function, and where do you define "newInterestRate" in your first block of code?

Edit:
double interestRate(double newInterestRate);
is a function declaration. This does not create a private double class property called "interestRate".
You probably mean to do something like...
1
2
  private:
    double interestRate;
Last edited on Jul 25, 2014 at 2:15am
Jul 25, 2014 at 2:50am
double interestRate(double newInterestRate);,
Why doesn't it be double interestRate; ?

More, your functions in class don't need to end with ;.
Last edited on Jul 25, 2014 at 2:52am
Jul 25, 2014 at 7:09am
Morning.
Like the others have said, you just need to declare a variable, rather than a function-like declaration.
So your class might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class SavingsAccount : public BankAccount
{
public:

	SavingsAccount()
	{
		// initialise member variables to something in the constructor
		// (like this or use an initialisation list)
		interestRate = 0;
	}


	void setInterestRate(double newInterestRate)
	{
		interestRate = newInterestRate;
	}
	double getInterestRate()
	{
		return interestRate;
	}
private:
	double interestRate;
};
Jul 26, 2014 at 1:02am
Thank you guys so much. Like I said last week was our first with classes and I missed nearly all of it due to vacation.
got it to work like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class SavingsAccount: public BankAccount
{
public:
	double newInterestRate();
	void setInterestRate(double newInterestRate)
	{
		interestRate = newInterestRate;
	}
	double getInterestRate()
	{
		return interestRate;
	}
	int getInterestEarned();
	int getNumberOfDays()
	;
private:
	double interestRate;
	int numberOfDays;
	
}:

obviously I have some work left to do, but you guys were a huge help thank you so much again!!
Last edited on Jul 26, 2014 at 1:03am
Jul 28, 2014 at 3:25pm
You don't need line 4.
Topic archived. No new replies allowed.