0
Can some one tell me that what is normal variable and instance variable?
c++
2 Respostas
+ 1
all scalar variables declared using primitive datatype are called normal variables
eg
int a, b, c;
Instance variable represent instantiation of a C++ class (predefined like string or user defined like person).
ex.
class point {
int x;
int y;
public:
point (){}
....
};
int main (){
int counter; // normal variable
point p; // instance variable
}
0
@Devender Mahajan No, instance variable is a variable defined in a class. When an object of a class is instantiated, class variables become instance variables. Instance variable is different to static variable which is also defined in a class with static keyword. An example would be helpful:
class A
{
int x;
string s;
static int y;
public:
void func();
static sfunc();
};
x and s are instance variables. y is a static variable. Likewise, func() is instance method and sfunc() is static method.
What's the difference? You don't have to create (instantiate) an object to call a static method, just call it like this: A::sfunc(). Static method can only use static variables but instance method can use both instance and static variables.
So what is normal variables? You guess it: variables other than class variables (static and instance).