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

    // simple String output

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

{

    os << st.str;

    return os;

}

    // quick and dirty String input

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

{

    char temp[String::CINLIM];

    is.get(temp, String::CINLIM);

    if (is)

        st = temp;

    while (is && is.get() != '\n')

        continue;

    return is;

}

The overloaded >> operator provides a simple way to read a line of keyboard input into a String object. It assumes an input line of String::CINLIM or fewer characters and discards any characters beyond that limit. Keep in mind that the value of an istream object in an if condition evaluates to false if input fails for some reason, such as encountering an end-of-file condition, or in the case of get(char *, int), reading an empty line.

Listing 12.6 exercises the String class with a short program that lets you enter a few strings. The program has the user enter sayings, puts the strings into String objects, displays them, and reports which string is the shortest and which comes first alphabetically.

Listing 12.6. sayings1.cpp

// sayings1.cpp -- using expanded String class

// compile with string1.cpp

#include

#include "string1.h"

const int ArSize = 10;

const int MaxLen =81;

int main()

{

    using std::cout;

    using std::cin;

    using std::endl;

    String name;

    cout <<"Hi, what's your name?\n>> ";

    cin >> name;

    cout << name << ", please enter up to " << ArSize

         << " short sayings :\n";

    String sayings[ArSize];     // array of objects

    char temp[MaxLen];          // temporary string storage

    int i;

    for (i = 0; i < ArSize; i++)

    {

        cout << i+1 << ": ";

        cin.get(temp, MaxLen);

        while (cin && cin.get() != '\n')

            continue;

        if (!cin || temp[0] == '\0')    // empty line?

            break;              // i not incremented

        else

            sayings[i] = temp;  // overloaded assignment

    }

    int total = i;              // total # of lines read

    if ( total > 0)

    {

        cout << "Here are your sayings:\n";

        for (i = 0; i < total; i++)

            cout << sayings[i][0] << ": " << sayings[i] << endl;

        int shortest = 0;

        int first = 0;

        for (i = 1; i < total; i++)

        {

            if (sayings[i].length() < sayings[shortest].length())

                shortest = i;

            if (sayings[i] < sayings[first])

                first = i;

        }

        cout << "Shortest saying:\n" << sayings[shortest] << endl;;

        cout << "First alphabetically:\n" << sayings[first] << endl;

        cout << "This program used "<< String::HowMany()

             << " String objects. Bye.\n";

    }

    else

        cout << "No input! Bye.\n";

     return 0;

}

Note

Older versions of get(char *, int) don’t evaluate to false upon reading an empty line. For those versions, however, the first character in the string is a null character if an empty line is entered. This example uses the following code:

if (!cin || temp[0] == '\0')    // empty line?

    break;                      // i not incremented

If the implementation follows the current C++ Standard, the first test in the if statement detects an empty line, whereas the second test detects the empty line for older implementations.

The program in Listing 12.6 asks the user to enter up to 10 sayings. Each saying is read into a temporary character array and then copied to a String object. If the user enters a blank line, a break statement terminates the input loop. After echoing the input, the program uses the length() and operator<() member functions to locate the shortest string and the alphabetically earliest string. The program also uses the subscript operator ([]) to preface each saying with its initial character. Here’s a sample run:

Hi, what's your name?

>> Misty Gutz

Misty Gutz, please enter up to 10 short sayings :

1: a fool and his money are soon parted

2: penny wise, pound foolish

3: the love of money is the root of much evil

4: out of sight, out of mind

5: absence makes the heart grow fonder

6: absinthe makes the hart grow fonder

7:

Here are your sayings:

a: a fool and his money are soon parted

p: penny wise, pound foolish

t: the love of money is the root of much evil

o: out of sight, out of mind

a: absence makes the heart grow fonder

a: absinthe makes the hart grow fonder

Shortest saying:

penny wise, pound foolish

First alphabetically:

a fool and his money are soon parted

This program used 11 String objects. Bye.

Things to Remember When Using new in Constructors

By now you’ve noticed that you must take special care when using new to initialize pointer members of an object. In particular, you should do the following:

• If you use new to initialize a pointer member in a constructor, you should use delete in the destructor.

• The uses of new and delete should be compatible. You should pair new with delete and new [] with delete [].

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

Все книги серии 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 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

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

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

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

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

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