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 4 months ago

info

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

Your Answer