我遇到了一个项目的问题,想知道我是否应该从一开始就设计一些不同的东西。
目标是设计一个菜单驱动的银行项目,该项目必须使用具有继承和虚函数的类。父类是“Account”,有两个孩子“Checking”和“Savings”。
我通过将菜单设计为从主函数调用的函数来启动该项目,然后从子菜单函数中调用类的适当虚拟成员函数。我遇到的问题是每个类的实例只能从创建它的函数中访问。
我知道将它们作为全局放在标题中并不是最被接受的处理事物的方式,而且我已经阅读过关于单例的内容,但我的问题是处理这个问题的正确方法,还是我只是错误地处理了这个问题从...开始?该站点和其他站点上的许多评论似乎表明,如果您需要这个,那么您设计的课程是错误的。
下面的代码显示了我迄今为止列出的一些内容。我计划为其他菜单选项构建更多功能,但它们都需要访问类的实例。
#include<iostream>
#include<string>
#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 << "Please enter your full name: ";
getline(cin, nameString);
while (nameString == "")
{
cout << endl << "Error: You must enter a name: ";
getline(cin, nameString);
}
// Get user's title, validate entry.
cout << "Enter your title (Mr., Mrs., Ms., Dr.): ";
getline(cin, titleString);
while (titleString != "Mr." && titleString != "Mrs." && titleString != "Ms." && titleString != "Dr.")
{
cout << endl << endl << "Error: A proper title is required (Mr., Mrs., Ms., Dr.): ";
getline(cin, titleString);
}
// Get user's SSN, validate for correct format.
cout << "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 << "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 << "Enter your account ID: ";
getline(cin, accountString);
while (accountString == "")
{
cout << endl << "Error: You must enter a name: ";
getline(cin, accountString);
}
// Personalized welcome message.
cout << endl << endl;
countString = "How are you " + titleString + " " + nameString + "?";
centerString(countString);
cout << endl;
countString = "Press <Enter> for our banking services....";
centerString(countString);
cout << endl << endl;
cin.get();
system("cls");
// Instance of class Account to hold strings that other classes will inherit.
Account accountObject(nameString, titleString, ssnString, accountString);
}