Problems with class files

I have viewed a lot of tutorials and did a lot of research. I was told that the use of classes can make your code reusable.

I am just trying to see if I can get an if statement to work in a .cpp to be used in main

I researched polymorphism, templates, and got fuzz brain.


I am caught up on it and my coded is below.

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
79
80
81
82
// header file
#ifndef FUNCTION1_H
#define FUNCTION1_H

using namespace std; r

class Function1
{

    string test1, test2, test3;

    public:
    Function1();    

    void set_values2 (string);

    string compare2() {return (test1, test2, test3);}



};

#endif // FUNCTION1_H


// .cpp source file

#include<iostream>
#include<conio.h>
#include<string>
#include"Function1.h"


using namespace std;

Function1::Function1()
{

 //ctor
}
void Function1::set_values2 (string a, string b, string c)
{

            test1 = a;
            test2 = b;
            test3 = c;

    //if((test1 == ans1) && (test2 == ans2) && (test3 == ans3))
    if(1 ==  1)  // This seems to be where the code went all wrong
    {
            cout << "your are correct!!" << endl;
    }
    else
    {
            cout << "you are incorrect." << endl;
    }
}


// main.cpp

#include<iostream>
#include<conio.h>
#include<string>
#include"Function1.h"

using namespace std;

int main()
{



        comp.set_values2 ("FOO", "BAR", "AR");


        comp.compare2();

    getch(); 
    return 0;
}


In my research I am finding a lot of material for int's and not so much for strings. I am very interested in working with multiple files.

Any ideas on where I am going wrong...???
Last edited on
closed account (Dy7SLyTq)
the () are not for returning. they are for function arguements and conditions. and what do you mean getch() links to conio? you dont link to conio, you access it with the #include macro. getch takes key board input
I redid my request because I think I explained the issue wrong. The above code is what I did do using an example for an int instead of a string.

I think they worked quite differently. The if statement I am using I am finding myself repeating over and over in my main function and I am thinking there has to be a way to reuse the if function in a separate class. The if function also involves a vector.

I just want to see if I can get the if function in a seperate class and working something like a template.

The getch() is not even a part of the problem....sorry about that. Is used to keep the screen opened.
Last edited on
closed account (Dy7SLyTq)
yes i know. ok you need to learn proper terminology because i didnt understand what you said. and once again you cant return multiple things with the same statement! if is not a function
closed account (Dy7SLyTq)
and its a header file. class files are compiled java
I am grateful for any suggestions. Excuse if I use the wrong terminology as I am teaching myself with no what you may call formal training.

I cleaned up some of my notes that may be clouding my issue, and I have reworked the code some with much searching the Inet I figured out some things. But am still trying to get the if statement working in it's class. Then will have to figure out how to get it working with a vector that I am hoping I can get in it's own class too.

Here is where I am at....

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

// Funtion1.h

#ifndef FUNCTION1_H
#define FUNCTION1_H

using namespace std; 

class Function1
{
    string test1, test2;

    public:
    Function1();    
    void set_values2 (string);

    string compare2() {return (test1);}
};

#endif // FUNCTION1_H

// Function1.cpp

#include<iostream>
#include<conio.h>
#include<string>
#include"Function1.h"

using namespace std;

Function1::Function1()
{

 //ctor
}

void Function1::set_values2 (string input)
{
            test1 = input;

    //if((ui1 == ans1) && (ui2 == ans2) && (ui3 == ans3))
    if(input == input )
    {
            cout << "your are correct!!" << endl;
    }
    else
    {
            cout << "you are incorrect." << endl;
    }
}

// main
#include<iostream>
#include<conio.h>
#include<string>
#include"Function1.h"

using namespace std;

int main()
{

        Function1 comp;

        /// Below for arguments of if function
        string input;
        cin >> input;  
        comp.set_values2 (input); 

    getch();    
    return 0;
}

Last edited on
closed account (Dy7SLyTq)
well whats up with the line (42) if (input == input)? it will always be true and it is pointless. If is not a function and for the last time, return(itemOne, itemTwo, itemThree) is illegal!!! you can do that in python and ruby(i think; its been a long time) but not c++
line 42 is just to test to see if the if function is doing what I want it to do

Why is return(itemOne, itemTwo, itemThree) not right. To say it is illegal is not telling me why.

Can you give me suggestions on how to made the code work? Or if there is a better way to do it.

closed account (Dy7SLyTq)
no line 42 is pointless. its saying does the current value of input equal the current value of input. its illegal because its not a feature. theres no other explanation. you can only return one thing. this is the third or fourth time ive said this. please dont make me say it again
To continually go over things that I have no problem with is not helping me with the overall solution which is classes and how to get functions to work within them. Line 42 was there to make sure that I was getting to the function and that it worked.

But, thank you for stressing the point on return.

It did urge me to look for the answer as to why you said that, and I found it elsewhere because you did not give an answer other than from the view point of "because I said you can't".

I figured out the solution but like I said thanks for the help you did give kid.

Much luck to you....

btw...

I was making it harder than it should have been. I think my problem was that I was not using getter and setters the way I was used to. I was following an example that I found on the internet and it didn't work for me So I went back to the way I am used to doing getters and setters and found that I could do functions in other files just like in main and call them from main and they run fine.

So fair warning when learning on your own and following examples of tutorials. Make sure you have good code, or you can waste days with something that didn't work when you found it.

I love coding when you figure out how to do something and actually make something useful from it.
Last edited on
closed account (3qX21hU5)
I see a few things that need some help here I'll go over them quickly


1) string compare2() {return (test1);} This seem to be a getter function for your class. With getters and setters always label them so people know what they are. For example since this is a getter you should have something like this

string getString() {return test1;} That way when other people read your code it is much easier for them to understand. Also you don't seem to be using this anywhere, but it could be for something you plan to do in the future.


2) if(input == input )

Like DTS was explaining earlier this code will always be true no matter what. From your comments it seems like you mean to change it later, but just in case I want to explain what it does a bit further.

This statement is essentially the same as saying if (1 == 1). So whenever you run that if statement it will always be true since 1 is always equal to 1.


3) This doesn't seem to be in your code anymore but figured I would go over it.

string compare2() {return (test1, test2, test3);} The reason why this is wrong is because functions can only ever return 1 thing at a time by the return statement.

Now if you do want to alter more then 1 variable you can use multiple different techniques to do so. One would be to use references (&)

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
#include <iostream>

using namespace std;
   
// Change multiple variables without returning
void changeAllToOne (int &a, int &b, int &c);

int main()
{
    int one = 10;
    int two = 20;
    int three = 30;
    
    // Changes all three variables that I pass to 1 without returning anything
    changeAllToOne(one, two, three);
    
    cout << one << two << three << endl;
    
    return 0;
}

// Notice the & operator.
void changeAllToOne(int &a, int &b, int &c)
{
    a = 1;
    b = 1;
    c = 1;
}


When you put the reference operator in a parameter you are telling the function to alter whatever variable you pass in directly instead of creating a temporary copy of the variable that gets destroyed after the function is done. You can find more about this in the tutorials on here.

But what it seems like you were trying to do with your triple return statement was to use it as a getter for all 3 private variables in the class. This is illegal because you can only return 1 thing at a time, so you will have to make a getter for each variable in your class.

Hope this helps a bit and good luck with your program, glad to see you like the vectors also :)
@ Zereo

Thank you that explained a lot.

I was kind of in the grey with the code sample that I was trying. It wasn't good code. I did find out some things not to do though.

I went back to my old style of getters and setters that worked for me.
------------------------
I do have a question though from number 1.

 
string getString() {return test1;}


The reason I renamed it to compare2 is because I had more than one getter. I would have normally did it like:

 
string getCompare2(){return(private variable here);}


From what I am seeing from what you are saying that it should always be getString so should have been more like.

 
string getString(){return(private variable 1 here);}




but what if you have more than one such as in.

1
2
3
4
5
string getString(){return(private variable 2 here);}
string getString(){return(private variable 3 here);}
string getString(){return(private variable 4  here);}
string getString(){return(private variable 5  here);}
string getString(){return(private variable 6 here);}


Would having multiple getString's confuse the copiler?

Does the same apply for setters too?

such as

1
2
3
4
5
void setString(string ?){code here}
void setString(string ?){code here}
void setString(string ?){code here}
void setString(string ?){code here}
void setString(string ?){code here}


-------------------
I knew that line 42 would equal itself. The point to that was just to see if it was even getting to the if statement and testing the condition. Hard to explain but I am trying to get into the OOP style of C++. Getting functions and variables to work between files for the reusability of code(or that is the way I understand it.)

I've copied your example for & references in my notebook for future reference. That is going to be very useful down the line.

I do like the idea of vectors. I am studying them now. They seem to have more functionality then the arrays I was using and so your push in that direction was awesome.

Last edited on
closed account (3qX21hU5)
So long as you have get or set in the name it should be fine they don't all need to be the same
Topic archived. No new replies allowed.