Section 14f

Scope Considerations with Classes


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Identifying Available Components

Scope Considerations

As you have read previously, most of the class data is private but many of the class methods are public. This supports the protection and safety that Object Oriented Programming (OOP) provides. This topic will provide a brief expansion on what you have already learned about the public vs private situation.

Public vs Private Scope

As mentioned in the previous topic, two of the more critical characteristics of OOP are encapsulation and data hiding. Just like the table saw you purchased to build your house, there are parts within it such as the table, the motor and saw blade, the table legs and feet, and so on. These are packaged or encapsulated into a thing you call a table saw which you can use, thus making the table saw available to you and they are said to be public. Now remembering that OOP is all about creating tools, take a deeper look at this particular one.

To extend on the metaphor, there are parts inside the motor that you should not manipulate. As an example, there are brushes and bearings inside the motor that could be harmed if untrained individuals opened them up. These parts are therefore not accessible by the table saw user (or user/programmer in the case of OOP) and would be considered private. This is another example of how object oriented programming is comparable to real world circumstances.

To continue extending the metaphor, there are times that the brushes need to be replaced. So in some cases, a person authorized to work inside the table saw has access to the motor brushes and she can replace them. She is said to be an accessor or mutator in the table saw class system. So while you cannot touch the brushes yourself, you can call a public function named replaceBrushes and the appropriate component within the class can manage the private brushes for you.

Extending this to data should be pretty easy. If you wish to copy files on your computer, you should be able to call or click on a public process that will copy this given file to that given directory. On the other hand, there are some operations involved in copying that involve opening the file, getting a file handle, managing the bytes in bulk and making sure all the data lands in the right place on the hard drive, and then closing the file and cleaning things up; these are parts of the process very few people are capable of doing. Those operations must stay private.

In C++, every component of a class whether it is data or functions will be configured as public or private as appropriate for the class; this action will be taken by the locations in the header file under either the public keyword or the private keyword. As a note, if a programmer does not set this, the compiler commonly sets the state of the data or function to some default condition. In C++, if the scope is not set, the data and/or methods commonly default to public which means all the data or methods are accessible to the users/programmers. While C++ can handle this for you, it is better to set your own public and private groups in the header file to make sure your class and its objects do what they were meant to do.

While there will be many more examples as this reference continues, a quick example can be provided from the StudentClass where most of the methods are accessible to the users/programmers but the setNameString which is a utility specific to managing the name string data and would not be appropriate for a user/programmer who is using this class for managing students.

As mentioned, while this is a pretty brief treatment of scope, there will be more as you continue your programming journey. Stay Tuned.


Watch this video to learn more about scope management within classes.