简体   繁体   中英

Set and get member functions manipulation of data members

So I'm an newbie to programming and I have encountered a case for which I suppose qualifies as an authentic question in this awesome forum. Is there a way to write statements inside my get functions so that I can obtain all the changed data member values without having to create multiple get functions for each data member?
Regards

I am practicing building programs which are easy to maintain by localizing the effects to a class's data members by accessing and manipulating the data members through their get and set functions. In this regard I have two data members for which I wish to change. After compiling, the set functions works well by changing the values but the get functions can only return one of the data member values at a time.

class GradeBook
{
    public:
    void setCourseName(string code,string name)
    {   
        CourseCode = code;
        CourseName = name;
     }

    string getCourseName()
    {
        return CourseCode;
        return CourseName;
    }

    void displayMessage()
    {
        cout<<"Welcome to the GradeBook for: \n" <<getCourseName()
        <<endl;
    }
    private:
        string CourseName;
        string CourseCode;
};//end class GradeBook

After compiling and running the program, the program outputs the CourseCode but the CourseName doesn't get displayed. I had to create two get functions each to obtain the two data members. I don't want to have 2 get functions to obtain the data member values. I just want to use one get function to keep the code at minimum.I wish to use one get function to return two values for each data member. I have already tried using one return statement and separated the data members with a comma.

Your idea of using return twice cannot work, the first return will return control to the caller and the second will never be executed. You should have got warning about it from your compiler. I believe that an initial solution could be to use std::pair (docs: https://en.cppreference.com/w/cpp/utility/pair ), see snippet below.

NOTE: using namespace std; (which is most likely what you are doing in the code you do not show), is a bad practice, consider using the fully qualified name

#include <string>
#include <utility>
#include <iostream>

//Bad practice, I added it only to keep differences with OP code small
using namespace std;
class GradeBook
{
    public:
    void setCourseName(string code,string name)
    {   
        CourseCode = code;
        CourseName = name;

    }


    std::pair<string, string> getCourseName()
    {
        return {CourseCode, CourseName};
    }

    void displayMessage()
    {
        //only in C++17
        auto [code, name] = getCourseName();
        cout<<"Welcome to the GradeBook for: \n" << code << " - " << name
        <<endl;
    }

    private:
        string CourseName;
        string CourseCode;

};//end class GradeBook

Note that auto [code, name] is a feature called structured binding, available only in C++17, if you have an older compiler, you have to return a std::pair<std::string, std::string> and access its elements using the member variables first and second . Now, std::pair is good for this contrived example, but, for your case, you might want to consider doing something a bit more readable, because the elements of the pair have the same type so the user of your library will have difficulties remembering what is the first and second element. So you might want to use a custom-made struct with some more meaningful names.

#include <string>
#include <utility>
#include <iostream>

//Bad practice, I added it only to keep differences with OP code small
using namespace std;

struct CourseCodeAndName{
    std::string code;
    std::string name;
};

class GradeBook
{
    public:
    void setCourseName(string code,string name)
    {   
        CourseCode = code;
        CourseName = name;

    }


    CourseCodeAndName getCourseName()
    {
        return {CourseCode, CourseName};
    }

    void displayMessage()
    {
        auto codeAndName = getCourseName();
        cout<<"Welcome to the GradeBook for: \n" << codeAndName.code << " - " << codeAndName.name
        <<endl;
    }

    private:
        string CourseName;
        string CourseCode;

};//end class GradeBook

See this example. Alternatively you can use std::tuple.

class GradeBook
{
/* ... */
public:
    std::pair<std::string, std::string> get(){
        return std::make_pair(CourseName, CourseCode);
    }
};        

int main()
{       
    GradeBook book1("Hello","World")
    auto result = book1.get();
    cout << result.first << result.second;
}

If you write: return x,y; or: return x; return y;

You should know that in first case you get the last value (you get y), and in second case you get the value of first return (you get x, because as soon as compiler see return, function will return the value, and then function will go in epilogue state (cleaning of stack memory assigned to function, both inline and non-inline function).

And about the use of get function it's normal. If you want to use the value to do something of logic (not to display), yes you should use a lot of get function. Instead if you want to display the values, use a void function, for example "void printData();", and inside it write code to print data. You probably setted the class variables as private (following the encapsulation rules) so you will have access to them inside the print function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM