Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable in a class member

Reply
Thread Tools

variable in a class member

 
 
vord.fok@gmail.com
Guest
Posts: n/a
 
      12-18-2005
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

greets

 
Reply With Quote
 
 
 
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      12-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>
> greets
>

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

JB
 
Reply With Quote
 
 
 
 
vord.fok@gmail.com
Guest
Posts: n/a
 
      12-18-2005
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 ?

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-18-2005
(E-Mail Removed) 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(;
change(100);

 
Reply With Quote
 
vord.fok@gmail.com
Guest
Posts: n/a
 
      12-18-2005
void main ()
{int a=0,b=0;
change(a);
change(a);
change(b);
change(b);
change(10);
change(-4);
change(;
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

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-18-2005
(E-Mail Removed) 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(;
> 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.

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-18-2005
On 18 Dec 2005 02:19:48 -0800, "(E-Mail Removed)"
<(E-Mail Removed)> 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
>
>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:stream (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
(E-Mail Removed)
 
Reply With Quote
 
vord.fok@gmail.com
Guest
Posts: n/a
 
      12-18-2005
Thanks 4 all the help guys !

Greetings =)

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-18-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments