+ 1
how can I use a particular variable name multiple times in a single program
6 Answers
+ 5
just write its name (without its type) when you need it...for example if i want to use the variable x=12 for many purposes
i can write (in main):
int x=12;
int y=x*x;
x=x+1;
cout<<3*x+y;
------------------------
output: 183
+ 3
ok...well you can't do that directly but here is something may help: you can use the keyword "namespace" to sort functions and variables in "namespaces"..and here,you can put more than two variables with the same name in different namespaces and then call the vatiables by the operator "::"..for example:
#include <iostream>
namespace tara{
int x=1;
}
namespace abdo{
int x=2;
}
int main() {
std::cout<<tara::x<<endl;
std::cout<<abdo::x;
return 0;
}
--------------------------------------------------
outputs:1
2
+ 1
int x, y, z;
0
yes abdo17 is right about it
0
My question was that can I store x= 1 & x =2 in the same program simultaneously
0
A possible answer would be to use instance variables for objects you will learn about them in the classes section.