     12.1 WELCOME.CPP (  C++)

    #include <iostream.h>

    main() {
       cout << "Welcome to Borland C++ programming!  \n"
        return 0 ;
    }


     12.2.  COMMENTS.CPP (   ANSI   C++)

    1:     #include <iostream.h>
    2:
    3:     // ----------------------------------------------------------------------------------
    4:     //  :  
    5:     //  : V1.0
    6:     //  :     C++
    7:     // ----------------------------------------------------------------------------------
    8:
    9:     main()
    10:    {
    11:    cout << "A Brief C++ Commentary\n"; //  
    12:    cout << "\n"; //     
    13:
    14:    /*   ,      
    15:       .
    16:      C++   . */
    17:
    18:    cout << "// This is not a comment.\n\n"; //  
    19:    cout << "/* This also is not comment.*/ \n\n";
    20:    cout /*  . */ << "This text is displayed.\n";
    21:    return 0;
    22:    }


     12.3.  FILTER.CPP ( -  C++)

    1:	#include <iostream.h>
    2:
    3:	main()
    4:	{
    5:	  char ;
    6:
    7:	  while (cin.get(c))
    8:	     cout.put(c);
    9:	  return 0;
    10:	}


     12.4.    GETVAL.CPP (   -
    )

    1:	#include <iostream.h>
    2:	#include <stdlib.h>
    3:
    4:	void test(void);
    5:
    6:	main()
    7:	{
    8:	  double fp;  //  
    9:	  long k;  //   10:
    11:   cout << "Enter a floating-point value:   ";
    12:   cin >> fp;
    13:	  test();
    14:   cout << "Value entered is:   " << fp <<  '\n';
    15:   cout << "Enter an integer value:   ";
    16:   cin >> k;
    17:	  test();
    18:   cout << "Value entered is:   " << k <<  '\n';
    19:	  return 0;
    20:	}
    21:
    22:	void test(void)
    23:	{
    24:   if (!cin.good()) {
    25:     cout << "Error detected";
    26:	    exit(1);
    27:	  }
    28:	}

     12.5.  GETSTR.CPP (   
  )

    1:	#include <iostream.h>
    2:
    3:	main()
    4:	{
    5:	  char s[25];
    6:	  char c;
    7:
    8:    cout << "Enter a 24-char string safely: \n";
    9:	  cin.get(s, 25, '\n');
    10:   cout << "You entered:  " << s << An';
    11:	  if (cin.get(c) &&  != \n')
    12:     cout << "Maximum line length reached\n";
    13:	  return 0;
    14:	}


     12.6. CONVERT.CPP (     -)

    1:	#include <iostream.h>
    2:	#include <stdlib.h>
    3:
    4:	#define SIZE 35
    5:
    6:	main()
    7:	{
    8:	  int value;
    9:	  char s[SIZE];
    10:
    11:   cout << "Value? ";
    12:	  cin.get(s, SIZE, '\n')
    13:	  value = atoi(s);
    14:   cout << "Decimal=" <<   dec << value
    15:   << "Hexadecimal=0x" <<  hex << value
    16:   << "Octal=0" << oct <<   value << '\n' ;
    17:	  return 0;
    18:	}


     12.7.  CONVERT2.CPP (   )

1:    #include <iostream.h>
2:    #include <stdio.h>
3:    #include <stdlib.h>
4:
5:    #define SIZE 35
6:
7:    main()
8:    {
9        int value;
10:      char s[SIZE];
11:      char buffer[80];
12:
13:      cout << "Value? ";
14:      cin.get(s, SIZE, '\n');
15:      value = atoi(s);
16:      sprintf(buffer,"Decimal=%d Hexadecimal=%#x Octal=%#o\n"
17:      value, value, value);
18:      cout << buffer;
19:      return 0;
20:   }


     12.8. SCOPE.CPP (   )

    1:    #include <iostream.h>
    2:
    3:    int k = 100; //  
    4:
    5:    main()
    6:    {
    7:
    8:         int i = 200; //  
    9:         cout << "Global k == " << k << '\n';
    10:       cout << "Local i == " << i << '\n';
    11:       {
    12:	    int k = 300;
    13      cout << "Local k == " << k << '\n';
    14:     cout << "Global k == " << ::k << '\n';
    15:      }
    16:      return 0;
    17:   }


     12.9.  INLINE.CPP (  )

    1:   #include <iostream.h>
    2:
    3:  inline int max(int a,   int b)
    4:         {
    5:            if (a >= b)
    6:                 return a;
    7:             else
    8:                return b;
    9:          }
    10:
    11:	main()
    12:	{
    13:	  int , , z;
    14:
    15:   cout << "X? ";
    16:   cin >> x;
    17:   cout << "Y? ";
    18:   cin >> y;
    19:	  z = max(x, );
    20:   cout << "max(a, b) == " << z << '\r\';
    21:	  return 0;
    22:	}


     12.10.  MEHERR.CPP ( new  ,   
 )

    1:	#include	<iostream.h>
    2:	#include	<new.h>
    3:	#include	<stdlib.h>
    4:
    5:	struct q	{
    6:	  int ia[1024]; // 2048- 
    7: 	};
    8.
    9:	main()
    10:	{
    11:	struct q	*qp; //    q
    12:
    13:	set new handler(O); //  new     
    14:	for (;;)  { //    " "
    15:	   qp = new q; //     q
    16:    cout <<       qp, << ' \n ' ; //,   
    17:	   if (qp == 0) { //    new 0 :
    18:       cout << "Out of memory\n"; //    
    19:	     exit(1);	//      1
    20:	  }
    21:	}
    22:	return 0	;//      "unreachable code"
    23:	}


     12.11. NEWSTR.CPP (    new)

    1:	#include <iostream.h>
    2:
    3:	#define SIZE 80
    4:
    5:	main()
    6:	{
    7:
    8.	  char *sp
    9:	  sp = new char[SIZE];
    10;   cout <<        "String?";
    11:	  cin.getlinefsp, SIZE);
    12:   cout << "You entered:" << sp << '\n';
    13:	  delete[]	sp;
    14:	  return 0
    15:	}


     12.12. NEWARRAY.CPP (    new)

     1:     #include <iostream.h>
     2:
     3:     #define COUNT 100 //   
     4:
     5:     main()
     6:     {
     7:     int *array; //   
     8:     int i; //  
     9:
    10:     array = new int[COUNT];
    11:
    12:     //  
    13:     for (i = 0;  i < COUNT;  i++)
    14:     array[i] = i;
    15:
    16:     //   
    17:     for (i = 0;  i < COUNT;  i--) {
    18:       cout.width(e);
    19:       cout << arrayfi];
    20:     }
    21:     delete[] array;
    22:     return 0;
    23:     }


     12.13.  OVERLOAD.CPP ( )

    1:      #include <iostream.h>
    2:
    3:      int square(int a);
    4:      double square(double a);
    5:      long double square(long double a);
    6:
    7:      main()
    8:      {
    9:      int x = 10;
    10:     double  = 20.5;
    11:     long double z = 30.75;
    12:
    13:     cout << square(x) << '\n';
    14:     cout << square(y) << '\n';
    15:     cout << square(z) << '\n';
    16:     return 0;
    17:     }
    18:
    19:     int squarefint a)
    20:     {
    21:     return a * a;
    22:     }
    23:
    24:     double square(double a)
    25:     {
    26:     return a * a;
    27:     }
    28:
    29:     long double square(long double a)
    30:     {
    31:     return a * a;
    32:     }


     12.14.  CENTER.CPP (    )

    1:      #include <iostream.h>
    2:      #include <string.h>
    3:
    4:      #define SIZE 128
    5:
    6:      void Center(const char *instr, char *outstr,
    7:                           int width = 0, char IFill = '-', char rFill = '-');
    8:
    9:      main()
    10:     {
    11:     char instr[SIZE]; //  
    12:     char outstr[SIZEJ; //  
    13:     int length; //    
    14:
    15:     cout << "Enter a string:";
    16:     cin.getline(instr, SIZE / 2);
    17:     length = strlen(instr);
    18:
    19:     Center(instr, outstr);
    20:     cout << outstr << '\n';
    21:     Center(instr, outstr, length + 8);
    22:     cout << outstr << '\n';
    23:     Center(instr, outstr, length + 16, '*', '*');
    24:     cout << outstr << '\n';
    25:     Center(instr, outstr, 78, '<', '>');
    26:     cout << outstr << '\n';
    27:
    28:     return 0;
    29:     }
    30:
    31:     // instr:    
    32:     // outstr:    
    33:     // width:     
    34:     // fill:     
    35:
    36:     void Center(const char *instr,  char *outstr,
    37:                           int width, char IFill, char rFill)
    38:     {
    39:     if (outstr == 0) //      
    40:        return;           //   -  
    41:
    42:     if (instr == 0) { //    ,
    43:          *outstr = 0; //   
    44:                             //  . }
    45:          return;
    46:     }
    47:     //  .    
    48:	int len = strlen(instr);
    49:	if (width < len)
    50:	   width = len;
    51:	int wd2 =	(width - len) / 2;
    52:
    53:	int i; //	  	    for
    54:
    55:	for ( i = 0; i < wd2; i++)
    56:	   outstr[i]	= IFill;
    57:	for ( /*i= i*/; i < wd2 +	len; i++)
    58:	   outstr[ij	= instr[i - wd2]	
    59:	for (/*i	= i*/; i < width;	i++)
    60:	   outstr[i]	= rFill;
    61:	outstr[i]	= 0; //   
    62:	}


     12.15. REFVAR.CPP (  )

    1:     #include <iostream.h>
    2:
    3:     main()
    4:	{
    5:	int ivar = 1234;	//   
    6:	int *iptr = &ivar	;    //    ivar
    7:	int &iref = ivar;	//    ivar
    8:	int *p = &iref;	//    iref
    9:
    10: cout << "ivar == " << ivar << '\n' ;
    11: cout << "*iptr ==        " << *iptr << '\n' ;
    12: cout << "iref == " << iref << '\n';
    13: cout << "*p == " << *p << '\n' ;
    14:	return 0;
    15:	}


     12.15. REFVAR.CPP (  )

    1:     #include <iostream.h>
    2:
    3:     main()
    4:	{
    5:	int ivar = 1234;	//   
    6:	int *iptr = &ivar	;    //    ivar
    7:	int &iref = ivar;	//    ivar
    8:	int *p = &iref;	//    iref
    9:
    10: cout << "ivar == " << ivar << '\n' ;
    11: cout << "*iptr ==        " << *iptr << '\n' ;
    12: cout << "iref == " << iref << '\n';
    13: cout << "*p == " << *p << '\n' ;
    14:	return 0;
    15:	}


     12.16. . (     )

    1: #include <iostream.h>
    2:
    3: #include <stdlib.h>
    4: #include <stdio.h>
    5;
    6: double GetDouble(const char *prompt);
    7: void GetData(double Spaid, double irate);
    8: void Calculate(const double paid, const double rate,
    9;  double &list, double &tax);
    10:
    11: main ()
    12: {
    13:  double paid, rate, list, tax;
    14:  GetData(paid, rate);
    15:  Calculate(paid, rate, list, tax);
    16:  printf('List pride = $%8.2f\n", list);
    17:  printf("Tax paid = $%8.2f\n", tax);
    18:  return 0;
    19: }
    20:
    21: //      
    22: double GetDouble(const char *prompt)
    23: {
    24:  char s[20]; //   
    25:  printf (prompt); //   
    26:  scanf ("%20s", s); //    
    27:  return atof(s); //   
    28:  }
    29:
    30:	//         
    31:	// :   paid  rate
    32:	void GetData(double &paid, double &rate)
    33:	{
    34:	paid = GetDouble("Price paid?");
    35:	rate = GetDouble("Tax rate (ex: .06)?");
    36:	}
    37:
    38:	//        
    39:	// :   list  tax
    40:	void Calculate(const double paid, const double rate,
    41:	                           double &list, double &tax)
    42:	{
    43:	list = paid / (1 + rate);
    44:	tax = paid - list;
    45:	}


     12.17.   REFFUNC.CPP ( )

    1:	&include <iostream.h>
    2:	&include <stdlib.h>
    3:	&include <string.h>
    5:	&define FALSE 0
    6:	&define TRUE 1
    7:	&define SIZE 10
    8:	&define BUFLEN 20
    9:
    10:	double &ref(int index);
    11:	void ShowArray(void); 12:
    13:	double array[SIZE]; 14:
    15:	main()
    16:	{
    17:	int done = FALSE, index;
    18:	char buffer[BUFLEN];
    19:
    20:	for (index = 0; index < SIZE; index++)
    21:	   ref(index) = index; //   !
    22:	while (Idone) {
    23:	  ShowArray();
    24:   cout << "\nEnter index from 0 to 9, Enter to quit:";
    25:	  cin.getline(buffer, BUFLEN);
    26:	  done = (strlen(buffer) ==0);
    27:	  if (Idone) {
    28:	    index = atoi(buffer);
    29:     cout << "Enter floating-point value:";
    30:	    cin,getline(buffer, BUFLEN);
    31:	    ref(index) = atof(buffer);
    32:	  }
    33:	}
    34:	return 0;
    35:	}
    36:
    37:	double &ref(int index)
    38:	{
    39:	  if ((index < 0) | | (index >= SIZE))
    40:	    index = 0;
    41:	  return array[index]; //	    
    42:	}
    43:
    44:	void ShowArray(void)
    45:	{
    46:   cout << "\nArray :\n";
    47:	  for (int i = 0; i < SIZE ; i++)
    48:      cout << "[" << i << "] == " << array[i] << '\n';
    49:	}

