��������� ���� C++
���� 26

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

(Default Arguments)

(����������)

����������� �� ������� �� ������������ ������� �� ������������� ������� �� ����� �������� � C++. ������� �� ���, �� ���������� �� �� ��������� ��-����� �� ���� ����� �� �������. �� �� ������� �� ������������� �������, ����� �� �������� �� ������� �������� ������� (������� ��������� �� ���), �� ��� ��������� � ��������� �� ������������ ����� ���� ���� �������.

1. ������ �� ��������� �� ������������. ���� ���������� ���������� overfun.cpp �� �. 3 �� ���������� ������. ����� �� ���� �������� �������� ���� �� �� ��������� ����� �� ������ �� ��� �������� ������. �� ����� ���� �������� ���� ��� ����������� ������� - void box_display(), void box_display(char ch)void box_display(char ch, int n).

���� �� �����, �� � ����������� �� ������������ �� ������� ���� ���� �������, ����� ����� ���� ��� ������ � ���������� �� ���� ���������, ����� � �� �������. ��-���� � ����� ����� �� ����������.

// overfun_def.cpp
// default argument function instead of oberloaded functions

#include <iostream.h>
#include <string.h>                 // for strcpy()
#include <conio.h>                  // for getch()

const int MAX_LENGTH = 40;          // maximum length of text

class fancy_text                    // class displays text
{
   private:
      char text[MAX_LENGTH];        // text to be displayed

   public:
      void set_text(char tx[])      // set the text
      {
         strcpy(text, tx);
      }

      // default line of MAX_LENGTH dashes
      // or line of MAX_LENGTH other symbols
      void box_display(char ch = '-', int n = MAX_LENGTH);
};

// line of n characters
// no default arguments are given in the definition
void fancy_text::box_display(char ch, int n)
{
   int j;

   for (j = 0; j < n; j++)
      cout << ch;

   cout << endl << text << endl;

   for (j = 0; j < n; j++)
      cout << ch;
}

void main()
{
   fancy_text ft1;

   ft1.set_text("Gone with the Wind");

   // display text with default lines
   ft1.box_display();
   cout << endl << endl;

   // default length and equal signs
   ft1.box_display('+');
   cout << endl << endl;

   // equal signs; length matches text
   ft1.box_display('=', 18);
   cout << endl << endl;

   getch();

}   // main()

������������ �� ��������� � ����� � ��������: void box_display(char ch = '-', int n = MAX_LENGTH);. ��������� � ���� ������� ���������� � �� ��� ��� ��������� �� ����������� (������� �����) � ���� ��� �� ����������� �� ������������, ����� �� ����� �� ���������, ��� ��� �� �������. � ������ ���� ��������� �� ���� � ����������� MAX_LENGTH.

����������� �� ��������� � ����� ������ � � ��� �� �� ������ ����������� �� ������������. ��� ���� ��������� � �������� � ��������� �� ������������, �� ������������ �� ������� ������ ���� �������� ���������:

Default argument value redeclared for parameter 'ch'.

� �������� ������� �� ���������� ��� ��� ���������� �� ��������� box_display �� ������ ft1  - �������, ft1.box_display();, ������� ��������� ��� ������� ��������� � ������ �� �������� � ����� ��������� �� ������������, �.�. ch'-'nMAX_LENGTH. ��� ������� ���������, ft1.box_display('+');,  ���� �� ���� ������� �� �������� �������� �� ������������, ����� �������� � ���� � ������������ - MAX_LENGTH. ������� ��������� � ���� ����������� �� ���������� ������� - ������ �� ����������� � �� ����� ���������.

����� �� ������� �� ���� ������, ����� �� �������� ���-������ �������� ��� ����� ���������. ������ ��� �������� �������� ��������� ������ ��� ���������� �� ��������� �� ������������. ������� ��, �� ���� �� �� ������� ��������, ����� � ������ (�.�. �����) ��������, �� ����� �� ���� ��������. �.�. �� ����� �� �������� ��������� �� ������� �����: ft1.box_display(67);, ���� �� ��������, �� ������������ �� �������, �� ��������� ���������� �� ch � ��������� �� ������ � ��������� �� ������������ '-', � �� �������� n = 67.

! ���� ������ � ������� �� ��������, ������� � ���� �� ���������� ������. ������ ������������ �� ������� ������� (� ������ 67) �� ������� ����� �� ������� � �� ������ MAX_LENGTH �� ���� ������ ������� (� ������ �������� � ������� ����� 67 � �������� ������� � C).

������ ��-����, ���� ����������� � ���� �� ������� ����� �� ���� ��������� �� ������������ � ������������ �������� ��� ����������� �� ������������ �� ����� �� ���� � ���� ������: �.�. ���� �� �� ������� ��������� ��������, ��� ��������� � ������������� � �.�. �� ������ ���������. �� ����� ����� �� ����� �� ����������� �� ������� ����� �� ����������� �� � �� ������������ - �������� ������� ���������� ��-���� � ������:

void badfunc(int, int=2, int);     // no good

void okfunc(int, int=2, int=10);   // ok

������� �� ���������� (����� ��-����) � ���� ���� �� �������� �������� �� ���������� ������ - ���� ��� � �� � ����, ������ ��� ��������� ���� ����� ���� ��� �� ��������� ������ ������, � ����������� �� ��������� � ��������� �� ������������ � �������� ������� � ������ ���� � �������� �������� overfun.cpp.

----------------------------------------
Gone with the Wind
----------------------------------------

++++++++++++++++++++++++++++++++++++++++
Gone with the Wind
++++++++++++++++++++++++++++++++++++++++

==================
Gone with the Wind
==================

2. ���� ����������� ������� � ��������� �� ������������ (The cin.getline() Function). � ���� �� ���������� ������ �� ���������� � ��������� cin.get(), ����� ����� ����� �� ������������.  ���������  cin.getline() � ������� - �������� � ��� ���� �� �� ����� ����� �� 80 ������� � ��� �� �� ������� �� ������������ str:

cin.getline(str, 80);     // get up to 80 chars, place in str

������ �� �������� �� ���� �����, �� �� �� ������� ������� �� ������������ � ���������� (��-������������) �� �� ������� ������� <Enter>. ���� ���� �� �� ������� ���� �� ������ ����� ��������, ����� � ������ � ������ ��������� ������ ���� ������ �� ���������� ����������� �� ������ �� �����������. ��������

// get up to 80 chars or until encounter $ sign, place in str
cin.getline(str, 80, '$');

���� ��������� �� ��������� ���� �� ������������ �� ����� �� ������ ��� ��������� �� ������� $ �� �����������. ����� ���� �� �� ������ ������������ �� ��������� cin.getline() ��� ����� IOSTREAM.H � ���� ������� �� �������� ���������� ��������� ����������:

istream getline(char, int, char = '\n');    // simplified declaration

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

Keywords: �++,  OOP programming , C++ , Classes , Inheritance , Reusability , Creating New Data Types , Polymorphism and Overloading
������� ����: ���� , �����, ������� ����������� ������������ , ������������ switch if else ?