AM22 Tech
»
Technical Discussions
»
C/C++ Discussions
»
C++ question about public data member?
 Rank: Guest
Joined: 12/12/2009(UTC) Posts: 540
|
Lets say that I have the following class: class Car: { private: bool RaceCarStatus; public: Car() { RaceCarStatus = false; } ~Car() { RaceCarStatus = false; } void setRaceCarStatus(bool y) { RaceCarStatus=y; } bool getRaceCarStatus() { return RaceCarStatus; } };
// the main looks like this: int main() { Car y; cout << "Initial value for y: " << endl; cout<< " Race Car Status= "<< y.getRaceCarStatus()<< endl; y.setRaceCarStatus(true); cout << "Modified value for y: " << endl; cout<<" Race Car Status= "<< y.getRaceCarStatus() <<endl; return 0; } In class Car, we define data member RaceCarStatus as public. Under this circumstance, are setRaceCarStatus() and getRaceCarStatus() necessary ? Why ?
|
|
|
|
|
|
|
|
|
|
 Rank: Administration
Joined: 1/9/2010(UTC) Posts: 449  Location: India Was thanked: 1 time(s) in 1 post(s)
|
If you make RaceCarStatus as public then you can directly access those variables using these statements:
y.RaceCarStatus = true OR bool status = y.RaceCarStatus
Making a variable public gives its access to any code. Making it private will make it private to the class and hence outsiders cannot access it directly.
|
|
|
|
|
|
AM22 Tech
»
Technical Discussions
»
C/C++ Discussions
»
C++ question about public data member?
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.