Class help, again

Ok so im trying to learn how to use classes the right way and i watched some tutorials and he can change the string from the class object but what if i want the user it input into the string?

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
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

class MyClass
{
    public:
        void setStrings(string Sa, string Sb)
        {
            Pname = Sa;
            PLname = Sb;
        }
        string getString()
        {
            return Pname;
            return PLname;
        }

        void setInts(int Ia, int Ib)
        {
            money = Ia;
            health = Ib;
        }
        int getInts()
        {
            return money;
            return health;
        }

    private:
        string Pname;
        string PLname;
        int money;
        int health;
};

int main()
{
    MyClass MCO;

    cin >> MCO.setStrings(Sa);


}


I don't believe that code will compile...

You're also learning from a bad source and not really learning the "proper" way to use classes.

Start Rant:

What is it with people these days thinking they know some basic syntax of a language and can make tutorials. Have they ever actually asked anyone if they think their information is accurate? And why post it on YouTube? Is it because they just want to get more hits than some other guy? WTF?

And what's with all of these people refusing to read tutorials? Why have a lot of people been resorting to video tutorials who aren't even acknowledged by anyone in the programming community? I must admit, you can say the same about most books and even online references and tutorials, but at least this site has members who can back the tutorials/references. I don't believe our information is the best, and at times I wonder if it's really that good, but the fact remains that someone did take several days, if not months, out of their life to type up a lot of the articles here.

My point is, don't just assume that because some guy (who probably still lives in his mom's basement) is making a C++ tutorial on YouTube that he knows what he's talking about, is really teaching you the right way, or that he isn't just giving you false hope. Read articles here, do some Google searches for code, ask questions here, buy a book.

End Rant:
Like Volatile Pulse said, make sure your tutorials have some credibility. The articles on classes here are good:
http://www.cplusplus.com/doc/tutorial/classes/
1
2
3
4
5
string getString()
{
    return Pname;
    return PLname;
}

You can't return two things from one function. If you must use accessor (get) functions, then you need one get function per private variable.

As for your question, you can do the cin >> stuff in one of your class' functions like the constructor. Or if you want to do it in main...
1
2
3
4
5
6
7
int main()
{
    MyClass MCO;
    string input1, input2;
    cin >> input1 >> input2;
    MCO.setStrings(input1, input2);
}


The value of input1 and input2 is then copied into "Sa" and "Sb", which your function then copies into Pname and PLname.
Last edited on
The guy actually had this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class BuckysClass{
    public:
        void setName(string x){
            name = x;
        }
        string getName(){
            return name;
        }
    private:
        string name;
};

int main()
{
    BuckysClass bc;
    bo.setName("sir bucky wallace");
    cout << bo.getName();
    return 0;
}



all the other stuff was my doing, i didnt know you couldnt return 2 values from one function. So everytime i want to make a variable i have to put it in its own function?

can i do this?:

return Pname, PLname;
Last edited on
So everytime i want to make a variable i have to put it in its own function?

No, it's a variable not a function. If you want accessor/mutator functions then yes you should only have one accessor/mutator per variable.

can i do this?:

return Pname, PLname;


No, you can only return one single thing. So make it two functions.
1
2
3
4
5
6
 string getPName() {
  return PName;
}
string getPLname() {
  return PLName;
}
I'm beginning to think that classes are still a bit too advanced for you simply because you don't understand basic syntax of functions. Classes aren't overly difficult, but mix that with a lack of knowledge in a certain area, you're asking for disaster.

To answer your question, no. You're still attempting to return two values. A function can return 1 value at max. There is ways around this, but that's not the point of this thread.

The idea behind threads is to wrap a bunch of related "things" into one name. This makes it easier to access each "thing". If you realize you're writing code for the user's first name and last name, their age, phone number, etc and have functions that manipulate this data quite often, that's when you look to classes. It's very easy to move one class to another program because classes are typically held within their own header files (corresponding class can be within the same files) to keep it easy for you to reuse your class. You just need to link with the .cpp file and include the header file.

Going back to your issue. Where do you feel you're really having trouble with? What have you been reading/watching/trying that you just didn't understand (before classes). I know this isn't your first time programming, you've been here a while, but you seem like you still have a lot of questions. Feel free to ask whatever you need to.

For extra support, when you're bored and you have something on your mind, open up a new project and start coding. Grab code snippets from other programs (it'll usually be yours or someone's on the forum) and see if there is another way you can do something. Also, look to see if there is something you didn't understand 100%. Remember, just because you haven't learned it, doesn't mean you can't do it. Try it, the worse that can happen is that you have some memory leaks (if anything worse happens by accident you probably shouldn't be touching a computer anyways) so feel free to try an idea out, or some question that's been sitting in the back of your mind.
i have to make a new function for each return, that seems like a waste of space and alot of effort, there isnt an easier way? also i just have to make a new function for each return and not for the variables in the other functions right? so i can have all my ints in one function but i have to return them in seperate functions?
You don't have to do anything with classes besides of following the syntax. I believe if you don't like the concept of classes, you should use structs instead. They're identical, but structs have public members and are typically used without getter/setter methods. Look at some code that's floating around to understand the concept of why people suggest these getter/setter methods. It really enhances the code.
To elaborate by what I mean with using classes, here is an example: http://pastebin.com/qA3eub6J

I wrote this not too long ago and it is a header class that allows you to create a text based menu. A code snippet from it:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Define Menu Positions
enum position{LEFT, CENTER, RIGHT, TOP, BOTTOM};
 
class Menu {
public:
    // Define Empty Menu with a Title and set Left and Right Selectors
    Menu (std::string title = "",
          std::string leftSelector = "",
          std::string rightSelector = "",
          position verticalPosition = TOP,
          position horizontalPosition = LEFT) {
        SetTitle(title);
        SetSelectors(leftSelector, rightSelector);
        SetLocation(verticalPosition, horizontalPosition);
        Wrap(true);
        iLastSelection = 1;
    }
 
    // Set Title for the Menu
    void SetTitle(std::string title = "") {
        strMenuTitle = title;
    }
 
    // Set Left and Right Selectors
    void SetSelectors(std::string leftSelector = "", std::string rightSelector = "") {
        strLeftSelector = leftSelector;
        strRightSelector = rightSelector;
    }
 
    // Set Menu Location
    void SetLocation(position verticalPosition = TOP, position horizontalPosition = LEFT) {
        posVPosition = verticalPosition;
        posHPosition = horizontalPosition;
    }
 
    // Allow Option Selection to return from bottom/top to top/bottom
    void Wrap(bool wrapAround = true) {
        bWrapAround = wrapAround;
    }
 
    // Add new Option
    void AddOption(std::string option = "") {
        vMenuOptions.push_back(option);
    }
 
    // Set Option x to "text"
    void SetOption(unsigned int number = 1, std::string option = "");
 
    // Allow Option Selection to return from bottom/top to top/bottom
    int LastSelection() {
        return iLastSelection;
    }
 
    // Allow Option Selection to return from bottom/top to top/bottom
    int Size() {
        return vMenuOptions.size();
    }
 
    // Display menu with an option already highlighted
    int Play(int startPosition = 1);
private:
    // Define a string to hold the left selector
    std::string strMenuTitle;
    // Define a string to hold the left selector
    std::string strLeftSelector;
    // Define a string to hold the right selector
    std::string strRightSelector;
    // Define a position to hold the vertical position
    position posVPosition;
    // Define a position to hold the horizontal position
    position posHPosition;
    // Define a vactor for storing the option strings
    std::vector<std::string> vMenuOptions;
    // Define an int to hold the last selected option
    int iLastSelection;
    // Define wrap around option
    bool bWrapAround;
};


There, I declare my Menu class and have some of the methods already defined (view the link to see the entire code). The reason I made a class for this is easy portability. Here is an example of how to use the menu:
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
42
43
44
45
// Declare MenuExample
    Menu MenuExample;
    MenuExample.SetSelectors(">");
    MenuExample.AddOption("Exit");
    MenuExample.AddOption("Give Me a Title");
 
    // Handle Menu
    do {
        switch(MenuExample.Play(MenuExample.Size())) {
            // Exit
            case 1:
                break;
            // Give Me a Title
            case 2:
                if(MenuExample.Size() == 2) {
                    MenuExample.SetTitle("Here Is My Title");
                    MenuExample.AddOption("Change My Selectors");
                }
                break;
            // Change My Selectors
            case 3:
                if(MenuExample.Size() == 3) {
                    MenuExample.SetSelectors("-> ", " <-");
                    MenuExample.AddOption("Move Me To Yhe Bottom Right");
                }
                break;
            // Move Me To The Bottom Right
            // Cemterize Me!
            case 4:
                if(MenuExample.Size() == 5) {
                    MenuExample.SetLocation(CENTER, CENTER);
                    MenuExample.AddOption("_Volatile Pulse Productions_");
                }
                if(MenuExample.Size() == 4) {
                    MenuExample.SetLocation(BOTTOM, RIGHT);
                    MenuExample.AddOption("Change My Previous Option");
                }
                break;
            // Change My Previous Option
            case 5:
                if(MenuExample.Size() == 5)
                    MenuExample.SetOption(4, "Centerize Me!");
                break;
        }
    } while (MenuExample.LastSelection() != 1);


As you can see, even though it took me quite some time to write my header file, it's already set up to meet almost all of my needs. I can simply keep reusing it over and over again. I also have a 2nd version that allows for the menu to control itself.
Topic archived. No new replies allowed.