Reputation: 124
class A
{
int a;
double b;
public:
A(){a=20;B=78.438;}
void data()
{ int num1; num1=a;}
}
I have above CPP code. Can I access 'num1' variable using object of class A type?
I think this question is different than "How to access variables defined and declared in one function in another function?".
Because here I want to access variable , which is in the function, which is a member of the class A. And I want access through object of class A type.
Upvotes: 0
Views: 91
Reputation: 49986
Can I access 'num1' variable using object of class A type?
No, you cannot.num1
is declared inside a function data()
so you have no access to it through object of type A. You would have to move declaration of int num1;
to class body.:
class A
{
int a;
double b;
public:
int num1;
A(){a=20;B=78.438;}
void data()
{
num1=a;
}
}
now you can write:
A a;
a.num1 = 1;
Upvotes: 3