mardi 20 février 2018

Private getters and setters [on hold]

I am still new to C++ and OOP programming, and I was just wondering about something curious. I have a class let's name it Foo, with some private members. My question is: the Foo objects don't "pass data" to other objects during their lifespan. They receive data, do things, and save new data to file. That means, only Foo objects will access Foo private members. Is it wrong to implement private getters and setters? Or should I use direct access? Pseudo code below:

Is this okay?

class foo
{
  private:
    string a;
    string b;
    string c;
    void setA(string A){this->a=A;}
    string getA()const{return this->a;
    void setB(string B){this->b=B;}
    string getB()const{return this->b;
    void setC(string C){this->c=C;}
    string getC()const{return this->b;
  public:
    //many omitted methods//
    void Method(); //<-- this method does things and calls private getters and setters to modify private members
}

In main:

{
Foo obj=....;
obj.Method();
}

Or should I:

class foo
{
  private:
    string a;
    string b;
    string c;
  public:
    //many omitted methods//
    void Method();
}

void foo::method()
{
    string s1;
    //initialize s1;
    this->a=s1; //operation example
    std::cout<<"A equals: "<< this->a;
}

Not sure if I explained my concerns in simple way. Thank you in advance for your replies and help.

Aucun commentaire:

Enregistrer un commentaire