Class member variables made public.

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.
Last edited on
closed account (28poGNh0)
I am not sure of what you want , are you looking to access the private data of studentType?
No, accessing the private data for studentType is still done with the member functions of studentType. By declaring studentType student a public variable of studentSchedule, you don't have re write the member functions in studentSchedule to access the member functions of studentType.
Private members are useful because they can't be touched from outside the class scope. If you don't have a reason to keep other code from modifying those members I don't see a problem making them public.

I may have got your question wrong, but if you have to keep variables private can't you do
1
2
3
4
studentType& studentSchedule::GetStudent() {return student;}

// main
char gender = schedule.GetStudent().GetGender();

?
> organizer.searchById(organizer.courses[courseIndex].getCourseId());
> is perfectly okay if the variable courses is public.
> but much more tedious if it's private.
If `courses' were private you'll simply do organizer.search_course_by_id( course_index );
Still ¿what is that code supposed to do? (¿wouldn't simply return its parameter?)


> Keep in mind each of these variables has a get, set, show function
¿how many times would a student change his gender?
Having member variables private is a good idea, making them public or protected is not.

Then you need to have an interface, made up of public functions which allows one to manipulate the object data.

This doesn't mean that you have a public get, set, and show function for each variable. If that were the case, then all the variables might as well be public.

Instead make use of constructors to initialise data into an object with an initialiser list. Also make use of the fact that member functions have direct access to member variables.

This brings into play one aspect of encapsulation - everything to do with an object is in the object.

As an example : instead of using get functions to print a student details in main(), prefer to have a PrintThisStudent function say inside the Student Class. This function can be called from main(), it has direct access to the member variables, and avoids all the need for all the get functions.

I am helping someone else with a similar concept, read about it here :

http://www.cplusplus.com/forum/beginner/106633/


There is also a link to a huge discussion we had about getters & setters.

Hope all goes well.
Topic archived. No new replies allowed.