What is the difference between Instance and Static variable

Showing Answers 1 - 9 of 9 Answers

mujeeb3108

  • Nov 15th, 2011
 

Static variable: A value assigned to a static variable will be shared across all instances of the class.

Instance variable: A value assigned to an instance variable is local to that object and it will be different for each instance of that class.

  Was this answer useful?  Yes

mujeeb3108

  • Nov 15th, 2011
 

Static variable: A value assigned to a static variable will be shared across all instances of the class.
Instance variable: Each instance variable will hold different value that will not be share among instances,

On the other hand an instance variable may have different values across every different instance of the class.

Code
  1. class MyClass

  2. {

  3. int myInstance;

  4. static int myStatic=10;

  5.  

  6. public:

  7. putvalues(int my)

  8. {

  9. myInstance = my;

  10. }

  11. getvalues()

  12. {

  13. printf("myInstance %d",myInstance);

  14. printf("myStatic %d",myStatic);

  15. };

  16. main()

  17. {

  18. MyClass a,b;

  19. a.putvalues(100);

  20. b.putvalues(200);

  21. a.getvalues();

  22. b.getvalues();

  23.  

  24. }

  25.  

  26. output:

  27. a:

  28. myInstance 100

  29. myStatic      10

  30. b:

  31.  

  32. myInstance 200

  33. myStatic      10

  34.  

pavani

  • Feb 18th, 2012
 

Where I have to write clrscr() in a CPP programme

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions