473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

variable in a class member

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
8 1402
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
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
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
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
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
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
Thanks 4 all the help guys !

Greetings =)

Dec 18 '05 #8
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gord | last post by:
Hello, I think that what I'm trying to do is impossible, but before I give up I thought I'd try and pick a few more knowledgeable brains than my own. I have any array of user defined type...
3
by: Defused | last post by:
> i have a file named CDSEncoder.cpp, and i wanted to create a member variable > of this in KNNDlg.cpp...so i wrote the following under my KNNDlg.h file > > protected: > CDSEncoder m_encoder; >...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
3
by: Tomás | last post by:
class Monkey { int cow() { static int k = 4; ++k; return k; }
5
by: Jeff | last post by:
Hey Below is a C# program I've made.... it's an app where I experiment with variable scope. In this code you see two variables named i (one is a local variable and the other is a member of the...
7
by: The Cool Giraffe | last post by:
Usually, when i declare a method and a variable in the header file i go as follows. public: int number; void doSome (); Then, in the CPP-file i will define it as follows. int number;
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
13
by: yanlinlin82 | last post by:
I'd like to write all code of a class in only header file. But when there is any static member varia ble, I have to define it in cpp file, not the header file. Otherwise, the static member variable...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.