Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.
Example(s):1575, 1632, 1539 (Only one at a time)
Login
[lwa]
Q&A
#5433
Question:
How is global object different from a local object?
Answer:
Declaration: Global Objects are declared either at the end of a class or outside any method/function scope while local objects are declared within a method/function scope.
Purpose: Global objects can be used by all methods within the same program file, while the local objects can only be used within the method/function scope where they are declared.
Security: Global objects are less secure as they can be unintentionally/intentionally modified by other routines and thus incorrect values may be available for other routines. Local objects can be used within routines only so they can not be modified by external routines unless they are passed by reference deliberately.
Notes:
Example:
class A
{
int x;
}A1; //A1 is a global object defined along with class definition
A A2; A2 is a global object outside functions scope.
void func1()
{
A A3; //A3 is Local object to be used within func1()
A1.x; A2.x; //These are valid as they come from global objects
A3.x ; //This is valid as it is being used within func1()
}
void func2()
{
A1.x; A2.x; //These are valid as they come from global objects
A3.x; //This is invalid as it is being declared locally in func1() only.
}