Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to call function without connecting it with the object but it always give error that it is undefined.
In program below i am trying to call displayMenu() but getting error on it. when i connect it with any object then it works fine.

What I have tried:

#include <iostream>
using namespace std;
class BankAccount{

    string f_name,l_name,acc_type;
    long balance,acc_num;
    public:
    //cunstructor to set values automatically
    BankAccount(string fname, string lname, string acctype, long bal){
            f_name=fname;
            acc_type=acctype;
            l_name=lname;
            balance=bal;
            if(balance<50000)
            {
                acc_type="Basic";
            }
    }
    //displays menu 
   void displayMenu()
    {
        cout<<"Select an option\n\n1.Deposit/withdraw\n2.Change Account type\n3\n4.Change Name\n5.Display Details \n ";
    }
    //let user change name 
    string changename()
    {
        cout<<"Your current name is "<<f_name<<" "<<l_name<<" Enter your details below to change it\n";
        cout<<"Enter your first name \n";
        cin>>f_name;
        cout<<"Enter your second name \n";
        cin>>l_name;
    }
    //returns first name;
   string getf_name()
    {
        return f_name;
    }
        //returns second name;

   string getl_name()
    {
        return l_name;
    }
    //returns account number
    long getAccountNumber()
    {
        return acc_num;
    }
    //let user change account type
    string changeAccountType()
    {
        char ch;
        if(balance<50000)
        {
            cout<<"Account type can't be changed because balance is less than 50,000. You need to maintain atleast 50,000 or more in your account if you want to switch to current account\n";
        }
        else
        cout<<"Your Account type "<<" "<<acc_type<<" Do you wish to change it to Basic? \n";
    }

        //displays all account details
//let user deposit and withdraw 

int depositWithdraw()
{
int n;
int amount;
cout<<"Do you want to deposit or withdraw\n 1. Deposit \n 2.Withdraw"<<endl;
cin>>n;
if(n==1)
{
cout<<"Enter amount to deposit\n";
cin>>amount;
balance+=amount;
}
else if(n==2)
{
cout<<"Enter Amount to withdraw\n";
cin>>amount;
if(balance<amount)
{
cout<<"You don't have enough balance\n";
}
else if(n==2)
{
    balance=balance-amount;
    if (balance<50000)
    {
        acc_type="Basic";
    }

}

}
}


void displayDetails()
{
    cout<<"Name: "<<f_name<<l_name<<endl;
    cout<<"Account type: "<<acc_type<<endl;
    cout<<"Account Balance "<<balance;
}
};

int main()
{
            BankAccount customer1("Hamza","Jameel","current",25000);
            BankAccount customer2("Bilal","Aziz","current",25000);
            BankAccount customer3("Raja","Ali","current",60000);
            BankAccount customer4("Abdullah","Shafique","basic",75000);
            
            displayMenu();

            customer1.changeAccountType();
}
Posted
Updated 12-Mar-22 7:03am
v2
Comments
Rick York 12-Mar-22 12:50pm    
I consider it much better to write :
cout<<"Select an option\n";
cout<<"\n";
cout<<"1.Deposit/withdraw\n";
cout<<"2.Change Account type\n";
cout<<"3\n";
cout<<"4.Change Name\n";
cout<<"5.Display Details\n";

Change the signature for the display menu from

void displayMenu()


to
static void displayMenu()


then you can call it using

BankAccount::displayMenu();
 
Share this answer
 
Comments
Hamza Jameel 12-Mar-22 10:51am    
Thanks ;)
OriginalGriff 12-Mar-22 10:56am    
Um. Are you sure? Those look like member variables it's using there ...
Rick York 12-Mar-22 12:46pm    
displayMenu does not access any member variables.
OriginalGriff 12-Mar-22 13:29pm    
This is true, but the sensible next step is to put the code that actions the user selection in there as well (so they are kept together and hopefully in step) and those functions do need instance variables.

To be accurate, you'd really want to separate the "Menu processing" stuff from the "Account" class completely, so this wouldn't be a problem - you know that as well as I do!
Tony isn't really right when he suggests you use a static method!
Because your function - correctly - accesses member variables of the class, you cannot declare it as static. Or if you do, the member variables have to be statiuc as well, and that means that your whole app can only have one account name, balances, and so on.

Which isn't what you want - in the real world, each bank account is separate - it doesn't matter how much money you have in your account, I can only spend what I have in mine, and vice versa.
That's what classes are there for: the keep related data together in what is called an instance - your account is one instance, my account is a different instance.
It;'s just like cars: your car is different from my car, they are separate instances. If you put your mobile in the glove box of your car and then we go for a drive in my car, would you expect to find your mobile in my glove box? Of course not! You understand instances, just you never thought of them by that name, is all.

So what you actually need to do is create an instance of your class and then use that to call the displayMenu function so that it fills the information into the right instance:
C++
BankAccount myAccount;
BankAccount yourAccount;
myAccount.displayMemu();
yourAccount.displayMenu();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900