13 Replies - 521 Views - Last Post: 11 April 2010 - 06:15 PM Rate Topic: -----

#1 Guest_Jermaine*


Reputation:

Classes: Calling a function in main

Posted 09 April 2010 - 12:11 PM

Hi, I'm trying to write a program using classes that gives the user some options. Before the user does anything, he's asked to enter his name and bank account number. A List of options like display account info, make deposit or withdrawal, or leave the program will appear afterwards.

If the user chooses to display account information, the name he entered, his account number, and the set balance(which is zero unless he deposited anything) should show.

If you scroll down to my main file program, you should some if statements. How do I call functions in those statements correctly? I know it's a little different since I'm using classes.

//.h file 
class account
{
     private:
            double balance, deposit, withdrawal, checkingBalance, savingsBalance;
            string name, AccNum;
             
     public:
            double displayAccInfo (string name, string AccNum, double checkingBalance, double savingsBalance);
            void makeDeposit(double balance, double deposit); 
            void makeWithdrawal(double balance, double withdrawal);
};



//Functions file
#include "program3.h"

account::account()
{
     name = n; 
     balance = 0;
     checkingBalance = 0; 
     savingsBalance = 0;  
}
double account::displayAccInfo (string name, string AccNum, double checkingBalance, double savingsBalance)
{
       cout<<name; 
       cout<<AccNum; 
       cout<<"Your checking account balance is: "<<checkingBalance<<endl;
       cout<<"Your savings account balance is: "<<savingsBalance<<endl;  
               
}
void account::makeDeposit(double balance, double deposit)
{
     balance += deposit; 
     
} 
void account::makeWithdrawal(double balance, double withdrawal)
{
     balance -= withdrawal; 
} 


//Main file
#include<iostream>
using namespace std;

#include "program3.h"

int main() 
{
     double deposit, withdrawal, checkingBalance, savingsBalance;
     int selection; 
     string AccNum, name;
     

     std::cout<<"Hi, Welcome to CSC International Bank. Please enter your name and account number"<<std::endl;
     std::cin>>name;  
     std::cin>>AccNum; 
     
     std::cout<<" "<<std::endl;
     std::cout<<"What would you like to do today?"<<std::endl; 
     std::cout<<"To display account info, please press 1."<<std::endl; 
     std::cout<<"To make a deposit, please press 2."<<std::endl; 
     std::cout<<"To make a withdrawal, please press 3."<<std::endl; 
     std::cout<<"To leave program, please press 4."<<std::endl; 
     std::cin>>selection; 

     if (selection == 1)
     {
          
     }
     if (selection == 2)
     {
          
     }
     if (selection == 3)
     {
          
     }
     if (selection == 4)
     {
          
     }
     system("pause"); 
     return 0; 
}



Is This A Good Question/Topic? 0

Replies To: Classes: Calling a function in main

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Classes: Calling a function in main

Posted 09 April 2010 - 12:29 PM

make a instance of your class like this
account myAccount;



then call a function like so

myAccount.makeDeposit(balance, deposit);


Was This Post Helpful? 0
  • +
  • -

#3 amdcpp  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 06-February 10

Re: Classes: Calling a function in main

Posted 09 April 2010 - 12:37 PM


After they enter their name and account number, you would
make an object instantiation from the account class.

You should code a constructor in the public section which
takes those two arguments (as const) and within that
constructor, should set default values for their balances,
most likely zero unless you give them a sign up bonus.

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Classes: Calling a function in main

Posted 09 April 2010 - 02:00 PM

ok thats your assignement, but whats your question? i showed you how to make an instance of your class
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Classes: Calling a function in main

Posted 09 April 2010 - 10:09 PM

I'm trying to set my program up so that a user could make a name and an account number. Both of these will be stored. The user can either display account info, make a deposit, make a withdrawal or leave program. Whether the makes deposit or withdrawal, when account info is display it should show the user's name, account number and his or her current balance.

My question is how can I go about getting the owner's name and account number and then based on this information, the correct account info will show? Everything else except the main function has been changed.

#include<iostream>
#include<string>
using namespace std;

#include "program3.h"

int main() 
{
     double deposit, withdrawal, checkingBalance, savingsBalance, balance;
     int selection; 
     string AccNum, name;
     
     account myAccount;

     std::cout<<"Hi, Welcome to CSC International Bank. Please enter your name and account number"<<std::endl;
     std::cin>>name;  
     std::cin>>AccNum; 
     
     std::cout<<" "<<std::endl;
     std::cout<<"What would you like to do today?"<<std::endl; 
     std::cout<<"To display account info, please press 1."<<std::endl; 
     std::cout<<"To make a deposit, please press 2."<<std::endl; 
     std::cout<<"To make a withdrawal, please press 3."<<std::endl; 
     std::cout<<"To leave program, please press 4."<<std::endl; 
     std::cin>>selection; 

     if (selection == 1)
     {
           myAccount.displayAccInfo(name, AccNum, checkingBalance, savingsBalance);
     }
     if (selection == 2)
     {
           myAccount.makeDeposit(balance, deposit);
     }
     if (selection == 3)
     {
           myAccount.makeWithdrawal(balance, withdrawal);
     }
     if (selection == 4)
     {
          system("pause"); 
     }
     
     return 0; 
}

Was This Post Helpful? 0

#6 amdcpp  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 06-February 10

Re: Classes: Calling a function in main

Posted 10 April 2010 - 03:53 AM


In your makeDeposit() and makeWithdrawl() methods, you are passing the balance to
them, but you should only pass the deposit, withdrawl data members:
void account::makeDeposit(const double deposit) {
     balance += deposit;
}



The person shouldn't have to enter their balance if they wanted to make a deposit
or a withdrawl.

If you want to get their balance or name, you can make a couple getter methods:
void getName() const {
     cout << "Account Name: " << name;
}



In main(), you would just call getName():
someonesAccount.getName()



This post has been edited by amdcpp: 10 April 2010 - 04:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Classes: Calling a function in main

Posted 11 April 2010 - 11:58 AM

View Postamdcpp, on 10 April 2010 - 02:53 AM, said:


In your makeDeposit() and makeWithdrawl() methods, you are passing the balance to
them, but you should only pass the deposit, withdrawl data members:
void account::makeDeposit(const double deposit) {
     balance += deposit;
}



The person shouldn't have to enter their balance if they wanted to make a deposit
or a withdrawl.

If you want to get their balance or name, you can make a couple getter methods:
void getName() const {
     cout << "Account Name: " << name;
}



In main(), you would just call getName():
someonesAccount.getName()




I tried but it didn't work. But then I tried something else and it seems like it's about to run but I'm getting the following linker errors.

[Linker error] undefined reference to `account::displayAccInfo(std::string, std::string, double, double)'

[Linker error] undefined reference to `account::makeDeposit(double, double)'

[Linker error] undefined reference to `account::makeWithdrawal(double, double)'

My code:
#include "program3.h"
#include <string>
account::account()
{
     name = n; 
     balance = 0;
   
}
double account::displayAccInfo (string name,string AccNum, double checkingBalance, double savingsBalance)
{
       cout<<name; 
       cout<<AccNum; 
       cout<<"Your checking account balance is: "<<myAccount.makeDeposit()<<endl;
       cout<<"Your savings account balance is: "<<myAccount.makeWithdrawal()<<endl;  
               
}
void account::makeDeposit(double balance, double deposit)
{
     balance += deposit; 
     
}
void account::makeWithdrawal(double balance, double withdrawal)
{
     balance -= withdrawal; 
} 


#include<iostream>
#include<string>
using namespace std;

#include "program3.h"

int main() 
{
     double deposit, withdrawal, checkingBalance, savingsBalance, balance;
     int selection; 
     string AccNum, name;
     
     account myAccount;

     std::cout<<"Hi, Welcome to CSC International Bank. Please enter your name and account number"<<std::endl;
     std::cin>>name;  
     std::cin>>AccNum; 
     
     std::cout<<" "<<std::endl;
     std::cout<<"What would you like to do today?"<<std::endl; 
     std::cout<<"To display account info, please press 1."<<std::endl; 
     std::cout<<"To make a deposit, please press 2."<<std::endl; 
     std::cout<<"To make a withdrawal, please press 3."<<std::endl; 
     std::cout<<"To leave program, please press 4."<<std::endl; 
     std::cin>>selection; 

     if (selection == 1)
     {
           myAccount.displayAccInfo (name,AccNum, checkingBalance,savingsBalance);
     }
     if (selection == 2)
     {
           myAccount.makeDeposit(balance, deposit); 
     }
     if (selection == 3)
     {
           myAccount.makeWithdrawal(balance, withdrawal);
     }
     if (selection == 4)
     {
          system("pause"); 
     }
     
     return 0; 
}



class account
{
     private:
            double balance, deposit, withdrawal, checkingBalance, savingsBalance;
            string name, AccNum;
             
     public:
            double displayAccInfo (string name, string AccNum, double checkingBalance, double savingsBalance);
            void makeDeposit(double balance, double deposit); 
            void makeWithdrawal(double balance, double withdrawal);
};

Was This Post Helpful? 0

#8 Guest_Jermaine*


Reputation:

Re: Classes: Calling a function in main

Posted 11 April 2010 - 02:43 PM

can anyone help?
Was This Post Helpful? 0

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Classes: Calling a function in main

Posted 11 April 2010 - 03:26 PM

I would make the functions inline:
#include<iostream>
#include<string>
using namespace std;

class accounts
{
private:
	double balance, deposit, withdrawal, checkingBalance, savingsBalance;
	string name, password, AccNum;
	int Acctype;
	
public:
	void setaccount(string n, string Acc,string pwd)
	{
		name = n; 
		AccNum = Acc;
		checkingBalance = 0;
		savingsBalance = 0;
		password = pwd;
	}
	
	bool check_acc(string n, string pwd)
	{
		if (n == name && pwd == password) {
			return true;
		}
		return false;
	}
	void displayAccInfo ()
	{
		cout<<name << "\t pasword: " << AccNum <<endl; 
		cout<<"Your checking account balance is: "<<checkingBalance<<endl;
		cout<<"Your savings account balance is: "<<savingsBalance<<endl;  
		
	}
	
	void makeDeposit(double deposit, int Acc_type)
	{
		if (Acc_type == 1) {
			savingsBalance += deposit;
		} else if (Acc_type == 2) {
			checkingBalance += deposit;
		}
	}
	void makeWithdrawal(double withdrawal, int Acc_type)
	{
		if (Acc_type == 1) {
			savingsBalance -= withdrawal;
		} else if (Acc_type == 2) {
			checkingBalance -= withdrawal;
		}
	} 
	
} accs;

accounts customers[5];


int main() 
{

	string n[5] = {"Joe", "Bob", "Mary", "Sally", "Jo"};
	string A[5] = {"aab", "bbb", "ccc","ddd","eee"};
	string p[5] = {"123", "456", "789", "abc", "def"};
	for (int i = 0; i < 5; i++) {
		customers[i].setaccount(n[i],A[i],p[i]);
	}
	
	for (int i = 0; i < 5; i++) {
		customers[i].displayAccInfo();
	}
		
	return 0; 
}


That should get you going.

Remember to keep it simple!
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Jermaine*


Reputation:

Re: Classes: Calling a function in main

Posted 11 April 2010 - 04:05 PM

What to make them inline but it's a class assignment and I'm not allowed to
Was This Post Helpful? 0

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Classes: Calling a function in main

Posted 11 April 2010 - 04:30 PM

View PostJermaine, on 11 April 2010 - 06:05 PM, said:

What to make them inline but it's a class assignment and I'm not allowed to

Ok then, but I would still use an array of objects:
#include<iostream>
#include<string>
using namespace std;

class accounts
{
private:
	double balance, deposit, withdrawal, checkingBalance, savingsBalance;
	string name, password, AccNum;
	int Acctype;
	
public:
	bool check_acc(string n, string pwd);
	void displayAccInfo ();
	void makeDeposit(double deposit, int Acc_type);
	void makeWithdrawal(double withdrawal, int Acc_type);
	
	void setaccount(string n, string Acc,string pwd)
	{
		name = n; 
		AccNum = Acc;
		checkingBalance = 0;
		savingsBalance = 0;
		password = pwd;
	}
		
};

bool accounts::check_acc(string n, string pwd) {
	if (n == name && pwd == password) {
		return true;
	}
	return false;
}

void accounts::displayAccInfo () {
	cout<<name << "\t pasword: " << AccNum <<endl; 
	cout<<"Your checking account balance is: "<<checkingBalance<<endl;
	cout<<"Your savings account balance is: "<<savingsBalance<<endl;  
	
}

void accounts::makeDeposit(double deposit, int Acc_type) {
	if (Acc_type == 1) {
		savingsBalance += deposit;
	} else if (Acc_type == 2) {
		checkingBalance += deposit;
	}
}

void accounts::makeWithdrawal(double withdrawal, int Acc_type) {
	if (Acc_type == 1) {
		savingsBalance -= withdrawal;
	} else if (Acc_type == 2) {
		checkingBalance -= withdrawal;
	}
} 



accounts customers[5];


int main() 
{

	string n[5] = {"Joe", "Bob", "Mary", "Sally", "Jo"};
	string A[5] = {"aab", "bbb", "ccc","ddd","eee"};
	string p[5] = {"123", "456", "789", "abc", "def"};
	for (int i = 0; i < 5; i++) {
		customers[i].setaccount(n[i],A[i],p[i]);
	}
	
	for (int i = 0; i < 5; i++) {
		customers[i].displayAccInfo();
	}
		
	return 0; 
}


Was This Post Helpful? 0
  • +
  • -

#12 amdcpp  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 06-February 10

Re: Classes: Calling a function in main

Posted 11 April 2010 - 04:52 PM


Make sure when you change the public method
declaration in the class header file, that you
change it in the implementation file also. For
example, if the code here
void account::makeDeposit(const double d) {
     balance += d;
}


is put in the header file, it will automatically
make it an inline method because you defined it in
the header. If you aren't allowed to use inline
methods, then just type in the header this:
void account::makeDeposit(const double d);


And in the implementation file (.cpp) you would code
the definition for that method:
void account::makeDeposit(const double d) {
     balance += d;
}



The signatures must be the same in each file. By
signature I mean the
void account::makeDeposit(const double d)

// Returns: void
// Takes: const double



If the signature in your header file takes two doubles,
and the signature in your implementation file takes
only one double, the compiler will give you an error
as they will be two seperate methods; one with a
declaration and no definition, and the other with a
definition and no declaration.

This post has been edited by amdcpp: 11 April 2010 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#13 Guest_Guest*


Reputation:

Re: Classes: Calling a function in main

Posted 11 April 2010 - 05:40 PM

I'm getting the linker error " [Linker error] undefined reference to `account::display(std::string, std::string, double)' ". How can I fix this.
Was This Post Helpful? 0

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Classes: Calling a function in main

Posted 11 April 2010 - 06:15 PM

look for the definition of that function, if its not defined that means you have not created it yet, or you have a parameter mismatch. Both should have the same amount of parameters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1