如何在 C++ 中为数组的元素创建访问器? [关闭]

问题描述 投票:0回答:0

编辑**:好的,所以我不确定混淆在哪里,但我的问题在标题“How do I create an accessor for an Array's elements in C++?”中有明确说明。 这是“基本”问题。 我在所有问题中都只提到数组。 menu() 不是访问器,但它也与我的问题无关,也与我的问题无关。

我被要求创建一个涉及打印星期几的类。

“设定日期。 打印日期。 当天返回。 第二天回来。

我为一周中的几天创建了一个数组:(出于我的问题的目的,代码已经大大缩短了) 我为字符串日创建了 set 和 get 但我想知道如果我可以(或者如果它的常见做法是?)对数组做同样的事情?


#include <iostream>
#include <string>

using namespace std;
class dayType
{
public:
    void menu();                           
    void setDay(string);                   //mutator; Independent of the array.
    string getDay() const;                  //accessor; Independent of the array .
    string getDaysArray();                 //accessor. What else do I need?

private:
    string day;
//Has nothing to do with the array. 
    string days[7] = {"Mon", "Tues", "Wed",
                     "Thurs", "Fri", "Sat", "Sun"};

string dayType::getDay() const // not asking about this
{
    return day;
}


}

我对指针和 几乎没有触及创建类的表面。

我已经写出了整个代码,但在我的函数中:

void dayType::nextDay() //Takes the set day and prints the next day

我正在直接访问数组 days[7],我知道访问器和修改器通常用于变量,只是不确定是否应该使用数组完成以及应该如何完成? 如果我们可以坚持这种编码风格,我知道它可能不是最广泛接受的风格,但这是我学习的方式以及我现在继续学习的方式。

c++ arrays oop accessor mutators
© www.soinside.com 2019 - 2024. All rights reserved.