⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chararray.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Exercise 22.16: CharArray.cpp
// CharArray class member-function and nonmember-function
// definitions.
#include <iostream> // required to perform C++ stream I/O
#include <string>   // required to access string functions
#include "CharArray.h" // PhoneNumber class definition

// default constructor
CharArray::CharArray(  )
{
   length = 1; // save size of the array

   text = new char[ 1 ]; // dynamically allocate an array

   text[ 0 ] = '\0'; // initialize to null character

} // end default constructor

// two-argument constructor
CharArray::CharArray( char value[], int size )
{
   length = size; // save size of the array

   text = new char[ size ]; // dynamically allocate an array

   for ( int i = 0; i < size; i++ )
   {
      text[ i ] = value[ i ];
   } // end for

} // end two-argument constructor

// returns the CharArray's character array
void CharArray::getText( char value[] )
{
   for ( int i = 0; i < getLength(); i++ )
   {
      value[ i ] = text[ i ];
   } // end for

} // end function getText

// sets the character array
void CharArray::setText( char value[], int size )
{
   length = size; // save size of the array
   
   delete [] text; // free previously allocated memory

   text = new char[ size ]; // dynamically allocate an array

   for ( int i = 0; i < size; i++ )
   {
      text[ i ] = value[ i ];
   } // end for

} // end function setText

// returns the CharArray's length
int CharArray::getLength()
{
   return length;

} // end function getLength

// sets the length
void CharArray::setLength( int value )
{
   // if this CharArray is being shortened
   if ( value <= getLength() )
   {
      length = value;

   } // end if
   else // if this CharArray is being extended
   {
      // dynamically allocate a new array
      char *newText = new char[ value ];
      
      // transfer over pre-existing characters
      for ( int i = 0; i < getLength(); i++ )
      {
         newText[ i ] = text[ i ];

      } // end for

      delete [] text; // free previously allocated memory

      text = newText;

      length = value;
  
   } // end else

} // end function setLength

// returns the char at the specified index
char CharArray::getChar( int index )
{
   if ( ( index >= 0 ) && ( index < getLength() ) )
   {
      return text[ index ];

   } // end if
   else
   {
      return '\0';

   } // end else

} // end function getChar

// sets the char at a specified index
void CharArray::setChar( char myChar, int index )
{
   if ( ( index >= 0 ) && ( index < getLength() ) )
   {
      text[ index ] = myChar;
   
   } // end if

} // end function setChar

/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -