- #1
sir_manning
- 66
- 0
Good evening
I'm in the midst of reviewing for my exam, I've come across a piece of code I don't understand. It is as follows:
//////////////////////////
#include <iostream>
using namespace std;
class number{
private:
int value;
public:
number(int v){value = v;}
void operator + (const number & num){
value = value + num.value; //<-what is going on here?
}
void print(){cout<<"\n"<<value<<"\n";}
};
int main(void){
number five(5);
number six(6);
five+six;
five.print();
return 0;
}
////////////////////////
This code compiles and gives an output of 11. However, it seems to me that there is a privacy violation on the line indicated. How is it that the operator function can access a private member ("value") of another number class ("num")?
Thanks.
I'm in the midst of reviewing for my exam, I've come across a piece of code I don't understand. It is as follows:
//////////////////////////
#include <iostream>
using namespace std;
class number{
private:
int value;
public:
number(int v){value = v;}
void operator + (const number & num){
value = value + num.value; //<-what is going on here?
}
void print(){cout<<"\n"<<value<<"\n";}
};
int main(void){
number five(5);
number six(6);
five+six;
five.print();
return 0;
}
////////////////////////
This code compiles and gives an output of 11. However, it seems to me that there is a privacy violation on the line indicated. How is it that the operator function can access a private member ("value") of another number class ("num")?
Thanks.