Explain why a class might provide a set function and a get function for a data member.

What will be an ideal response?

A data member is typically declared private in a class so that only the member func- tions of the class in which the data member is declared can manipulate the variable. A class typically provides a set function and a get function for a data member to allow
clients of the class to manipulate the data member in a controlled manner. A set func- tion should validate the data it is setting to ensure that invalid data is not placed in the object. A get function can return the value of a data member without allowing
clients to interact with the data member directly. In addition, set and get functions hide the internal data representation. For example, a Time class might represent the time as the total number of seconds since midnight. Such a class can provide member functions getHour, getMinute and getSecond to calculate the hour, minute and sec- ond, respectively—even if the underlying data representation does not contain hour, minute and second data members. Similarly, a Time class could provide member functions setHour, setMinute and setSecond to set the current hour, minute or second value. This would require each set function to perform calculations that update the data member containing the total number of seconds since midnight.

Computer Science & Information Technology

You might also like to view...

Explain why it is important to use the right words when describing your problem to tech support.

What will be an ideal response?

Computer Science & Information Technology

____ are Ruby on Rails programs that manage the relationship between controllers and the application's database.

A. Views B. Models C. Controllers D. Records

Computer Science & Information Technology