简体   繁体   中英

Would this be considered an Accessor? (C++)

Sorry for the terrible formatting but I just wanted to know for sure if the following would be considered an Accessor.

So my class definition looks something like this...

class work {
        public:
           void getInput(); //Mutator
           void output(); //Accessor?
  //...

So here's the function..

void work::output()
{
  dayofweek = day + getMonthvalue(month, year) + ....;
  final = dayofweek % 7;

  if(final == 0)
    cout << "The day of the date is Sunday\n";

  else if(final == 1)
    cout << "The day of the date is Monday\n";

  else if(final == 2)
    cout << "The day of the date is Tuesday\n";

  /*.
    .
    .*/

  else {
    cout << "The day of the day is Saturday\n";
  }
}

What you've shown as output would more often be written as an inserter :

class work { 
    // ...

    friend std::ostream &operator<<(std::ostream &os, work const &w) { 
        static char const *names[] = { 
          "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
        };

        return os << names[getDayOfWeek(w)];
    }
};

Just to be clear: an inserter gets its name from the fact that it inserts an item of some type into a stream. The mirror image (getting an item of some type from a stream) is an extractor .

If you really insist on a proper name for the code exactly as it stands right now, my own position would be that it's a mistake (forcing output to cout loses flexibility, using an if ladder makes the code ugly and clumsy).

Some terminology

  • A mutator changes the data within the class.
  • An accessor retrieves it.

By this no, not really. Accessor is derived from accessing .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM