By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

variable in a class member

P: n/a
How do you keep a value in a member function ?

i mean ...

void main ()
{
int a =0;

change(a);
change(a);
}

void change(int a)
{a++
cout << a ;
}

output should be 1 and 2;

i forgot how you keep a value in a function :p

greets

Dec 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
vo******@gmail.com wrote:
How do you keep a value in a member function ?

i mean ...

void main ()
{
int a =0;

change(a);
change(a);
}

void change(int a) void change(int &a) // pass parameter by reference {a++
cout << a ;
}

output should be 1 and 2;

i forgot how you keep a value in a function :p

greets

Based on what you require, see above for the solution.

JB
Dec 18 '05 #2

P: n/a
Thanks for that but you misunderstood me , i should have done this to
clearafy my statement

void main ()
{
int a=0,b=0;
change(a);
change(a);
change(b);
change(b);
}

void change (int c)

{c++
cout << c;
}

and print out 1 2 3 4 .

should i use a static int ?

Dec 18 '05 #3

P: n/a
vo******@gmail.com wrote:
Thanks for that but you misunderstood me , i should have done this to
clearafy my statement

void main () int main() {
int a=0,b=0;
change(a);
change(a);
change(b);
change(b);
}

void change (int c) // you are passing by value. What it means is that whatever changes you
make to c here are not reflected back in the variables passed by the
caller.

{c++ missing semicolon.
cout << c;
}

and print out 1 2 3 4 .


What you exactly want to do?
1) Print a larger number each time function is called, starting from 1?

2) Print c+1,c+2,c+3,c+4 where c is the value passed to change for its
_first_ execution?
3) Always print 1 then 2 then 3 and then 4?

What is the exact relation that you want between your output and the
value of c?
What output you expect if I do something like this in main :
change(10);
change(-4);
change(8);
change(100);

Dec 18 '05 #4

P: n/a
void main ()
{int a=0,b=0;
change(a);
change(a);
change(b);
change(b);
change(10);
change(-4);
change(8);
change(100);
}

void change (int c)
{int z = 0;
z++;
cout << z << endl;
}

output should be

1
2
3
4
5
6
7
8

thanks for the quick replys gus , i appreciate it allot :)

greets

Dec 18 '05 #5

P: n/a
vo******@gmail.com wrote:
void main ()
This should be int main()
{int a=0,b=0;
change(a);
change(a);
change(b);
change(b);
change(10);
change(-4);
change(8);
change(100);
}

void change (int c)
{int z = 0;
static int z = 0; // although 0 is not technically required.
z++;
Prefer preincrement over post-increment if the sole purpose is to
increment.
++z;
cout << z << endl;
}


As you guessed it, local static variables are used to remember the
value/state across multiple invocations.

Dec 18 '05 #6

P: n/a
On 18 Dec 2005 02:19:48 -0800, "vo******@gmail.com"
<vo******@gmail.com> wrote:
How do you keep a value in a member function ?

i mean ...

void main ()
{
int a =0;

change(a);
change(a);
}

void change(int a)
{a++
cout << a ;
}

output should be 1 and 2;

i forgot how you keep a value in a function :p

greets


If you want to change the variable's value, you must pass it by
reference as someone else has already shown you. However, there are
some other things worth pointing out:

1. "Member functions" are functions which are defined as part of a
class definition. You use no classes in your code except for
std::ostream (i.e. std::cout) which is defined in the STL library.
Your change() and main() are global, or stand-alone functions.
However, the same advice to pass the int by reference would equally
apply to a member function.
2. The main() function must return int, not void, in order to be a
proper C++ program. You can leave out a return statement, though, in
which case the compiler will generate code to return the value 0 from
your main() function back to the operating system shell or process
from which it was invoked.
3. You need a semicolon after "a++" inside the change() function.
4. If main() is defined before change(), you must provide a
forward-declaration of change() in order to be able to call it inside
of main().
5. In order to use cout, you must include the appropriate standard
header <iostream> (without an .h).
6. Also, you must provide access to the namespace std in order to use
cout, either by writing a using directive, a using declaration, or by
explicitly qualifying the name: std::cout.

Older compilers might let you get away with your present code WRT
items 2 and 6, but it isn't standards-conforming code and should be
corrected.

Good luck learning C++ ... it is an exciting adventure!

--
Bob Hairgrove
No**********@Home.com
Dec 18 '05 #7

P: n/a
Thanks 4 all the help guys !

Greetings =)

Dec 18 '05 #8

P: n/a
vo******@gmail.com wrote:
How do you keep a value in a member function ?

i mean ...

void main ()
[redacted]


Your program can print anything, since void main() is not Standard C++.

main() must return an int.

Dec 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.