[ Pobierz całość w formacie PDF ]
.If we say class Student_info, then every member between the first { and the first protection label isprivate.If, instead, we write struct Student_info, then every member declared between the { and the first protectionlabel is public.For example,class Student_info {public:double grade() const;etc//.};is equivalent tostruct Student_info {public by defaultdouble grade() const; //etc.//};andclass Student_info {by defaultstd::string name; // private This document is created with the unregistered version of CHM2PDF Pilotother private members//public:double grade() const;other public members//};is equivalent tostruct Student_info {private:std::string name;other private members//public:double grade() const;other public members//};In each of these definitions, we're saying that we'll allow users to get at the member functions of Student_info objects,but we will not allow them to access the data members.There is no difference between what we can do with a struct or a class.In fact, there is no way, short of reading thecode, for our users to distinguish whether we used struct or class to define our class type.Our choice of struct orclass can have a useful documentation role.In general, our programming style is to reserve struct to denote simpletypes whose data structure we intend to expose.For that reason, we used struct to define our original Student_infodata type in Chapter 4.Now that we intend to build a type that will control access to its members, we use class todefine our Student_info type.9.3.1 Accessor functionsAt this point, we've hidden our data members, so that users can no longer modify the data in a Student_info object.Instead, they must use the read operation to set the data members, and the grade function to find out the final gradefor a given Student_info.There's one more operation that we must still provide: We must give users a way to get atthe student's name.For example, think a bit about the program in 4.5/70, in which we wrote a formatted report ofstudent grades.That program needs to access the student's name in order to generate the report.Although we wantto allow read access, we do not want to allow write access.Doing so is straightforward:class Student_info {public:double grade() const;must change definitionstd::istream& read(std::istream&) //addedstd::string name() const { return n; } //private:changedstd::string n; //double midterm, final;std::vector homework;};Instead of giving our users access to the name data member, we've added a member function, also named name, thatwill give (read-only) access to the corresponding data value.Of course, we have to change the data member's nameto avoid confusing it with the name of the function.The name function is a const member function, which takes no arguments and which returns a string that is a copy ofn.By copying n, rather than returning a reference to it, we ensure that users can read but not change the value of n.Because we need only read access to the data, we declare the member function as const.When we defined grade and read, we did so outside the class definition.When we define a member function as part This document is created with the unregistered version of CHM2PDF Pilotof the class definition, as we have done here with the name function, we are hinting to the compiler that it should avoidfunction-call overhead and expand calls to the function inline (4.6/72) if possible.Functions such as name are often called accessor functions.This nomenclature is potentially misleading, because itimplies that we are granting access to a part of our data structure.Indeed, historically, such functions were oftenintroduced to allow easy access to hidden data, thus breaking the encapsulation that we were trying to achieve.Accessors should be provided only when they are part of the abstract interface of the class.In the case ofStudent_info, our abstraction is that of a student and a corresponding final grade.Therefore, the notion of a studentname is part of our abstraction, and it is fitting to provide a name function.On the other hand, we do not provideaccessors to the other grades midterm, final, or homework.These grades are an essential part of ourimplementation, but they are not part of our interface.Having added the name member function, we can now write the compare function:bool compare(const Student_info& x, const Student_info& y){return x.name()template >(istream& is, Str& s){obliterate existing// value(s)s.data.clear();read and discard leading whitespace//char c;while (is.get(c) && isspace(c))nothing to do, except testing the condition; //if still something to read, do so until next whitespace character//if(is) {do s.data.push_back(c); // compile error!, data is privatewhile (is.get(c) && !isspace(c));if we read whitespace, then put it back on the stream//if (is)is [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • lunamigotliwa.htw.pl
  •