Can some one tell me that what is normal variable and instance variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can some one tell me that what is normal variable and instance variable?

c++

17th Mar 2017, 3:54 PM
mayank
mayank - avatar
2 Answers
+ 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 }
17th Mar 2017, 4:32 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
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).
17th Mar 2017, 6:06 PM
hdo
hdo - avatar