How do I return values from class

closed account (9G3v5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class PaySlip{
  private:
    string name;
    int payGrade, basicSal, overtimeH, overtimeP, grossP, netP, withT;
  public:
    void setValue(string names, int payGrades, int basicSals, int overtimeHs, int overtimePs, int grossPs, int netPs, int withTs){
      name = names;
      payGrade = payGrades;
      basicSal = basicSals;
      overtimeH = overtimeHs;
      overtimeP = overtimePs;
      grossP = grossPs;
      netP = netPs;
      withT = withTs;
    }
    int getValue(){          // what data type should i use?
      return payGrade, basicSal, overtimeH, overtimeP, grossP, netP, withT;
    }
};


As you can see I am returning the values of those variables in integer data type. How would I return the remaining variable called 'name'? It is in string type
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2)
As you can see I am returning the values of those variables in integer data type.

I can assure you, you're not. You're returning a single integer, and that integer has the value of withT.

Do you understand what the comma operator in C++ actually does? I'm guessing you don't.

https://en.cppreference.com/w/cpp/language/operator_other

If you want to pass multiple values back from a function, your options (or, at least, the good ones) are:

- pass them as arguments to the function, by reference
- return an object that encapsulates all the values you want to pass back, e.g. a struct, or perhaps a tuple.


Last edited on
closed account (9G3v5Di1)
can you please write how to do that
Here is a tutorial on how to pass arguments by reference:

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Here is a tutorial on how to use structures:

http://www.cplusplus.com/doc/tutorial/structures/

(I'll leave aside tuples, as that's a little more advanced.)

I'm not going to write your code for you. You'll learn much better by reading and understanding what's needed, and then attempting it yourself. If you hit problems, show us what you've done, and we'll help you fix those problems.
An alternative to returning multiple is to return single values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    void setValue( string names, int payGrades, int basicSals,
        int overtimeHs, int overtimePs, int grossPs, int netPs, int withTs )
    // member initialization list
    : name( names ),
      payGrade( payGrades ),
      basicSal( basicSals ),
      overtimeH( overtimeHs ),
      overtimeP( overtimePs ),
      grossP( grossPs ),
      netP( netPs ),
      withT( withTs )
    {
      // empty body
    }

    string getName() const { return name; }
    int getGrade() const { return payGrade; }
    int getSal() const { return basicSal; }
    // more "getters" 


PS. I don't know what the values of your class are, but can I get a PaySlip with $2 gross and $1'000'000 net?
closed account (9G3v5Di1)
what is the use of const before the return
The const is part of the function signature, and not part of the body itself.

const at the end of the function signature of a class function (method) tells the compiler that that function shall not modify member variables of the class.* It becomes a compiler error if you were to try to do payGrade = 3; within string getName() const { ... } for example.

Check out:
https://www.geeksforgeeks.org/const-member-functions-c/
https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration

(*Unless it's mutable... but don't worry about that unless you want to.)
https://www.geeksforgeeks.org/c-mutable-keyword/
Last edited on
There are two parts to that (as described in the linked texts).

Given:
1
2
3
4
5
6
class Foo {
  // members
public:
  void foo();
  void bar() const;
};


1. The compiler enforces the restriction on the implementation:
1
2
3
4
void Foo::bar() const
{
  // compiler prevents you from modifying *this (accidentally)
}


2. Compiler can allow use of members of const objects:
1
2
3
const Foo snafu;
snafu.bar(); // ok and safe, bar won't attempt to change the snafu
snafu.foo(); // error. snafu is const. Not safe to call foo() that might modify 
Topic archived. No new replies allowed.