Member Access Specifiers:
The commands that determine whether a member of a class can be accessed from outside the class or not are called member access specifiers.
Normally two types of member access specifiers arc used. These are:
- Private:
- Public:
Private Specifier:
The members of a class that can be accessed only from within the class are called private members. They cannot be accessed from outside that class.
Normally all data members are declared as private. The member functions can also be declared as private. The members that are made private can only he accessed from within the class.
All class members that conic after the specifier private: and up to the next member access specifier are declared as private and are accessible only from inside the class. The default access mode is private. Thus if no access specifier is given, the members are treated as Private.
Making a data to he accessed from within the class is called data hiding, i.e. the data declared as private is hidden from outside the class.
Public Specifier:
Public members of a class can be accessed both from inside and from outside the class. Normally, member functions are declared as public. The data members can also he declared as public.
Example:
The following example explains the use of member access specifiers in C++ classes.
class cdate
{
private:
int y, d, m;
public:
void gdate (void)
{
cout << “Year ?” ; cin >> y;
cout << “Month ?” ; cm >> m;
cout << “Day ?” ; cm >> d;
void pdate (void)
}
void pdate (void);
{
cout << d << “/“ << mc < ”/” << y;
}
In the above example the class has been defined. Its name is “cdate”. It has three data members y, d, and m of int type and has been declared as private. These data members can only be accessed from inside this class. The class also has two member functions. “gdate” and “pdate”. The “gdate” function gets the date and “pdate” function, prints the date in date format (dd/mm/yyyy) on the computer screen. Both the function members are declared as public.
comment closed