Write a Cpp code to access global and local variables by creating objects and classes?


Warning: Trying to access array offset on false in /home3/indiakep/public_html/wp-content/plugins/dw-question-answer/inc/Template.php on line 8
All QuestionsCategory: Cpp LanguageWrite a Cpp code to access global and local variables by creating objects and classes?
Chetan Shidling Staff asked 6 years ago

info

1 Answers
Chetan Shidling Staff answered 6 years ago

Code:

#include <iostream>
using namespace std;
int i=5;//Global variable
class ABC
{
public:
void show()
{
int i=20;//local block declaration
cout<<i;
cout<<“\nGlobal variable:”<<::i;
}
};
int main()
{
ABC obj1;
obj1.show();
return 0;
}

Output:

20
Global variable:5