Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
A tip: It is a good idea to always initialize your variables when they are defined.
#include <iostream>
// Function to return multiple values using references
void initialize(int &t, int &a, int &b, char &c)
{
a = 10;
b = 20;
t = a + b;
c = 'A';
}
// Return multiple values from functions in C++
int main()
{
int t{}, a{}, b{}; // <--- Initialized variables.
char c{};
initialize(t, a, b, c);
std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", t = " << t;
return 0;
}
I believe the only reason that line 18 is not an error is because the variables are passed by reference.
#include <iostream>
class Thing
{
int a, b;
char c;
int t;
public:
Thing( int aval, int bval, char cval ) : a(aval), b(bval), c(cval) { t = a + b; }
void print() { std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", t = " << t << '\n'; }
};
int main()
{
Thing thing( 10, 20, 'A' );
thing.print();
}
t could in fact be, however it’s derived from a and b so is appropriate as a method result rather than a member. Makes for simple and robust code despite deliberately going against the obvious @OP question.
Hi, thank you very much for you help and for all answers,
i've made a little change, tell me what do you think, because i need returne multiple value from the class(indepandant), in order to use each value in separate variable.
The point of having an object of type Thing is that a single object will hold all the data members and access all the member functions that you need.
Your lines 26-30 declare five different objects that all have the same exact data in them, so they are entirely redundant.
All you need is one object:
Thing thing(19, 30, "aaa");
Then you call all the member functions on that one instance:
1 2 3 4
thing.print();
thing.pirnt1();
thing.print2();
// etc.
Also note that returning something and printing something are two different things, although it is easy to conflate them if you only ever do simple things like printing values.
Currently, your functions return nothing (return type: void), but print to the screen (cout).
If your assigment is to actually return values, you would have to do something like: int getA() { return a; } etc.
Lastly, note that function names like "print1", "print2", "print3" are really poor names because they don't tell me anything about how they actually differ from one another. This is partially due to the fact that your class itself is very contrived and without a clear purpose. I understand this might just be for practice, but just something to keep in mind.
I am often odd from what others say, but I dislike print functions and << overloads.
I feel the user should DIY on how and what THEY want to print for THEIR project.
so I like to return either a string (instead of a print to console) in print functions or a vector of strings (depend on the data) and if I do a << it needs to be possible to override it.
... just like treating 't' as a data member and not as the more appropriate derived value.
BTW your to_string() fits very nicely with the overloaded operator<<, so everybody wins, and maybe in that string function is where/how the overloaded operator<< can be more flexible