2014-12-11 34 views
0

对不起,可怕的格式,但我只是想知道如果以下将被视为Accessor。这会被视为Accessor吗? (C++)

所以我的类定义看起来是这样的......

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

因此,这里的功能..

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"; 
    } 
} 
+3

通常,*存取器*返回正在*存取*的数据。 – 2014-12-11 02:40:55

+1

通常,* mutator *会更改数据成员的值。 – 5gon12eder 2014-12-11 02:43:29

+3

我会同时打电话给他们两个 – 2014-12-11 02:45:30

回答

1

一些术语

  • 增变更改数据在课堂上。
  • 访问者检索它。

由此否,不是真的。 访问器来自访问

2

你已经证明什么为output会更经常被写为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)]; 
    } 
}; 

只要是明确的:一个inserter得到它的名字来自它插入某些类型的项目成事实流。镜像(从流中获取某种类型的项目)是extractor

如果你真的坚持了正确的名称,代码完全按照目前的情况是正确的,我自己的立场是,这是一个mistake(强制输出到cout失去弹性,使用if梯使代码丑陋而笨拙) 。