In your code above there are 2 functions. 1 to set the value and the other o return the value.
>Couldnt you just make one function in class Example that returns the value of value?
Doesn't the value need to be set before being returned?
About private members;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class MyClass
{
public:
void manipulateData()
{
data = data + 1;
}
int data;
};
int main()
{
MyClass obj;
obj.data = obj.data - 5;
return 0;
}
| |
obj
has all access (root) access to data and can do all sorts of manipulations on it.
We don't want the kind operation done on data by
obj
(data = data - 5)
The only manipulation we want on data is provided by
manipulateData
which does an increment by 1 on the data.
The only way we can enforce this is to make data
private
by which
obj
will only access data via
manipulateData
which follows the conventions we have imposed on data manipulation.
In OOP, A class should have
private
data which is maniulated by functions, methods or procedures.