Intermediate Business Programming with C++
When we define a structure, we frequently do it with the intent of manipulating the data. These manipulations require functions to perform the actions. In C++, these functions can be included within the structure. These functions are then called member functions. A structure: Account could be created that permits the setting of the balance or to find the value of the balance and also to permit either a deposit or a withdrawal. For example a structure Account could be defined as in the following:
struct Account { float theBalance; void settheBalance(float); float gettheBalance(); void doDeposit(float); void doWithdrawal(float); };
Notice that the member functions prototypes above are listed in the structure just like the data members. When a structure has functional members, the functions entire definition may be included within the structure's definition block or just the prototype can appear in the structure definition acting as the declaration of the function. If only the prototype of the member function is included within the definition block, then the definition may be anywhere within the program. When the definition of a function is outside of the structure block, the fact that the function is a member of that particular structure must be specified in order to notify the compiler that the function belongs to the specific structure. To do this, the scope specifier operator (::) must be attached to the header when the function is defined. For example:
void Account::settheBalance(float theAmount) { theBalance = theAmount; }
and
float Account::gettheBalance() { return theBalance; }
and
void Account::doDeposit(float theAmount) { theBalance = += theAmount; }
and
void Account::doWithdrawal(float theAmount) { theBalance -= theAmount; }
theBalance is a data member of the instance (i.e. part of the memory of the instance) but the member functions are not a part of any instance. That is, there can be several instances of the structure each of which would have their own: theBalance as part of the memory of each instance. However there are only four member functions for the entire structure.
Using this method of defining the structure Account could be used to hide the member theBalance from outside of the structure. In this case it would be possible to post a deposit or a withdrawal through their respective functional members. To do this, recall that these functions are in fact members of the structure. As with the data members, the programmer accesses these functional members of the structure by also using the dot operator. For example:
Account myAccount; float theAmount; cout << "What was the balance of the account when it started? "; cin >> theAmount; myAccount.settheBalance(theAmount);
establishes theBalance of the instance myAccount to have the value of the float: theAmount.
Further a deposit to myAccount could be done after the code above by doing something like the following:
count << "What is the amount of the deposit? "; cin >> theAmount; myAccount.doDeposit(theAmount); cout << "What is the amount of the withdrawal? "; cin >> theAmount; myAccount doWithdrawal(theAmount);
For example see ACCOUNT.CPP
As another example, consider the structure Date discussed above. There are several different types of functions that could be used with this structure. When dealing with Dates, there are times in which the output requires the programmer to know:
-
what is the day of the week,
-
what is the month's name, or
-
whether a particular year is a leap year.
These are only a few functions that could be performed on a structure. They could be implemented in the following manner:
struct Date { long aMonth, aDay, aYear; void setDate() { char dash; cout << "What is the date? "; cin >> aMonth >> dash >> aDay >> dash >> aYear; if(aYear < 10) aYear += 2000; else if(aYear <100) aYear += 1900; } void monthName() { char monthName[][10] = {"","JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY","JUNE","JULY","AUGUST", "SEPTEMBER", "OCTOBER","NOVEMBER", "DECEMBER"}; cout << endl<< endl << "The date is " << monthName[theMonth] << " " << theDay << ", " << theYear; } }; void main() { Date aDate; aDate.setDate(); aDate.monthName(); }
Notice in this example all of the member functions were defined within the structure definition. See THE_DATE.CPP.
For additional examples see CLOCK1.CPP and DATESTRC.CPP. Note: putch() and kbhit() had to be changed to _putch() and _kbhit() in Visual Studio .NET 2005 because of security reasons. This was done is other examples as well.
Категории