Classes

Encapsulation and access control with structures naturally leads to the idea of a class. A class is identical to a struct in almost every way except one: items within a class default to private.

class class_name
  {
  public:
    public data members and functions

  private:
    private data members and functions
  };

An instance of the class is known as an object.

Any information that the class needs to hold/save can be contained within the class by creating data members, which are simply variables.

The data members in the class are typically manipulated using functions, which are known as methods.

Initialization

When a class is being designed, a special initialization method, called a constructor, should also be created. The job of the constructor is to initialize the data members of the class. The name of the constructor is ALWAYS the same as the class.

class X
  {
  public:
    X();             //The constructor

  private:
    int i;
  };

To define a constructor:

class_name::class_name()
  {
  /* initialization code goes here */
  }


X::X()
  {
  i = 0;
  }

The constructor will be automatically called when an object is created.

There may be more than one constructor in a class, all of which will be the name of the class. The compiler will distinguish between the constructors by examining the number of arguments, argument data types, and order of arguments.

class X
  {
  public:
    X();             //The default constructor
    X(int);          //A second constructor

  private:
    int i;
  };


X::X()
  {
  i = 0;
  }


X::X(int initI)
  {
  i = initI;
  }

To use the class and constructors:

int main()
  {
  X x1;              /* Creates an object using default constructor */

  X x2(8);           /* Creates an object using other constructor */

  /* code to manipulate the objects */
  
  return 0;  
  }

If no default constructor is created, the compiler will create one. DO NOT rely on this.

Methods

The methods of a class are basically coded like regular functions. The two main differences are that:

  1. the prototype is placed inside of the class definition
  2. the header for the method MUST associate the method with the class that it belongs to

The prototype has the same format as a regular function prototype.

The header is slightly different though. As with structures, the scope resolution operator (::) will be used to associate the method and the class.

return_data_type   class_name::method_name (argument list)
  {
  method code goes here
  }

Because a method is a member of the class it has direct access to all of the data members and other methods of the class. Therefore, it can call or change any of the values by name.

For example:

class X
  {
  public:
    X();             //The default constructor
    X(int);          //A second constructor
    
    int get_i();        //method prototype
    void set_i(int i);  //method prototype

  private:
    int i;
  };


X::X()
  {
  i = 0;
  }


X::X(int initI)
  {
  i = initI;
  }


int  X::get_i()
  {
  return i;
  }


void  X::set_i(int newI)
  {
  if ( newI != get_i() )
     i = newI;
  }

When this class is being used in a program, the dot operator (.) will be used to call a method.

int main()
  {
  X x1;              /* Creates an object using default constructor */
  X x2(8);           /* Creates an object using other constructor */


  x1.set_i( 256 );
  
  cout << x2.get_i();
    
  return 0;  
  }