Thread: C++ console working with 2 classes

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    C++ console working with 2 classes

    Hi I am working on a project to get two classes to work with each other but I am getting
    'Mortgage::fixedMonthlyPayment': illegal call of non-static member function
    PLEASE HELP I am having a lot of trouble getting this thing to work.
    my code is below:

    Code:
    //MortgageSchedule.cpp
    #include <iostream>
    #include <iomanip>
     
    using namespace std;
    #include <fstream>
    
    
    MortgageSchedule::MortgageSchedule(Mortgage m)
    {
    	Mortgage();
    	return;
    
    }
    
    void MortgageSchedule::setFileName(String s)
    { 
    	return;
    }
    
    void MortgageSchedule::displayOnConsole(void)
    {	
    	for(int i=1; i<= Mortgage::mortgageTerm(); i++)
    		{
    			double a = Mortgage::fixedMonthlyPayment();
    			Mortgage;
    			Total_Amount_Paid += a;
    
    			Mortgage::makeMonthlyPayment();//not saved in variable
    			Check_Total_Interest_Paid += Mortgage::monthlyInterest();
    		}//End for
    		
    		cout<< setw(65) << "Full Mortgage Summary Report\n"
    			<< setw(60) << "----------------------------------------\n"
    			<< setw(10)<<"Total Amount Paid"<<setw(20)<<"Total Interest Paid\n"
    			<< setw(9) <<"$"<<Total_Amount_Paid
    			<< setw(19) <<"$"<<Mortgage::interestPaidSoFar()
    			<< setw(19) <<"check$"<<Check_Total_Interest_Paid;
    	
    	return;
    } 
    
    	 
    void MortgageSchedule::writeToFile(void) 
    {
    	
    	return;
    }
    
    
    
    
    void MortgageSchedule::setPeriod(char c)
    {
    	Choice=c;
    	
    	return;
    }
    Code:
    //MortgageSchedule.h
    //Filename: MortgageSchedule.h
    
    #ifndef MortgageSchedule_H
    #define MortgageSchedule_H
     
    #include "mortgage.h"
    #include "..\INCLUDE\str.h"
    
    class MortgageSchedule
    
    {
    public:
     
    	void setPeriod(char c);
    	// y  yearly display
    	// m   monthly display
    	// s	summary display
    	// default: summary display
    	
    	void setFileName(String s);
    	// Sets diskfile name for output.
    
     
    	void displayOnConsole(void); 
    	// Writes the schedule to the console
    	// Period must be set by a previous message, 
    	// otherwise the a default schedule is used.
    	
    
    
    	void writeToFile(void);
    	// Same as displayToConsole
    	// Except results are written to a 
    	// disk file which is set by a previous setFileName message.
    	// In case no previous message is sent. Output goes to "practice.txt".
    
    	String retrieveDiskFileName(void)const;
    		 
    	MortgageSchedule(Mortgage anyMortgage);
    
    private:
    
    	char Choice;
    	double Total_Amount_Paid;
    	double Check_Total_Interest_Paid;
    	/* You do this part*/
    
    };
    
    
    #endif
    this is the mortgage class---------This is what I am trying to access

    Code:
    // Filename: mortgage.cpp
    // Abbreviate to work with Tutorial
    
    #include "mortgage.h"
    
    #include <math.h>
    
    
    
    const int Mortgage::DEFAULT_TERM = 30;
    
    const double Mortgage :: DEFAULT_LOAN = 40000.00; 
    
    const double Mortgage :: DEFAULT_RATE = 0.10;
    
    
    
    void Mortgage::makeMonthlyPayment(void)
    
    {
    
    	double interestDue, paymentToPrincipal;
    
    	interestDue = monthlyInterestRate * remainingLoan;
    
    	paymentToPrincipal = monthlyPayment-interestDue;
    
    	remainingLoan = remainingLoan - paymentToPrincipal;
    
    	monthlyInterestPayment = interestDue;
    
    	totalInterest += interestDue;
    
      	return;
    
    }
    
    
    
    
    
    
    double Mortgage::rate(void) const
    
    {
    
    	return 12 * monthlyInterestRate;
    
    }
    
    
    
    double Mortgage::originalLoan(void) const
    
    {
    
    	return initialLoan;
    
    }
    
    
    
    double Mortgage::remainingPrincipal(void) const
    
    {
    
    	return remainingLoan;
    
    }
    
    
    
    
    
    double Mortgage::marketValue(void) const
    
    {
    
    	return initialLoan - remainingLoan;
    
    }
    
    
    
    double Mortgage::monthlyInterest(void) const
    
    {
    
    	return monthlyInterestPayment;
    
    }
    
    
    
    double Mortgage::interestPaidSoFar(void) const{
    
    	return totalInterest;
    
    }
    
    
    
    	
    
    double Mortgage::fixedMonthlyPayment(void) const
    
    {
    
      	return monthlyPayment;
    
    }
    
    
    
    	
    
    int Mortgage::mortgageTerm(void) const
    
    {
    
      	return term;
    
    }
    
    
    
    	
    
    Mortgage::Mortgage(double principal,double yearlyInterestRate,
    
    					int lengthOfTermInYears)
    
    {
    
    
    
        term = lengthOfTermInYears;
    
        initialLoan = principal;
    
        remainingLoan = principal;
    
        monthlyInterestRate = yearlyInterestRate/12;
    
    	 monthlyPayment = this->computeMonthlyPayment();
    
    		 //Interest payments commence with monthly payments.
    
        monthlyInterestPayment = 0;
    
        totalInterest = 0;
    
    	return;
    
    }
    
    
    
    
    
    Mortgage::Mortgage(void)
    
    {
    
        term = DEFAULT_TERM;
    
        initialLoan = DEFAULT_LOAN;
    
        remainingLoan = DEFAULT_LOAN;
    
        monthlyInterestRate = DEFAULT_RATE/12;
    
        monthlyPayment = this -> computeMonthlyPayment();
    
    		//Interest payments commence with monthly payments.
    
        monthlyInterestPayment = 0;
    
        totalInterest = 0;
    
        return;
    
    }
    
    
    
    
    
    Mortgage:: ~Mortgage(void)
    
    {
    
      	return;
    
    }
    
     
    
    
    
    double Mortgage::computeMonthlyPayment(void)
    
    {
    
    	double temp1,temp2,temp3;
    
    	double answer;
    
    	int n = term*12;
    
    	temp1 = (1+monthlyInterestRate);
    
    	temp2 = pow(temp1,n);
    
    	temp3 = monthlyInterestRate/(temp2-1);
    
    	answer = temp3 * temp2 * initialLoan;
    
    	return answer;
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your constructor for MortgageSchedule takes a Mortgage instance as a parameter, but it doesn't use it. What are you trying to do in the constructor?

    My guess is that you want to store that Mortgage inside the MortgageSchedule class. If so, you would need a member variable to store it in, and you would need to initialize that member variable with the constructor parameter.

    Once you had a member variable, you could use that member variable inside your MortgageSchedule functions. Pay attention to the difference between the syntax Mortgage::fixedMonthlyPayment() which attempts to call a static function (ignore if you haven't learned that yet) and m.fixedMonthlyPayment() where m is an instance of Mortgage. In the second case, you are calling the function with m and using m's data, which is usually what you want.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    C++ console working with 2 classes

    I tried that but it says I have to declare it some where?

    this is what I tried now
    Code:
    // Filename: MortgageSchedule.cpp
    // This contains stubs for the class methods.
    // Last revision: 11/1/06
    // I modified the parameter list in this constructor method to match
    // its prototype in MortgageSchedule.h.
    // This is not necessary, but it reads better.
    
    #include "MortgageSchedule.h"
    
    #include <cmath>
    #include <iostream>
    #include <iomanip>
     
    using namespace std;
    #include <fstream>
    
    
    MortgageSchedule::MortgageSchedule(Mortgage m)
    {//Put this in
    	Mortgage ourm(m.originalLoan(), m.rate(), m.mortgageTerm());
    	return ourm;
    
    }
    
    void MortgageSchedule::setFileName(String s)
    { 
    	return;
    }
    
    void MortgageSchedule::displayOnConsole(void)
    {	
    	for(int i=1; i<= Mortgage::mortgageTerm(); i++)
    		{
    			//double a = Mortgage::fixedMonthlyPayment;
    			//Mortgage;
    			Total_Amount_Paid += ourm.fixedMonthlyPayment();
    			
    			ourm.makeMonthlyPayment();//not saved in variable
    			Check_Total_Interest_Paid += ourm.monthlyInterest();
    		}//End for
    		
    		cout<< setw(65) << "Full Mortgage Summary Report\n"
    			<< setw(60) << "----------------------------------------\n"
    			<< setw(10)<<"Total Amount Paid"<<setw(20)<<"Total Interest Paid\n"
    			<< setw(9) <<"$"<<Total_Amount_Paid
    			<< setw(19) <<"$"<<Mortgage::interestPaidSoFar
    			<< setw(19) <<"check$"<<Check_Total_Interest_Paid;
    	
    	return;
    } 
    
    	 
    void MortgageSchedule::writeToFile(void) 
    {
    	
    	return;
    }
    
    
    
    
    void MortgageSchedule::setPeriod(char c)
    {
    	Choice=c;
    	
    	return;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You declared a local variable there. You want a member variable in the MortgageSchedule class.

    Look at how you did setPeriod. You assigned c to the Choice member variable. You'll probably want to do something similar with a new member variable for the Mortgage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2006, 11:08 AM
  2. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  3. High Schoolers - Techy Classes?
    By mart_man00 in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 05-09-2003, 02:14 PM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 08:00 PM
  5. Linux console window questions
    By GaPe in forum Linux Programming
    Replies: 1
    Last Post: 12-28-2002, 12:18 PM