A class groups a set of values and a set of operations. The values and the operations of a class are called its members. Member variables implement the values and member functions implement the operations.
Suppose we wish to define a class to represent complex numbers . The Complex class definition shown in Program illustrates how this can be done. Two member variables, real and imag, are declared. These represent the real and imaginary parts of a complex numbers (respectively).
Program: Complex Class Definition
Every object instance of the Complex class contains its own member variables. Consider the following variable declarations:
Complex c; Complex d;Both c and d are instances of the complex class. Therefore, each of them has a real and imag member variable. The member variables of an object are accessed using the dot operator. E.g., c.real refers to the real member variable of c and d.imag refers to the imag member of d.
The Complex class definition also contains a list of function prototypes which declare the member functions of the class. In general, a member function performs some operation on an instances of the class. Again, the dot operator is used to specify the object on which the operation is performed. E.g., c.SetReal(1.0) invokes the SetReal function on c and d.Put(cout) invokes the Put function on d.