��������� ���� C++
���� 34
.
 �������� ���� ���������� �� ���������
(Arrays as Instance Data)
.
(����������)
.
������������ �� �������� ���� ���������� �� ����� (�������� � ������ 16) ���������� ������� ��������� ��������, �������� � ������� ������������ � ��������������. ��� ���� � ���������� ������ ���� �������� �� ����� ���������� � �� ���� ���������� �������, ����� ������� ����������� �� ����������� �� �������-������������� ������������ (���).
.
1. ������������ �� ����� (Sizing an Array). � ���������� ������ 31 � ���������� stackcon.cpp ������� st ���� ��������� � ����������� 20 �� ������� �����:

class Stack                   // a stack holds up to 20 ints
{
   private:
      int st[20];             // integers are stored in array
      int top;                // index of last item pushed

   public:
   . . .

};

����� �� ����������?

1. ������� 20 ���� �� ������ �� ���� ���� ����������. ��� ���������� � ���������� ������, ���� ����� ���� � �� ������ � �� �� ������ ������ �� ������� ����� � ���� �����.

2. ����� 19 (20 - 1) � ���������� ��� ��������� �� ����� push(), �� �� �� ������ ���������� �� ������ ����� ������� �� ������:

void push(int var)
{
  if(top >= 19)                // if at or beyond the end of array,
    cout << "Illegal push";    // error message
  else
    st(++top) = var;
}

���� �, �� ��� ������� �� ���������� ��� ���� ���������� ���� �� ����� ������� �� ������, �� ��� ������ �� ������� ���� ����� ���� �� ��� ����� (��� ��� ������ �������� �� ������� �����), ���� ���� � �� ���� ����������, �� ���������� � ���� �� ���� ���������� ���� ��� ������ ������.

��������� ���� �� ���� ����� ���� ������� �� �������� � ������ ��������� (array sized with external const), ����� ��� �� ������� �� ������� �������������� - �������� size ��� array_size ��� ���� ��� ��-����� stack_array_size. ����� ���� � ������ �� �� ������� � ��������.

  // size of st variable of Stack class
  const int stack_array_size = 20;

����� � ����������� �� ����������� ������ �� �� ������� ����� ����������� �� ����� � ����� ���� ��������� � ������ 27 ���� ��������� ����� �������� �� ���������� � � ������ �� ��������� �� �� ��������� �� ���� �� �����. ����� ���� ����� ���� ����� �� ��������� ���� �� ����������� �� ���, ������ ����� ������ ���������� �� ����� ������ �� � � ���� � ���������� ����� ���� (����� ���������� � ������� �� �����).

2. �������������� �� ���������� �� ����� (Initializing Member Variables). �� ��������� �������� ����� ��-���� � ���������

Stack
{
  private:
    const int size = 20;  // illegal initialization!!!
    int st[size];
    int top;
  public:
  . . .

};

� ������������ ������ ���������

 Cannot initialize a class member here.

��� ���� ������������ �� ����� ������ �� ������������� �� ������ �� ������������� (initialization list), ����� ���� � ���������� � ������ 32. ������� �� ���� �, �� �������� ��������� ��-���� � ���� ���� ���������� �� ����� �� ���� �������������� � ���� � ���� ��������, ����� �� �� ��� ��� ������ ������ �� �����. ���� ���������� � � ������ � ����������� �� ���, ������ ���� ����� �� ������������ ����� ������ �� �������.

������� ��, ���� �� �������������� ������������ size � ������������ �� ������� �����

  Stack() : top(-1), size(20)       // constructor
  {   }

�� �� �� ���������, �� ���� ������������� ����� �� ����� �� ��������� �� ��������, �.�. �� ����� �� ���������� �� ����������, � �� �� ����� �� ������� �����������! �.�. �� �� ����� ���� ���� �� ������� �������� ��������� � �����

  int st[size];

���� � �� ������� � ������������, �� size � ��������� �� ������� �����

class Stack            // a stack holds up to 20 ints
{
   private:
     const int size;   // array size; mention that it is defined as constant!!!
     int st[size];     // the constructor complains "Constant expression required."!!!
     int top;          // index of last item pushed

   public:
     Stack() : top(-1), size(20) // constructor with an initialization of size!
     {   }

   . . .

};

�� ������������ ������ ���������

Constant expression required.

��� ������ �� �� ��������, �� ��������������� �� � ����������� (Initialization Is Not Assignment) � ������ ������������ �� �� �������, �� �������������� ����������� size, ����� ���� �� ������� ��� � ������������ ������ ����������� size = 20;, ����� � ��-����:

class Stack                   // a stack holds up to 20 ints
{
   private:
     const int size;  // array size
     int st[size];           // integers are stored in array
     int top;                // index of last item pushed

   public:
     Stack() : top(-1)       // constructor
     {
       size = 20;
     }

   . . .

}

�������� ��� ������� ������� ����������� ������������ ������ ��� ���������:

Constant member 'Stack::size' is not initialized.

Cannot modify a const object.

�� �������� ������, �� �� ����� �� �������������� ������� �� ������ (������� ����� �� �������� �� ����� �� ���������� �� ����������), �� �� ���������� ������, ���� ����� ����� �� ����� �� ����������� �� ���������� (�.�. ��-����).

3. ������������� � ������� ��� (The enum Hack). �� �������� ������ � ��������� ������ Hack [1], ����� �������� "������� �� �", (����� ���� ����� �� �����������), ������ ���� �� � ���-���������� ����� �� �������� � ��������, � ������ ���� ������ �����������. ��� � ������ �� �����, ����� �������� ��������� ��� (enum), �� �� �� ������ � ��������. (�� ���������� ������ ����� ����� ������ 22).

class Stack
{
  private:
    enum { size = 20 };  // established at compile time
    int st[size];        // OK
    int top;

  . . .

};

���� ����� ������, ������ ������������ ������ ������ ����� size � ������� 20. ����� ����������, ���� ����� �� � ��������� � ������� ����������� �� ������� �� ��������� �� ���� ����������, ��� ���������� � ������. ��� ���� ����������� �� C++ �� ��������� ANSI/ISO �������� � ����� ����������.

4. ������������� ��� �������� ��������� (Static Constant Variables). ���� � ���� ���������� �� �����, ����� �� ��������� ������ ������ [1] ��� �� �� �������� �� ������ �����������. ��� � �����, ����� �������� ���� �������������:

class Stack
{
  private:
    static const int size = 20;  // static constant
    int st[size];
    int top;

  . . .

}

����������� size �� ���� �� �� ������� (����� � ������!), �� ������ � ��������, �� �� ������������ �� ����� �� ������������ � ������ ���� �� �� �������� �� ������������ �� ������ st. �������� ���������� �� ����� ���� �������� � ������ 28.

��� � ����������� �������� staticon.cpp, ����� ����� ������ ���� ���������� �������� stackcon.cpp �� ������ 31.

// staticon.cpp
// class models a stack
// uses static constant for member array size
#include <iostream.h>
#include <conio.h>            // for getch()

class Stack                   // a stack holds up to 20 ints
{
   private:
      static const int size = 20;  // array size
      int st[size];           // integers are stored in array
      int top;                // index of last item pushed

   public:
      Stack() : top(-1)       // constructor
      {   }

      void push(int var)      // place an item on the stack
      {
        if(top >= size - 1)                // if at or beyond the end of array,
          cout << "Illegal push";    // error message
        else
          st[++top] = var;
      }

      // remove an item from the stack
      int pop()
      {
        if (top < 0)                                 // if beyond the beginning of array,
        {
          cout << "Cannot pop: stack is emty! ";     // error message
          return 0;
        }
        else
          return st[top--];
      }
};  // class Stack

void main()
{
   Stack s1;                  // create a stack object
   s1.push(11);               // push 3 items onto stack
   s1.push(12);
   s1.push(13);

   cout << s1.pop() << endl;  // pop 3 items and display them
   cout << s1.pop() << endl;
   cout << s1.pop() << endl;

   getch();
}   // main()

�������� �������� �� �������� if(top >= size - 1), ����� ��������� ���� ��� �� ���� �� ������ top = size - 1 (��� ������� �� ����!) ��� ��� ���� ���������� �������� top > size - 1. ����� � �� �������� ��� ��������� pop(): ����� return 0; � ��� � ���������, ������ cout �� ������ �� ����� ������� �������� � ��� �� ���� ���� ���, ����� ���������� ��������, ����� � ���������.

5. ������������� �� �����, ����� � ���������� �� ����� (Initializing a Member Array). �������� �� ����� �� �� ������������� ���� ���� ������������� ������ (initialization list) � ������������, ���� �������� ���� ���������� �� �����.

� ���������� weekdays.cpp �� ������ 20 ������� �� ������� day_name ���� ��������� ���� �������� ��������� � ������������� ����� ����� weekday. �� ��� � ���� �� ����� � � �������� �� ���� ��������� ����� � ����. �� ���������, �������� ��� ����������� �� ������� �� ����� �� ����� �����������:

// FIRST INCORRECT construction
class weekday
{
  private:
    // no good
    char day_name[DPW][MAX] = { "Sunday",    "Monday",   "Tuesday",
                                "Wednesday", "Thursday", "Friday",
                                "Saturday"};

  . . .

};  // end of class weekday

���� �����������

 Cannot initialize a class member here.

// SECOND INCORRECT construction
class weekday
{
  private:
    char day_name[DPW][MAX]

  public:
    // no good!!!
    weekday() : day_name[0]("Sunday"),   day_name[1]("Monday"),
                day_name[2]("Tuesday"),  day_name[3]("Wednesday"),
                day_name[4]("Thursday"), day_name[5]("Friday"),
                day_name[6]("Saturday")
    { }

  . . .

};  // end of class weekday

���� ����������� (����� ��, �� �� ��������� ������� ����)

( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.
Member 'weekday::day_name' is initialized more than once.
( expected.

��� ������ �������� �� ��� ������������ �� ������ �� ����, �� ������������ �� ������ �������� ����� ���� day_name, � ���� ������� ������ ����� (� ���������������� �������� ���� ���). ���� ����, ��������� �� day_name[0], day_name[1] � �.�. �� ������� �� ������������ ���� ������� �� ������� ���� �� ����� day_name � ��������� �������� �������� �������������� � ��������������� ������ �� ������������.

��������� ���� �� ���� �����, ��� ������������ day_name ���� ����������� ���� �������� � �������������� ���� ������������ �� ����� �� ������� �����:

class weekday                         // class of day names
{
   private:
      int day_number;                 // Sunday = 0, etc.
      static const char day_name[DPW][MAX];
  . . .

};  // end of class weekday

const char weekday::day_name[DPW][MAX] = {"Sunday",  "Monday",
                                          "Tuesday", "Wednesday",
                                          "Thursday", "Friday",
                                          "Saturday" };

��� � ������ ��������, ����� ����� ������ ����� �������:

// weekdays1.cpp
// creates a class of days of the week objects

#include <iostream.h>
#include <string.h>     // for stricmp()
#include <conio.h>      // for getche()

class weekday                         // class of day names
{
   private:
      int day_number;                 // Sunday = 0, etc.
      static const int MAX = 10;     // maximum length of day name, +1
      static const int DPW = 7;      // days per week
      static const char day_name[DPW][MAX];

   public:
      void inday()                    // user inputs day name
      {
         char tempday[MAX];           // holds user input
         int gotit = 0;               // match found? (0=false)
         int j;                       // loop variable

         while( !gotit )              // cycle until user enters
         {                            // a correct name
            cout << "Enter day of week (e.g., Friday): ";

            cin >> tempday;
            for (j = 0; j < DPW; j++)      // compare user input
            {                              // with list of names
               if (stricmp(tempday, day_name[j]) == 0)
               {                   // if there's a match,
                  gotit = 1;          // set flag,
                  break;              // break out of for loop
               }
            }  // end for

            day_number = j;           // record what day it was
         }  // end while
      }  // void inday()

      void outday()                   // display the day name
      {
         cout << day_name[day_number];
      }

      void outnumber()                // display the day number
      {
         cout << (day_number + 1);
      }

      void add(int days)             // add days to
      {                              // this weekday
         day_number += days;         // add days
         day_number %= DPW;          // ensure not > 7
      }
};  // end weekdays class

const char weekday::day_name[DPW][MAX] = {"Sunday",  "Monday",
                                          "Tuesday", "Wednesday",
                                          "Thursday", "Friday",
                                          "Saturday" };

////////////////////////////////////////////////////////////////
void main()
{
   weekday wd;                      // make a weekday object

   cout << "What day is it?" << endl;
   wd.inday();                      // user gives it a value

   cout << "You entered ";
   wd.outday();                     // display its day name

   cout << "\nThat's day number ";
   wd.outnumber();                  // display its day number

   wd.add(10);                      // add 10 days to it
   cout << "\nTen days later is ";
   wd.outday();                     // display its day name

   // stop the flow on the monitor
   getch();

}  //end main

�������� ��������, �� ����������� MAXDPW ���� �� ������� ���� ��������:

      static const int MAX = 10;     // maximum length of day name, +1
      static const int DPW = 7;      // days per week

�� �� ��������� ������� ������������ �� ���������� ��������� � ������� �������� ���������� �� ������ 28 �� �������� ���������� �� �����.
.

(����������)
.
����������
.
[1] Robert Lafore; C++ Interactive Course. Waite Group Press, Macmillan Computer Publishing, 1996.
.
�����: ����. �������� �����������
.
[ ���� � �������� �� ���� 33 �� ��������� 2009 � �� �������� "������" www.kosnos.com ]

Keywords: �++,  OOP programming , C++ , Classes , Inheritance , Reusability , Creating New Data Types , Polymorphism and Overloading
������� ����: ���� , �����, ������� ����������� ������������ , ������������ switch if else ?
Arrays as Instance Data
Array Sized with Constant
Static Constant Variables
The enum Hack
Arrays Sized at Compile Time
Initialization Is Not Assignment
Instance Data Initialization