Hello..
Can anybody explain that how member functions of a class has direct access to the data members of the class vidout any need to declare them locally in the function body.
As in main() we need to declare local variables inside the body of the function.
as given below:-
main()
{
int a;
void display()
{
cout<<"enter value of a";
cin>>a;
cout<<"value of a is:"<<a;
}
}
Above program shows syntax error: variable not declared
But in..
class ABC
{
private:
int a;
public:
void display()
{
cout<<"enter value of a";
cin>>a;
cout<<"value of a is:"<<a;
}
};
main()
{
ABC abc;
abc.display();
getch();
}
this program works well.
Please Explain why so...
Can anybody explain that how member functions of a class has direct access to the data members of the class vidout any need to declare them locally in the function body.
As in main() we need to declare local variables inside the body of the function.
as given below:-
main()
{
int a;
void display()
{
cout<<"enter value of a";
cin>>a;
cout<<"value of a is:"<<a;
}
}
Above program shows syntax error: variable not declared
But in..
class ABC
{
private:
int a;
public:
void display()
{
cout<<"enter value of a";
cin>>a;
cout<<"value of a is:"<<a;
}
};
main()
{
ABC abc;
abc.display();
getch();
}
this program works well.
Please Explain why so...
Comment