Post: need some help on this c++ code
05-04-2017, 06:20 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Okay i figured out the problem..what i was trying to do is add variables through classes and objects for anyone with the same problem use this code for an example

    #include <iostream>
using namespace std;
class calculator{
public:
int setadd(int x,int y){
answer=x+y;
}
int getadd(){
return answer;
}
private:
int answer;
};
int main(){
calculator bo;
bo.setadd(30,7);
cout<<bo.getadd();
return 0;
}
Last edited by mudville209 ; 05-06-2017 at 05:22 PM.
05-04-2017, 06:22 AM #2
all those ** are supposed to tab spaces dont mind them act as if they dont exist
05-04-2017, 04:52 PM #3
You can use this button You must login or register to view this content. in the toolbar ( go to the advanced editor ) if you need to insert code.

The problem in your code is that you redefine the "answer" variable in the scope of your setadd function; it will thus mask the member variable with the same name:

    
int setadd(int x,int y)
{
[COLOR="#FF0000"]int[/COLOR] answer; [COLOR="#FF0000"]// You want to use the member variable so you don't have to redefine it. This line is useless.[/COLOR]
answer=x+y;
}

int getadd()
{
return answer;
}

private:
int answer;
};


Your compiler should warn you about it so check your logs.
Also it is good practice to name your member variable with a prefix, like "m_answer" in your case, to avoid such confusions.
05-05-2017, 01:58 AM #4
the way u wrote the code is exactly how i wrote mine it doesn't work...
Last edited by mudville209 ; 05-05-2017 at 03:41 AM.
05-05-2017, 06:17 AM #5
i dont know if someone edited my post to make the code right or it was originally like that i cant remember i dont even know if thats possible(im new to ngu) but i tried using ur code and it didnt work either...the program runs and gives no errors but it just wont add the two variables
05-05-2017, 08:04 AM #6
It would help if you posted the full code. I don't think you get it thoug, don't copy and paste my code; remove the line that I commented, as stated in the comment.
05-05-2017, 11:34 PM #7
what i posted is the full code i just wanna know how u would add variables through classes and objects
05-06-2017, 12:50 AM #8
You edited your post to include the full code, don't say it was there in the first place when it was not the fucking case. Way to be a cunt. Your last question makes no fucking sense. Idk why I'm losing my time with retards like you. I hate liars
05-06-2017, 01:51 PM #9
i didnt edit it it WAS THIS GUY Last edited by Kronos; Yesterday at 02:12 AM.
05-06-2017, 01:51 PM #10
im really not trying to be a jerk its just that im new to this site and c++ and im getting confused

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo