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

88 Views Asked by At

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";
  }
}
2

There are 2 best solutions below

0
On

Some terminology

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

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

0
On

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).