• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

const int declaration problem

Having a problem getting the variable MAX declared, no matter what I try, I keep getting errors about declarations etc.

// Start of MortgageProgram.cpp
#include <iostream>
#include <iomanip>
#include <strstream>
using namespace std;
#include "mortgage.h"

void main(void)
{
  cout << "*** Start of Mortgage Program *** " << endl << endl;

  // create six Mortgage objects
  Mortgage account[MAX]={
        Mortgage(),                         // for default constructor
        Mortgage(10000,  .12,  5 ),         // constructor w/args
        Mortgage(14000,  .08,  5 ),         // constructor w/args
        Mortgage(-14000, .08,  5 ),         // generate error msg
        Mortgage(14000, -.08,  5 ),         // generate error msg
        Mortgage(14000,  .08, -5 )          // generate error msg
    };

    // display objects (initial state)
    cout << endl << "** Display objects (initial state) ** " << endl;
    cout.precision(2);
    cout.setf(ios::fixed | ios::showpoint);
    for(int i = 0; i < MAX; i++) {
        cout << "Monthly Payment[" << i << "]: $" << 
          setw(12) << setiosflags(ios::right) << account[i].getMonthly() << endl;
        cout << "Total Pay Back[" << i << "] : $" << 
          setw(12) << setiosflags(ios::right) << account[i].getPayBack() << endl;
    }
    cout << "Press ENTER to continue ...";
    cin.get();

    cout << "** Now altering some object values ** " << endl;
    account[0].setLoan(10000);
    account[0].setRate(0.12);
    account[0].setYears(5);
    // object 2
    account[2].setRate(0.06);

    // display objects (changed state)
    cout << endl << "** Display objects (changed values) **" << endl;
    cout.precision(2);
    cout.setf(ios::fixed | ios::showpoint);
    for(i = 0; i < MAX; i++) {
        cout << "Monthly Payment[" << i << "]: $" << 
          setw(12) << setiosflags(ios::right) << account[i].getMonthly() << endl;
        cout << "Total Pay Back[" << i << "] : $" << 
          setw(12) << setiosflags(ios::right) << account[i].getPayBack() << endl;
    }

    // program termination
    cout << "*** End of Mortgage Program ***" << endl;
    cin.get();
}
//end of MortgageProgram.cpp

// Start of mortgage.h
#ifndef MORTGAGE_H
#define MORTGAGE_H

// Mortgage class declaration
class Mortgage
{
private:
     int loan, years;
     double rate, monthly, payback;
public:
     const int MAX;
     Mortgage(void); // Constructor with default arguments
     Mortgage(int, double, int); // Constructor with parameters
     void setLoan(int);
     void setRate(double);
     void setYears(int);
     double getMonthly(void);
     double getPayBack(void);
};
#endif
//end of mortgage.h

// Start of mortgage.cpp
#include "mortgage.h"

Mortgage::Mortgage(void){}
Mortgage::Mortgage(int, double, int){}

// Post: Class member loan is set.
void Mortgage::setLoan(int tempLoan) {
     if(tempLoan > 0)
          loan = tempLoan;
     else
          loan = 0;
}

// Post: Class member rate is set.
void Mortgage::setRate(double tempRate) {
     if(tempRate > 0)
          rate = tempRate;
     else
          rate = 0;
}

// Post: Class member years is set.
void Mortgage::setYears(int tempYears) {
     if(tempYears > 0)
          years = tempYears;
     else
          years = 0;
}

// Pre:  operand has already been initialized.
// Post: monthly (payment) is returned.
double Mortgage::getMonthly() {
     double term = 0;
     term = ((1 + rate / 12) * (12 * years));
     monthly = ((loan * (rate / 12) * term) / term - 1);
     return monthly;
}

double Mortgage::getPayBack() {
     return 0;// Dummy Return value
}
// End of mortgage.cpp

There's 3 files to this program, just need to know how to declare the const int MAX within the mortgage.h file.

Thanks for any help you can provide.
0
Lurkin
Asked:
Lurkin
1 Solution
 
n_fortynineCommented:
I would either divorce MAX from Mortgage and keep it outside the class in the .h file
const int MAX = ...;
then class Mortgage {...};

or if you want to declare a const in the .cpp file probably you'll have to specifically do it like so
Mortgage::MAX = ...;

Why have MAX public anyway? Would it matter if MAX's inside or outside the class?
0
 
LurkinAuthor Commented:
You totally answered what I was doing wrong, I moved the declaration to before the class and it works perfectly.  It didn't have to be in the class, just had to be in the .h file.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now