Classes in C++
A class is used to pack data members and member function together. The class has a mechanism to prevent direct access to its members, which is the central idea of object-oriented programming. The whole declaration of class is given in Table. The class declaration is also known as formation of new abstract data type. The abstract data type can be used as basic data type such as int, float, etc.
Table: Syntax and an Example of class
The class is a keyword. The class declaration is same as struct declaration. The declaration of a class is enclosed with curly braces and terminated with a semicolon. The data members and member functions can be declared in two sections, that is private and public. The private and public keywords are terminated with a colon (:). The object cannot directly access the data members and member functions declared in private section, but it can access the data member and member functions declared in public section. The private members of a class can only be accessed by a public member function of the same class. Different sections of class are illustrated with examples in the following sections.
It is also possible to access private member variables directly like public member variables provided that the class should have at least one public member variable. Both the private and public member variables are stored in consecutive memory locations in the memory. A pointer to member variable provides address of member variable. By applying increment (++) and decrement (--) operations on pointer, we can access all private and public member variables of the class. The object of a class contains address of the first member variable. It can be also used to access the private or public data.