Although you wouldn't want to do this, if you have a namespace COMPANY, and a class in that namespace SOMECLASS. Why is it that in the .cpp file, you might define the functions as
COMPANY::SOMECLASS::someFunction()
{}
But in main, I get errors for doing:
int main() {
COMPANY::SOMECLASS::someFunction();
}
but instead you declare the namespace and do something like:
using COMPANY::SOMECLASS;
int main() {
someFunction();
}
My compile errors are:
1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2065: 'saver1' : undeclared identifier
1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2277: 'JWong::SavingsAccount::{ctor}' : cannot take address of this member function
1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2061: syntax error : identifier '{ctor}'
SavingsAccount.cpp:
#include "SavingsAccount.h"
// initialize static data member
double JWong::SavingsAccount::annualInterestRate = 0;
// default constructor, set savingsBalance to 0
JWong::SavingsAccount::SavingsAccount() : savingsBalance(0)
{}
// constructor
JWong::SavingsAccount::SavingsAccount(double savingsBalance) : savingsBalance(savingsBalance)
{}
double JWong::SavingsAccount::getSavingsBalance()
{
return savingsBalance;
}
void JWong::SavingsAccount::setSavingsBalance(double savingsBalance)
{
this->savingsBalance = savingsBalance;
}
// added these functions to make program cleaner
double JWong::SavingsAccount::getMonthlyInterest()
{
return monthlyInterest;
}
void JWong::SavingsAccount::setMonthlyInterest(double monthlyInterest)
{
this->monthlyInterest = monthlyInterest;
}
// returns monthly interest and sets savingsBalance to new amount
double JWong::SavingsAccount::calculateMonthlyInterest()
{
double monthlyInterest = savingsBalance * SavingsAccount::annualInterestRate / 12;
setSavingsBalance(savingsBalance + monthlyInterest);
setMonthlyInterest(monthlyInterest);
return monthlyInterest;
}
void JWong::SavingsAccount::modifyInterestRate(double newInterestRate)
{
SavingsAccount::annualInterestRate = newInterestRate;
}
double JWong::SavingsAccount::getAnnualInterestRest()
{
return SavingsAccount::annualInterestRate;
}
SavingsAccount.h
#ifndef JWONG_SAVINGSACCOUNT_H
#define JWONG_SAVINGSACCOUNT_H
namespace JWong
{
class SavingsAccount
{
public:
// default constructor
SavingsAccount();
// constructor
SavingsAccount(double savingsBalance);
double getSavingsBalance();
void setSavingsBalance(double savingsBalance);
double calculateMonthlyInterest();
double getMonthlyInterest();
void setMonthlyInterest(double monthlyInterest);
// static functions
static void modifyInterestRate(double newInterestRate);
static double getAnnualInterestRest();
private:
double savingsBalance;
// static members
static double annualInterestRate;
double monthlyInterest;
};
}
#endif
main.cpp:
#include <iostream>
#include <iomanip>
#include "SavingsAccount.h"
using std::cout;
using std::setprecision;
using std::fixed;
//using JWong::SavingsAccount;
int main()
{
JWong::SavingsAccount::SavingsAccount *saver1 = new JWong::SavingsAccount::SavingsAccount(2000.00);
}