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

All QuestionsCategory: Cpp LanguageWrite a Cpp code to access global and local variables by creating objects and classes?
Chetan Shidling Staff asked 5 years ago

info

1 Answers
Chetan Shidling Staff answered 5 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