?
Solved

const int declaration problem

Posted on 2003-03-02
2
Medium Priority
?
328 Views
Last Modified: 2010-05-18
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
Comment
Question by:Lurkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
n_fortynine earned 400 total points
ID: 8054955
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
 

Author Comment

by:Lurkin
ID: 8055088
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question