In this tutorial, you will understand how to use static object in c++ programming with example.
Static Object in C++
- It is optional
- Unlike local object, it has scope till the program lifetime
- If object is defined with static modifier, that is called as static class object
- Static objects are initialized using constructor like other normal objects
- It works in the same way for class objects too.
- It is allocated in the static storage area and have scope till end of program
- It has two types: They are
- Local static object
- Global static object.
Local Static Object
- It has local access & scope till end of the program
Global Static Object
- It has global access & scope till the end of the program.
LOCAL STATIC OBJECT
1. SOURCE CODE
#include<iostream>
using namespace std;
class Test
{
public:
void disp()
{
cout<<“Good Morning …\n”;
}
};
int main()
{
cout<<“———————————–\n”;
cout<<“\t Static Class Object\n”;
cout<<“———————————–\n”;
static Test obj; // local static object
obj.disp();
return 0;
}
2. OUTPUT