C++ How to Declare a Class Instance to be used in multiple functions
<p>I've run into an issue with a project, and wondering if I should have designed something differently from the start.</p>

<p>The goal is to design a menu-driven banking project, that must use classes with inheritance and virtual functions. The parent class is 'Account', with two children 'Checking' and 'Savings'. </p>

<p>I have started the project by designing the menus as functions called from the main, and then was going to call the appropriate virtual member functions of the classes from within the sub-meun functions. The issue I have run into is that the instance of each class is only accessible from within the function that it was created in.</p>

<p>I know dropping them in a header as a global is not the most accepted way of dealing with things, and I have read about Singletons, but my question is are the right way to deal with this, or did I just approach the problem incorrectly to start with? Many comments on this site and others seem to indicate that if you need this, then you are designing your classes wrong.</p>

<p>Code below shows some of what I've laid out so far. I planned on building more functions for the other menu options, but they would all need access to the instances of the classes.</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;string&gt;
#include"Account.h"
#include"Checking.h"
#include"Savings.h"

using namespace std;

void regScreen(), servicesScreen(), checkingScreen(), savingsScreen(), centerString(string);
double validCurrency(string);
const double CHECKING_RATE = .025, SAVINGS_RATE = .0375;
const double CHECKING_START_BAL = 0, SAVINGS_START_BAL = 0;

int main()
{
    regScreen();    


    //Create instance of Checking class
    Checking checkingObject(CHECKING_START_BAL, CHECKING_RATE);
    //Create instanceof Savings class
    Savings savingsObject(SAVINGS_START_BAL, SAVINGS_RATE);

    servicesScreen();

    return 0;
}

void regScreen() 
{
   // user entered variables
   string ssnString, nameString, titleString, accountString;
   // Used to center the text.
   string countString;
   // Get user's name, check for null entry.
   cout &lt;&lt; "Please enter your full name: ";
   getline(cin, nameString);
   while (nameString == "")
   {
    cout &lt;&lt; endl &lt;&lt; "Error: You must enter a name: ";
    getline(cin, nameString);
    }

   // Get user's title, validate entry.
   cout &lt;&lt; "Enter your title (Mr., Mrs., Ms., Dr.): ";
   getline(cin, titleString);
   while (titleString != "Mr." &amp;&amp; titleString != "Mrs." &amp;&amp; titleString != "Ms." &amp;&amp; titleString != "Dr.")
   {
    cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Error: A proper title is required (Mr., Mrs., Ms., Dr.): ";
    getline(cin, titleString);
}

// Get user's SSN, validate for correct format.
cout &lt;&lt; "Enter your SSN (XXX-XX-XXXX): ";
getline(cin, ssnString);
bool validssn; // Used for loop.
do
{
    // Re-Set value at start of each loop.
    validssn = true;

    // Check SSN entry for correct length.
    if (ssnString.length() != 11)
    {
        validssn = false;
    }

    // Check that - are in the correct location.
    if ((ssnString[3] != '-') || (ssnString[6] != '-'))
    {
        validssn = false;
    }           

    // Check that all other elements of the array are digits.
    if (((isdigit(ssnString[0])) || (isdigit(ssnString[1])) || (isdigit(ssnString[2])) || (isdigit(ssnString[4])) 
        || (isdigit(ssnString[5])) || (isdigit(ssnString[7])) || (isdigit(ssnString[8])) || (isdigit(ssnString[9]))
        || (isdigit(ssnString[10]))) == 0)
    {
        validssn = false;
    }

    // If any of the above are incorrect, display error message, get new entry from user.
    if (validssn == false)
    {
        cout &lt;&lt; "Error: You must enter a SSN in the correct format: ";
        getline(cin, ssnString);
    }
} while (validssn == false); // Exit loop if SSN value is good.

// Get user's account number, validate for null entry.
cout &lt;&lt; "Enter your account ID: ";
getline(cin, accountString);
while (accountString == "")
{
    cout &lt;&lt; endl &lt;&lt; "Error: You must enter a name: ";
    getline(cin, accountString);
}

// Personalized welcome message.
cout &lt;&lt; endl &lt;&lt; endl;
countString = "How are you " + titleString  + " " + nameString + "?";
centerString(countString);
cout &lt;&lt; endl;
countString = "Press &lt;Enter&gt; for our banking services....";
centerString(countString);
cout &lt;&lt; endl &lt;&lt; endl;
cin.get();
system("cls");

// Instance of class Account to hold strings that other classes will inherit.
Account accountObject(nameString, titleString, ssnString, accountString);   
}
</code></pre>
