2010-09-21, 01:40 PM
In this case, you could have the object Student, with 2 properties - sex and gpa.
Of course, you can add other variables if you wanted (age, height, etc.) but in this case those 2 are all we really know.
Then when you read the input file, for each line you create a new instance (like creating a new variable) and set its properties according to the data.
I'm less familiar with C++ but in Java, when you create an object, you can implement default "interfaces" which means you give the object a certain set of functions that behave in a pre-specified manner. Such as print() returning a string describing the object, or compareTo(object) telling you which has a larger value. Once you implemented these functions you can treat your new object just like the primitive types (int, char, double, etc.) in your code, sticking them in lists, adding/subtracting them, printing, etc.
Eg. if you had sex(char) in your Student class, returning true if sex==char and false if sex!=char, you could write if(student.sex('m')) malegpa += student.gpa(); else femalegpa += student.gpa(); Then again, that's not really clear code - you'd more likely want to write getSex() which returns a char (m or f) and have if(student.getSex() == 'm').
Of course, you can add other variables if you wanted (age, height, etc.) but in this case those 2 are all we really know.
Then when you read the input file, for each line you create a new instance (like creating a new variable) and set its properties according to the data.
I'm less familiar with C++ but in Java, when you create an object, you can implement default "interfaces" which means you give the object a certain set of functions that behave in a pre-specified manner. Such as print() returning a string describing the object, or compareTo(object) telling you which has a larger value. Once you implemented these functions you can treat your new object just like the primitive types (int, char, double, etc.) in your code, sticking them in lists, adding/subtracting them, printing, etc.
Eg. if you had sex(char) in your Student class, returning true if sex==char and false if sex!=char, you could write if(student.sex('m')) malegpa += student.gpa(); else femalegpa += student.gpa(); Then again, that's not really clear code - you'd more likely want to write getSex() which returns a char (m or f) and have if(student.getSex() == 'm').

