// you’re reading...

C++ Glossary

Member Access Control: Public or Private?

You can declare class members

  • Whether they are data items or member functions
    • Either in the public or the private section of a class

 


But because one of the main precepts of OOP is to hide the data

  • Data items normally go into the private section

 


The member functions that constitute the class interface

  • Go into the public section
    • Otherwise, you can’t call those functions from a program



You can also put member functions

  • In the private section
    • You can’t call such functions directly from a program, but the public methods can use them
      • Typically, you use private member functions to handle implementation details that don’t form part of the public interface



You don’t have to use the keyword private in class declarations

  • Because that is the default access control for class objects (A-1):
    • However, I will explicitly use the private label in order to emphasize the concept of data hiding

 


Example of (A-1)

class World
{
	float mass; // private by default
	char name[20]; // private by default
public:
	void tellall(void);
	...
};




Classes and Structures



Class descriptions look much like structure declarations

  • With the addition of member functions
    • And the public and private visibility labels
      • In fact, C++ extends to structures the same features classes have



The only difference is that

  • The default access type for a structure is public
    • Whereas the default type for a class is private



C++ programmers commonly use

  • Classes to implement class descriptions while restricting structures to representing pure data objects
    • Or, occasionally, classes with no private components


Discussion

No comments for “Member Access Control: Public or Private?”

Post a comment