Hello!
Please, I try to "invent" a function mmm, which would try to take private data, and, as it would not be allowed, I woudl "have" to make these data public to make mmm success it. Where did I do the mistake? Many thanks!!!
class First{
int a;
int b;
public:
First(int c,int d):a(c), b(d){};
int sss(){ return (a+b); }
};
int mmm(int x, int y) {
int z=x+y;
return z;
}
int main(){
First sum1(2,3);
First sum2(4,5);
cout<<sum1.sss()<<endl;
cout<<sum2.sss()<<endl;
int r;
r=mmm(First.a + First.b);
cout<<mmm()<<endl;
return 0;
}
Hello, I try to write szch a code, that data int a and int b would HAVE to be changed from default to public, meaning, such a code that would not work if a and b were default (private) and WOULD work if both were public.
You're correct, if you want to use mmm as a regular function those members will have to be public, however you can also implement mmm as a class method which would allow you to access those private members. Your other option is to make "getters" and "setters" for those variables, which would be public functions like: