In C++ objects can be classified as local object and global object.
Local object
A object defined inside a block is called local object.
A local object can be inside block or function or class
In above figure variable a and b are local variable inside complex class.
Inside main function Complex c=Complex(1,2);
is defined here c is a complex object.
Example Local object declaration and use in main().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; class Complex { private: int a; int b; public: Complex(int a,int b) { this -> a = a; this-> b = b; } void show() { cout << "Value is " << a<<"+"<<b<<"i"; } }; int main() { Complex c=Complex(1,2); c.show(); } |
c is defined as local object since it is inside main function.
Example Local object example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> #include <cstring> using namespace std; class Student { private: char name[30]; long rollNo; char branch[30]; public: Student(const char * name, long rollNo, const char * branch) // constructor { strcpy(this -> name, name); this -> rollNo = rollNo; strcpy(this -> branch, branch); } void show() { cout << "Student Details are" << endl; cout << "Name " << name << endl; cout << "Roll No " << rollNo << endl; cout << "Branch " << branch << endl; } }; int main() { Student s=Student("Ram", 1, "CSE"); s.show(); } |
what is the lifetime of a local object
The local variable exists up to control is in block. When ever control goes out of block local variable destroyed.
Global Object
An object declared outside any class or method is known as global object.

C++Global object Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; class Complex { private: int a; int b; public: Complex(int a,int b) { this -> a = a; this-> b = b; } void show() { cout << "Value is " << a<<"+"<<b<<"i"; } }; Complex c=Complex(1,2); int main() { c.show(); } |
Here object c is outside all class and method so it is a global object.
Example: Write a C++ program to show the difference between the local and global objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; class zzz { public: void show(int x) { cout << " x= " << x; } }; zzz a; // creating global object “a”. int main() { zzz O; // creating local object “O” a.show(10); // call using global object O.show(5); // call using local object } |
Output
1 | x=10 x=5 |
Local objects and global objects in C++ are used as per need. If the use of a variable is inside only a method or class then declare it local.
If a variable is required in various functions then make it global.
difference between global object and local object
Sr No | Global Objects | Local Objects |
1 | Global objects are declared outside of all classes and methods | Local Objects are declared inside any class, method, or block. |
2 | Can be accessed by any class or method in the program. | Can be accessed with in class, method or block where it is declared |