Ok, I've searched for the answer to this, and either I'm not asking right, or no one has asked yet. I understand the concept of keeping member variables of a class private. But, would there be a time when it is okay, like below:
preface: I have four classes: studentSchedule(shown), studentType, taskType, courseType. All have member variables that are private, similar to below. bool, int, char, etc..
In my main I declare studentSchedule organizer;
The reason I want to know if public is okay for class type member variable is now I have access to each array variable, AND all of its functions without having to duplicate functions for each and every class.
This duplication seems really inefficient to me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class studentSchedule:
{
private:
char fileName[MAX_CSTR_LEN];
bool found[MAX_TASKS];
int numFound;
int numCourses;
int numTasks;
//studentType student;
//taskType tasks[MAX_TASKS];
//courseType courses[MAX_COURSES];
protected:
//studentType student;
//taskType tasks[MAX_TASKS];
//courseType courses[MAX_COURSES];
public:
//studentType student;
//taskType tasks[MAX_TASKS];
//courseType courses[MAX_COURSES];
// there are comments because I am currently experimenting.
|
student type
1 2 3 4 5 6 7 8 9 10 11
|
class studentType
{
//friend class studentSchedule;
private:
char firstName[MAX_NAME_LEN];
char lastName[MAX_NAME_LEN];
char gender;
char gradeLevel[MAX_NAME_LEN];
//Keep in mind each of these variables has a get, set, show function.
|
So why rewrite all of these functions in studentSchedule class? When even if i declare student as public in studentSchedule, you still can not directly access its members from main.
Any insight on the why they should still be private is helpful.
Thank you in advance.
edit. example:
organizer.searchById(organizer.courses[courseIndex].getCourseId());
is perfectly okay if the variable courses is public.
but much more tedious if it's private.
Call me the guy looking for the easy way out, but it seems less messy.