convert to a class

Hi,

Please, can you help me, i wante to convert or use this code in a class:



#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;
char c;

initialize(t, a, b, c);
std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", t = " << t;

return 0;
}
Last edited on
Do you mean that you would like to have the main(), for example:
1
2
3
4
5
6
int main()
{
  MyType sample;
  std::cout << sample;
  return 0;
}


Your class MyType would obviously have member variables
and constructor that defines the initial value of each member variable.

The output should use standalone function, operator overloading:
1
2
3
4
5
std::ostream& operator<< ( std::ostream& out, const MyType& rhs )
{
  // output members of 'rhs' to 'out' here.
  return out;
}
Hello jeanpaulw,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

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

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.

In the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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.

You may find this useful as a starting point on classes.
https://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/

The rest of the site can be useful for reference in the future.

Another good site for reference is:
https://en.cppreference.com/w/

Work something up and post it, so you can get input on how you have done.

Andy
t looks to be better off as the result of a method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

class Thing
{
private:
    int a{0};
    int b{0};
    char c{'?'};
public:
    Thing(){};
    Thing(int aA, int aB, char aC){ a = aA; b = aB; c = aC; };
    ~Thing(){};
    
   /* int getA() const {return a;}
    int getB() const {return b;}
    char getC() const {return c;} */ // LATE get's SCRATCHINGS FOR VARIOUS (friend) REASONS
    int getT() const {return a + b;}
    
    friend std::ostream& operator<<(std::ostream& os, const Thing& th);
};

std::ostream& operator<<(std::ostream& os, const Thing& th)
{
    os
    << "a = " << th.a << ", b = " << th.b << ", c = " << th.c
    << ", t = " << th.getT();
    return os;
}

int main()
{
    Thing thing_1;
    std::cout << thing_1 << '\n';
    
    Thing thing_2(10, 20, 'A');
    std::cout << thing_2 << '\n';
    
    return 0;
}



a = 0, b = 0, c = ?, t = 0
a = 10, b = 20, c = A, t = 30
Program ended with exit code: 0
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 be put in the initializer list, too.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41


#include <iostream>
#include <string>
using namespace std;

class Thing
{
private:    
   int a, b;
   string c;
   int t;
public:
   Thing( int aval, int bval, string cval ) : a(aval), b(bval), c(cval) { t = a + b; }
   void print() {cout << "a = " << a << ", b = " << b << ", c = " << c << ", t = " << t << '\n'; } 

   
   void print1() {cout << "a = " << a << '\n'; }
   void print2() {cout << "b = " << b << '\n'; }
   void print3() {cout << "c = " << c <<  '\n'; }
   void print4() {cout << "t = " << t << '\n'; }
};

int main()
{
   Thing thing( 19, 30, "aaa");
   Thing thing1( 19, 30, "aaa");
   Thing thing2( 19, 30, "aaa");
   Thing thing3( 19, 30, "aaa");
   Thing thing4( 19, 30, "aaa");
   
   thing.print();
   thing.print1();
   thing.print2();
   thing.print3();
   thing.print4();
   
    return 0;
}

Last edited on
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.
Last edited on
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.
getters provide users with complete flexibility, to_string functions less so.
agreed; that ties to my first statement (I don't care for having them at all) but sometimes you do what someone told you to.
... 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
Topic archived. No new replies allowed.