2010-09-27, 09:22 AM
Could someone help me out with "private" in object programming? I just don't get the use of it. I understand declaring things in private is supposed to make it "private", but I just can't think of a good way to use it or how it's actually "private". This is one of the examples my teacher gave me. He doesn't speak english very well which is why I had to make this thread and ask for SP's help.
Now what I don't understand is how idNum (declared in the object/class) is private. When I run the program, it straight up tells me what the idnum is of aFreshman and aSophomore, so how is that private?
Code:
#include<iostream>
using namespace std;
// declaration section:
class Student
{
private:
int idNum;
public:
static double athleticFee;
void setIdNum(int);
void displayStudentData();
};
// implementation section:
double Student::athleticFee = 45.25;
void Student::displayStudentData()
{
cout<<"Student #"<<idNum<<"'s athletic fee is $"
<<athleticFee<<endl;
}
void Student::setIdNum(int num)
{
idNum = num;
}
int main()
{
Student aFreshman, aSophomore;
aFreshman.setIdNum(1234);
aSophomore.setIdNum(2345);
aFreshman.displayStudentData();
aSophomore.displayStudentData();
cout<<"The standard student athletic fee is $"<<
Student::athleticFee<<endl;
}Now what I don't understand is how idNum (declared in the object/class) is private. When I run the program, it straight up tells me what the idnum is of aFreshman and aSophomore, so how is that private?

