Here is the class definition that I have so far:

#ifndef SAVINGACCOUNT_H
#define SAVINGACCOUNT_H
#include<iostream>
#include<string>


class SavingAccount
{
    public:
        /** Default constructor */
        SavingAccount();
        /** Default destructor */
        ~SavingAccount();

        string getfirstName() const;
        string getlastName() const;

        static double modifyInterestRate();//static method declaration
        static double calculateMonthlyInterst(double,double);

        void setannualInterestRate(double);
        double getannualInterestRate() const;


    private:
        string firstName;
        string lastName;
        double savingsBalance;
        static double annualInterestRate;

};


#endif // SAVINGACCOUNT_H

and the implementation code:

#include<iostream>
#include<string>
#include "SavingAccount.h"

SavingAccount::SavingAccount()
{
    //ctor
}
string setfirstName()
{
    getline(cin,firstName);
}
string setlastName()
{
    cin>>SavingAccount::getlastName;
}
int InterestRate();
double modifyInterestRate()
{


}
double caclulateMonthlyInterest(double savingsBalance, double annualInterestRate)
{
    savingsBalance=+(annualInterestRate/12);
    return savingsBalance;
}

SavingAccount::~SavingAccount()
{
    //dtor
}

void setannualInterestRate()
{
    SavingAccount::annualInterestRate=0.03;
}
double getannualInterestRate()
{
    cin>>SavingAccount::annualInterestRate;

}

Can anyone help me with this here.
It gives a whole bunch of errors.

Hi Dexxta27,

Here's a few pointers to start you off:

- There must be a main() function in the .cpp function definition file
- You should put spaces between the #include and the < character
- You have definitions but no .h declarations for setfirstname() and setlastname()
- You have .h declaractions but no definitions for getfirstname() and getlastname()
- The function definition modifyInterestRate() has no function body
- None of your defined functions have the SavingAccount:: scope in the .cpp file
- Your constructor does nothing and should initialize the private object members
- In function definition for double caclulateMonthlyInterest(double savingsBalance, double annualInterestRate) shouldn't the statement be using "+=" and not "=+"?
- Why are you using "getline()" instead of just using "cin >>" syntax?
- In function setlastName() you are attempting to use the getlastname() class object function as if it were a string or other variable
- You declare a function InterestRate() in the .cpp file but don't define it anywhere
- I'm a little dubious about your usage of the "static" keywords

Start with that list, and see if you can make that work before trying to go much further with this.

Tom

savingsBalance=+(annualInterestRate/12); Two problems here. 1) you don't initialize savingsBalance in your constructor. 2) this should be savingsBalance+=(annualInterestRate/12); Also, use spaces before/after operators like += and such, just for better readability and visual debugging purposes, if nothing else.

Also, I think that your calculation for savingsBalance is wrong. Shouldn't it be this?

savingsBalance += (savingsBalance * (annualInterestRate/12));

Well I made most of the changes suggested. I don't know how main() should be implemented into this. Should it enclose all the definitions, are not?

To make things easier, here is the actual question:

Create a SavingAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the savers. Each member of the class contains private data members: firstName, lastName, and savingsBalance to indicate the amount the saver currently has on deposit. Provide member function calculateMonthlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate that sets the static annualInterestRate to a new value. Also, provide appropriate constructor, set functions, and get functions.
Write an input function to prompt for saver's name and initial balance.
Write a program to test class SavingAccount. Instantiate two different objects of class SavingAccount, saver1 and saver 2, with the values obtain via the input function. Set the annualInterestRate to 3 persent. Then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 4 percent, calculate the next month's interest and print the new balances for each of the savers.

Put the class definition into a header file (.h) and the class implementation into a source file (.cpp). The input function and main function should be placed in a separate source file (.cpp).

Here is the new .cpp:

#include<iostream>
#include<string>
#include "SavingAccount.h"


SavingAccount::SavingAccount()
{
    savingsBalance=0.0;
    annualInterestRate=0.0;//constructor
}

string SavingAccount::setfirstName()
{
    getline(cin,firstName);//I used getline since firstName is a string.
}

string SavingAccount::setlastName()
{
    cin>>SavingAccount::getlastName;
}

void SavingAccount::getlfirstName()
{
    cout<<SavingAccount::lastName;
}

void SavingAccount::getlastName()
{
    cout<<SavingAccount::lastName;
}

double modifyInterestRate()
{
    cout<<"Enter the new value: ";
    cin>>SavingAccount::annualInterestRate

}

double caclulateMonthlyInterest(double savingsBalance, double annualInterestRate)
{
    savingsBalance += (savingsBalance * (annualInterestRate/12));
    return savingsBalance;
}

SavingAccount::~SavingAccount()
{
    //dtor
}

double SavingAccount::getannualInterestRate()
{
    cin>>SavingAccount::annualInterestRate;

}

And here is the new .h:

#ifndef SAVINGACCOUNT_H
#define SAVINGACCOUNT_H
#include <iostream>
#include <string>


class SavingAccount
{
    public:
        /** Default constructor */
        SavingAccount();
        /** Default destructor */
        ~SavingAccount();

        void setfirstName(string)
        string getfirstName() const;
        void setlastName() const;
        string getlastName() const;

        static double modifyInterestRate();//static method declaration
        static double calculateMonthlyInterst(double,double);

        void setannualInterestRate(double);
        double getannualInterestRate() const;


    private:
        string firstName;
        string lastName;
        double savingsBalance;
        static double annualInterestRate;

};


#endif // SAVINGACCOUNT_H

@OP:
"It gives a whole bunch of errors." is not a very clear description of your problem. You really should be more specific about the errors. Given what I'm seeing so far, I suspect that there are way too many to post, so I suggest you post just the first couple errors.

@"Tom":
>>- There must be a main() function in the .cpp function definition file
Not necessarily. This *.cpp file is a class-implementation file, a main() isn't required unless it's the only *.cpp file in the project. Generally, there will be at least 2 *.cpp files in a project of this nature; a main.cpp that contains the main() an potentially other code and a class-implementation file.

>>- You should put spaces between the #include and the < character
A valid point for readability's sake.

>>- You have definitions but no .h declarations for setfirstname() and setlastname()
>>- You have .h declaractions but no definitions for getfirstname() and getlastname()
>>- The function definition modifyInterestRate() has no function body
>>- None of your defined functions have the SavingAccount:: scope in the .cpp file
All good observations, and valid points.

>>- Your constructor does nothing and should initialize the private object members
A valid point for SavingAccount::savingsBalance. The name strings' constructors automatically initialize them to the empty string, so they really don't need to be. If this were an overloaded/specified constructor, then it would probably be a good idea, but it's a default constructor.

>>- In function definition for double caclulateMonthlyInterest(double savingsBalance, double annualInterestRate) shouldn't the statement be using "+=" and not "=+"?
A valid statement.

>>- Why are you using "getline()" instead of just using "cin >>" syntax?
For string input, getline() is more reliable and less prone to stream-related errors because it consumes whitespace characters and std::cin >> ... doesn't.

>>- I'm a little dubious about your usage of the "static" keywords
If you look at the other thread started by the OP, their use of "static" is correct. Also, given the problem domain, it's not an unreasonable use.

OK, but any help original help on the situation? I fixed most of those. Here are the errors by the way:

Line Message
15 error: 'string' has not been declared
16 error: expected ';' before 'string'
18 error: 'string' does not name a type
28 "
29 "
12 "
17 "
22 "
in function 'double modifyInterestRate()':
34 error: 'cout' was not declared in this scope
35 error: 'cin' was not declared in this scope
31 error: 'double SavingAccount::annualInterestRate' is private
35 error: within this context/*points to line 35*/
52 error: 'cin' was not declared in this scope

You need to specify the namespace for string , cout , etc. You do that like this:

#include <iostream>
#include <string>

int main()
{
   std::string myString("Hellow world!");

   std::cout << myString << std::endl;
   std::cout << "Enter a new string: ";
   std::cin >> myString;

   return 0;
}

Some people might say that you can put using namespace std; before the main , but I wouldn't do that if I were you. It will be OK for a small program like this, but will cause complications in a larger project.

The error about the private variable means you're trying to access this variable directly in the program, but it's a private variable, so you won't be allowed to do that. Specifically, it's this bit:

double modifyInterestRate()
{
    cout<<"Enter the new value: ";
    cin>>SavingAccount::annualInterestRate
}

It should be

double SavingAccount::modifyInterestRate()
{
    std::cout << "Enter the new value: ";
    std::cin >> annualInterestRate
}

The same is true of caclulateMonthlyInterest .

From a design point of view, you should consider whether modifyInterestRate is what you want? It's generally a bad idea to mix up the user interface and the class implementation like this. What if someone in a non-english speaking country wants to use your class? What if you want to change the class so that it stores a monthly interest rate instead of an annual one? I would recommend something more like:

void SavingAccount::modifyInterestRate( double newRate )
{
    /* Set the new rate */
    annualInterestrate = newRate;
}

Note also how the function now also returns void , it doesn't need to return any value. So now the user can use the function with whatever words they like:

int main()
{
    SavingsAccount myAccount;
    
    ...
    
    double userRate;
    std::cout << "Enter a new annual interest rate: ";
    std::cin >> userRate;
    myAccount.modifyIneterstRate( userRate );

    ...
    
    return 0;
}

You need to specify the namespace for string , cout , etc. You do that like this:

#include <iostream>
#include <string>

int main()
{
   std::string myString("Hellow world!");

   std::cout << myString << std::endl;
   std::cout << "Enter a new string: ";
   std::cin >> myString;

   return 0;
}

Some people might say that you can put using namespace std; before the main , but I wouldn't do that if I were you. It will be OK for a small program like this, but will cause complications in a larger project.

The error about the private variable means you're trying to access this variable directly in the program, but it's a private variable, so you won't be allowed to do that. Specifically, it's this bit:

double modifyInterestRate()
{
    cout<<"Enter the new value: ";
    cin>>SavingAccount::annualInterestRate
}

It should be

double SavingAccount::modifyInterestRate()
{
    std::cout << "Enter the new value: ";
    std::cin >> annualInterestRate
}

The same is true of caclulateMonthlyInterest .

From a design point of view, you should consider whether modifyInterestRate is what you want? It's generally a bad idea to mix up the user interface and the class implementation like this. What if someone in a non-english speaking country wants to use your class? What if you want to change the class so that it stores a monthly interest rate instead of an annual one? I would recommend something more like:

void SavingAccount::modifyInterestRate( double newRate )
{
    /* Set the new rate */
    annualInterestrate = newRate;
}

Note also how the function now also returns void , it doesn't need to return any value. So now the user can use the function with whatever words they like:

int main()
{
    SavingsAccount myAccount;
    
    ...
    
    double userRate;
    std::cout << "Enter a new annual interest rate: ";
    std::cin >> userRate;
    myAccount.modifyIneterstRate( userRate );

    ...
    
    return 0;
}

For the most part I aggree with the things you've said. Although this:

Some people might say that you can put using namespace std; before the main , but I wouldn't do that if I were you. It will be OK for a small program like this, but will cause complications in a larger project.

I don't. While I do agree that it's a bad idea to use "using namespace std", there is another possibility besides direct resolution of every instance that I feel will benefit the OP greatly that you may have overlooked, identifier-specific using statements. These will save the OP from having to find everywere something from the std namespace is used and put "std::" in front of it:

using std::string;
using std::cout;
using std::cin;
//etc...

This will allow them to resolve only those names they want visible and keep the others hidden reducing the likelihood of an identifier collision.

Edited 5 Years Ago by Fbody: n/a

Good point, Fbody. I might also add that you can also control the scope of this kind of identifier-specific statement. So, you could have:

#include <iostream>

int main(){
    
    using std::cout;
    using std::endl;

    cout << "Hello!" << endl;

    return 0;
}

This way, you can reduce the chance of namespace clashes still further, but keep some of the convenience :o) I find this preferable to something like:

#include <iostream>
using std::cout;
using std::endl;

int main(){
   ...
}
This article has been dead for over six months. Start a new discussion instead.