The next function is also a constructor (done by overloading the constructor function) that allows your program to specify the values to assign to the class’s members.
Customer::Customer(int nRecType, |
// The class constructor, explicit |
|
// values |
char * szCustName, |
|
char * szAddr, |
|
double dSales) |
|
{ |
|
nRecordType = nRecType; strcpy(szCustomerName, szCustName); strcpy(szAddress, szAddr); dCurrentSales = dSales;
}
You next define the class function that gets, from the keyboard, new values for the class’s members. This function uses C++’s cin and cin.getline classes. Using cin for numeric values is fine; however for character string values, cin.getline is better because it limits the number of characters assigned and ignores any whitespace characters in the input string. Class functions, other than constructors and destructors, can have return values. You can use these return values to indicate either success or failure of the function, or to return a class member’s value.
void Customer::GetCustomer()
{
char szLine[2]; // Used to store a NEWLINE for cin.getline
//You get, from the console, the object’s data values, using a simple
//multiline format:
cout << “Enter ‘“ << CUSTOMER_RECORD << “‘ for a Customer ‘“ << SUPPLIER_RECORD << “‘ for a supplier: “;
cin >> nRecordType;
//Below you don’t use cin, but cin.getline, which gets all
//characters until the delimiting character (the optional third
//character). If the delimiting character is omitted, assume a
//newline. When getting input, cin.getline() does not
//retrieve more characters than the second parameter specifies,
//taking into account the ending NULL for the string.