Structures

In a C++ program, they will be defined as follows:

struct cityTemps
  {
  char city[80];
  int highTemp, lowTemp;
  };

In the past, if we wanted to have a function alter the value of a data member in a structure, we had to have the structure passed as one of the arguments.

Now, the function can become a member of the structure. The combining of data and the functions that work on it is known as encapsulation.

struct cityTemps
  {
  char city[80];
  int highTemp, lowTemp;

  void setHigh(int);
  };

When the function is defined, it must be done in such a way so that it will be associated with the structure. To specify that a function belongs to a specific structure, use the scope resolution operator (::).

Basic Format:  return_type struct_name::func_name(args)

void cityTemps::setHigh(int newHigh)
  {
  highTemp = newHigh;
  }

To call the function:

From within a non-member function:  struct_var_name.func_name(args)

From within a member function:      func_name(args)


cityTemps temp1;

temp1.setHigh(98);

The ability to add the functions to the structure has a created a completely new "creature". It is now capable of describing both characteristics and behaviors.

In essence, we can now create new data types, which are typically known as abstract data types (ADTs). An ADT will be treated like a new data type by the C++ compiler, which means that type-checking will be performed.

The variables created from these structures are known as objects. Calling a member function for an object is known as "sending a message" to that object.

Access to the different data members should be controlled in order to:

  1. Keep a programmer away from things they shouldn't be touching
  2. Allow the designer to make changes without worrying how it will affect a user

In order to control access, we'll use the keywords: public and private

public -- all member definitions that follow are available to everyone (default)

private -- only the creator of the structure has access

Typically, the variables will be made private, and the functions that access the variables will be made public.

struct cityTemps
  {
  private:
    char city[80];
    int highTemp, lowTemp;

  public:
    void setHigh(int);
  };

In general, structure definitions are contained in header files, which must be included whenever the structure is going to be used. This could lead to the problem of multiple declaration. This problem can be solved by using preprocessor directives.

The directive #define can be used to create compile time flags.

Format 1:  #define FLAG_NAME

Format 2:  #define FLAG_NAME  flag_value

In either case, the FLAG_NAME can now be tested to see whether or not it has been defined.

Format 1:  #ifdef FLAG_NAME
              code or #includes if FLAG_NAME has been defined
           #endif


Format 2:  #ifndef FLAG_NAME
              code or #includes if FLAG_NAME has NOT been defined
           #endif

The typical way to use these flags with structures is as follows:

#ifndef HEADER_FLAG
  #define HEADER_FLAG

  struct cityTemps
    {
    char city[80];
    int highTemp, lowTemp;

    void setHigh(int);
    };
#endif