C++: Getters and Setters?

删除回忆录丶 提交于 2019-12-10 21:15:38

问题


I am attempting to write some code to create getters and setters for the ID number, first name, last name, midterm score, and final score of the following data, which is in a text file, in a class i'm writing...

10601   ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91 
10611   THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81 
10622   BEVERLEE WAMPOLE 95 92 91 96 99 97 99 89 94 96 90 97 
10630   TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75 

The ID is the first number, first and last name are self explanatory, the midterm score is the second to last number, and the last number is the final score. This is my first time using getters and setters and so far I have...

int getID()const{return ID;}
void setID(int i) {ID=i}
string getFname()const{return Fname;}
void setFname(string f) {Fname = f}
string getLname()const{return Lname;}
void setLname(string l) {Lname = l}
int getMidterm()const{return Midterm;}
void setMidterm(int m) {Midterm=m}
int getFinal()const{return Final;}
void setFinal(int fl) {Final=fl}

Am I on the right track at all? Also, how do I specify that the first number in the data set is the ID, the first string is the first name, second string second name, second to last number is Midterm, and last number is final score?


回答1:


If you are asking about writing getters and setters, yes, this is how many people do it. To write "perfect" accessors to complex types, you would use this:

void setLname(const String& input) { Lname = input; }    //copies the string
void setLname(String&& input) { Lname = std::move(input); }    //moves the string
const String& getLname() const { return Lname; }    //allows to get a const String reference from a const object
String& getLname() { return Lname; }    //allows modifying access to Lname if the object is not const.

Note, that this is more than the two, people normally talk about, and usually only one getter and one setter are seen. Only very few people would want to write more than that. Yet, each of these four has a slightly different job to do and might be advisable to add.

However, lots of accessors are a code smell:

  1. They litter the code with functions that do not provide any abstraction.

  2. Whenever you have both a getter and a setter, you have essentially unrestricted access to that data member; so you could just as well have written public: String Lname.

  3. Lots of accessors are a certain sign that you are modelling data, not behaviour. In those cases, you should either rethink your design, or be honest and go ahead and use either public members, or, if all members would be public, a struct IMHO.

Note, that I'm talking about the pure accessors here, not the ones, that actually do something sensible, like checking a value range, updating other members accordingly, and so on. And these pure ones are the ones you should avoid.

So, if I were you, I would ask myself: What behaviour do I need from my class? Do I really need to talk about all its data members in other code, or can I find sensible abstractions that my class can provide which would make the accessors useless? Is it possible that I'm thinking about modelling the wrong class of objects? Or do I really just want a container for my data that can be used and modified everywhere?




回答2:


looks fine. some remarks:

  • Pass string as const string& so you don't make a useless copy.
  • More whitespace would help with readability
  • Don't abbreviate things. getFirstName() is more readable than getFname()

As for your second question, you can use the many scanning/stream utilities available in C++, like scanf, sscanf, fscanf, stringstream, iostream, fstream, etc.

std::ifstream inFile("database.txt");
std::string line;
while (std::getline(inFile, line)) // for each line
{
    std::stringstream ss;
    ss << line;

    int id;
    ss >> id;

    std::string firstName;
    ss >> firstName;

    // etc
}



回答3:


Part 1:
That's exactly how getter and setter methods should look (at least in Java).

Part 2:
To set the fields of the class you still have to parse the input manually. I recommend looking into C++ iostreams, especially the >> operator for input streams.



来源:https://stackoverflow.com/questions/18777452/c-getters-and-setters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!