Defining Classes and Member Functions
-
A class is a data type whose variables are objects.
-
An object is a variable that has member variables to hold values and member functions to manipulate and process those values.
-
A class can be obtained from a structure by adding the prototypes and definitions of some member functions.
-
In addition, keywords public or private are used to specify whether a class member function or class variable is public or private.
-
Public class member functions and variables are directly accessible anywhere in the program.
-
Private class member function and variables cannot be directly accessed in the program except within the definition of a member function of that class.
-
A structure is like a class with no member functions and whose member variables are all public by default.
-
Objects of a class type are declared in the same way as variables of the predefined type.
-
Member functions for classes are called using the dot operator as described in chapter 5 for predefined classes such as ifstream and ofstream.
-
A member function of a class is defined the same way as any other functions except that the class name and the scope resolution operator :: must be given in the member function heading.
-
Two or more different classes may have member functions with the same name. They are treated as distinct member functions.
-
Member variables belonging to the same class can be used within the definition of a member function of the class, but the dot operator cannot be used.
-
Combining a number of related items, such as variables and functions that use these variables into a single package, such as an object of a class, is called encapsulation.