A class is a user defined data type which binds data and its associated functions together. It allows the data and functions to be hidden, if necessary from external use.
Generally, a class specification has two parts.
i) Class declaration: it describes the type & scope of its members.
ii) Class function definitions: It describes how the class functions are implemented.
Class declaration.
The general form of a class is
class class-name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
} ;
1) The class keyword specifies that what follows is an abstract data of type class name. The body of a class is enclosed within braces & terminated by semicolon. 2) The class body consists of declaration of variables & functions which are called as members & they are grouped under two sections i.e. private & public. 3) Private and public are known as visibility labels, where private can be accessed only from within the class where public members can be accessed from outside the class also. By default, members of a class are private. 4) The variable declared inside the class are known as data members & functions are known as member functions. Only the member function can have access to the private data members & private functions. However the public members can be accessed from outside the class.
Example:
class item
{
int number;
float cost;
public :
void getdata (int a, float b);
void putdata (void);
};
In above example, class-name is item. These class data members are private by default while both the functions are public by declaration. The function getdata() can be used to assign values to the member variable number & cost, and putdata() for displaying their values.
These functions provide the only access to data members of the class.