In the C++ language generally, we create an object of the class in a main() function or member function but it is also possible to create an object globally.
Such objects are called global objects.
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 15 16 17 | #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 |