CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

info

1 Answers
CS Electrical And Electronics Staff answered 4 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