Читаем C++ Primer Plus полностью

Before looking at the new listings for the String class example, let’s consider another matter. Suppose you want to copy an ordinary string to a String object. For example, suppose you use getline() to read a string and you want to place it in a String object. The class methods already allow you to do the following:

String name;


char temp[40];


cin.getline(temp, 40);


name = temp;  // use constructor to convert type

However, this might not be a satisfactory solution if you have to do it often. To see why, let’s review how the final statement works:

1. The program uses the String(const char *) constructor to construct a temporary String object containing a copy of the string stored in temp. Remember from Chapter 11, “Working with Classes,” that a constructor with a single argument serves as a conversion function.

2. In Listing 12.6, later in this chapter, the program uses the String & String::operator=(const String &) function to copy information from the temporary object to the name object.

3. The program calls the ~String() destructor to delete the temporary object.

The simplest way to make the process more efficient is to overload the assignment operator so that it works directly with ordinary strings. This removes the extra steps of creating and destroying a temporary object. Here’s one possible implementation:

String & String::operator=(const char * s)


{


    delete [] str;


    len = std::strlen(s);


    str = new char[len + 1];


    std::strcpy(str, s);


    return *this;


}

As usual, you must deallocate memory formerly managed by str and allocate enough memory for the new string.

Listing 12.4 shows the revised class declaration. In addition to the changes already mentioned, it defines the constant CINLIM, which is used in implementing operator>>().

Listing 12.4. string1.h


// string1.h -- fixed and augmented string class definition



#ifndef STRING1_H_


#define STRING1_H_


#include


using std::ostream;


using std::istream;



class String


{


private:


    char * str;             // pointer to string


    int len;                // length of string


    static int num_strings; // number of objects


    static const int CINLIM = 80;  // cin input limit


public:


// constructors and other methods


    String(const char * s); // constructor


    String();               // default constructor


    String(const String &); // copy constructor


    ~String();              // destructor


    int length () const { return len; }


// overloaded operator methods


    String & operator=(const String &);


    String & operator=(const char *);


    char & operator[](int i);


    const char & operator[](int i) const;


// overloaded operator friends


    friend bool operator<(const String &st, const String &st2);


    friend bool operator>(const String &st1, const String &st2);


    friend bool operator==(const String &st, const String &st2);


    friend ostream & operator<<(ostream & os, const String & st);


    friend istream & operator>>(istream & is, String & st);


// static function


    static int HowMany();


};


#endif


Listing 12.5 presents the revised method definitions.

Listing 12.5. string1.cpp


// string1.cpp -- String class methods


#include                  // string.h for some


#include "string1.h"               // includes


using std::cin;


using std::cout;



// initializing static class member



int String::num_strings = 0;



// static method


int String::HowMany()


{


    return num_strings;


}



// class methods


String::String(const char * s)     // construct String from C string


{


    len = std::strlen(s);          // set size


    str = new char[len + 1];       // allot storage


    std::strcpy(str, s);           // initialize pointer


    num_strings++;                 // set object count


}



String::String()                   // default constructor


{


    len = 4;


    str = new char[1];


    str[0] = '\0';                 // default string


    num_strings++;


}



String::String(const String & st)


{


    num_strings++;             // handle static member update


    len = st.len;              // same length


    str = new char [len + 1];  // allot space


    std::strcpy(str, st.str);  // copy string to new location


}



String::~String()                     // necessary destructor


{


    --num_strings;                    // required


    delete [] str;                    // required


}



// overloaded operator methods



    // assign a String to a String


String & String::operator=(const String & st)


{


    if (this == &st)


        return *this;


    delete [] str;


    len = st.len;


    str = new char[len + 1];


    std::strcpy(str, st.str);


    return *this;


}



    // assign a C string to a String


String & String::operator=(const char * s)


{


    delete [] str;


    len = std::strlen(s);


    str = new char[len + 1];


    std::strcpy(str, s);


    return *this;


}



    // read-write char access for non-const String


char & String::operator[](int i)


{


    return str[i];


}



    // read-only char access for const String


const char & String::operator[](int i) const


{


    return str[i];


}



// overloaded operator friends



bool operator<(const String &st1, const String &st2)


{


    return (std::strcmp(st1.str, st2.str) < 0);


}



bool operator>(const String &st1, const String &st2)


{


    return st2 < st1;


}



bool operator==(const String &st1, const String &st2)


{


    return (std::strcmp(st1.str, st2.str) == 0);


}



Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных