Access specifier
Access-specifier is one of these three c++ keywords that are as follows:
1. Public
2. Private
3. Protected
Functions and data declared inside a class are private to in which class and may be accessed only through other members of the class by default. However, by using public access specifier, you permit functions or data to be accessible to other categories of your program. Once an access specifier has been used, that will remains in effect until either another access specifier is encountered or the end of the class declartion is reached. For switch back to private declarations, you could use the private access specifier. The protected access specifier is required just when inheritance is involved.
Commonly, a class specification has two categories:
A class declaration that describes the data parts in terms of data members and the public interface, in the terms of member functions
The class method definitions, that describes how certain class member functions are implemented
Approximately speaking, the class declaration gives a class overview, while the method definitions supply the details.
A class specifies the type and scope of their member. The keyword class denotes that the name, that follows (ClassName), is an abstract data type. The body of a class is enclosed inside the curly braces followed through a semicolon - the end of a class specification. The body of a class holds declaration of variables and functions, collectively known as members. The variables declared inside a class grouped under two sections, private and public, that define the visibility of members.
The private members are accessible only to their own class's members. Instead, public members are not only accessible to their own members, but also from outside the class. The members in the starting of class without any access specifier are private by default. Thus, the first use of the keyword private in a class is optional. A class, that is totally private, is hidden from the external world and will not serve any useful purpose.
The given declaration describes the specification of a class known as student having roll_no and name as its data members:
Class student
{
int roll_no; char name[20];
public :
void setdata(int roll_no_in,char *name_in)
{
roll_no=roll_no_in;
strcpy(name,name_in);
}
void outdata()
{
cout << "Roll No = " << roll_no<<endl;
cout << "Name = " << name << endl;
}
};
A class should be provided some meaningful name, (for example, student) reflecting the information it contains. The class name student becomes a new data type identifier that satisfies the properties of abstraction; it could be used to define instances of class data type. The class student holds two data members and two member functions. A data members are private by default although both the member functions are particular as public. A member function setdata() could be used to assign values to the data members roll_no and name. A member function outdata() can be used for showing the value of data members. The data members of a class student cannot be accessed by any other function except member functions of the student class. It is a common practice to declare data member as private and member function as public.