logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Guest
#1 Posted : Wednesday, October 13, 2010 5:48:25 AM(UTC)
Quote
Guest
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 ?
Sponsor

Soan
#2 Posted : Wednesday, October 13, 2010 5:49:04 AM(UTC)
Quote
Soan
Rank: Administration

Reputation:

Joined: 1/9/2010(UTC)
Posts: 449
Man
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.
Quick Reply Show Quick Reply
Users browsing this topic
Guest
New Topic Post Reply
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.