How to initialize variable globally? accessible globally..

Asked By 0 points N/A Posted on -
qa-featured

How to initialize variable globally? I want a variable to be accessible globally and its value should already be initialized. Is it possible?

if yes.. Then how can we do that?

SHARE
Best Answer by michal joran
Answered By 0 points N/A #116037

How to initialize variable globally? accessible globally..

qa-featured

 

Variable which are accessible throughout the program are called global variable.
 
If you want the variable to be accessible globally then you should declare those in the 'main( )' function. Do remember this variable will be declared and initialized once. But if a constructor of the class is made then it would be initialized whenever the instance of the class is created.
 
Constructor name are the same as that of the class name. If you want to delete the variable in the end of the program execution then you may define destructor. Which of the same name as that of the constructor with a '~' (tilt sign) in the start.
Best Answer
Best Answer
Answered By 10 points N/A #116038

How to initialize variable globally? accessible globally..

qa-featured

You can define a variable as a global variable that will be available in whole program and you can access it any time and anywhere in the program. You need to define it on the start of program and then you can use it over and over in the program but keep in mind that it perform complication in programs and is not used due to these reasons you need to use it carefully and you then will be able to use the value as you need.

As this is available in whole program so it will be more demanding for care as if you have used it on some other place then your program may not work correctly and there can be errors in your code. So the best thing to do is to use a variable within a function and it is better for code. Although you can define it globally as per your require.

You can just simply define it and then you will be able to use it globally.

Michel joran.
 

Login/Register to Answer

Related Questions