Subscript operator for sequence class

Question1:

For a sequence x, we would like to be able to refer to the individual items using the usual C++ notation for arrays. For example, if x has three items, then we want to be able to write x[0], x[1], and x[2] to access these three items. This use of the square brackets is called the subscript operator. The subscript operator may be overloaded as a member function, with the prototype shown here as part of the sequence class:

class sequence{
public:
	...
	value_type operator [](size_type index)
	const;
	...	
}

As you can see, the operator [] is a member function with one parameter. The parameter is the index of the item that we want to retrieve. The implementation of this member function should check that the index is a valid index (i.e., index is less than the sequence size), and then return the specified item.
For this project, specify, design, and implement this new subscript operator for the sequence.

My answer:

重载[]操作符:

sequence::value_type sequence::operator [](size_type index) const{
    assert(index >= 0 && index < size());
    return data[index];
}

测试:

// FILE: sequence_test.cxx
// An interactive test program for the new sequence class
#include <cctype>       // Provides toupper
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
#include "sequence1.h"  // With value_type defined as double
using namespace std;
using namespace main_savitch_3;

// PROTOTYPES for functions used by this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.

char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// The next character has been read (skipping blanks and newline characters), 
// and this character has been returned.

void show_sequence(sequence display);
// Postcondition: The items on display have been printed to cout (one per line).

double get_number( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.


int main( )
{
    sequence test, test1, test2; // A sequence that we’ll perform tests on
    char choice;   // A command character entered by the user
    
    cout << "I have initialized an empty sequence of real numbers." << endl;

    do
    {
        print_menu( );
        choice = toupper(get_user_command( ));
        switch (choice)
        {
            case '!': test.start( );
                      break;
            case '@': test.end();
                      break;
            case '+': test.advance( );
                      break;
            case '?': if (test.is_item( ))
                          cout << "There is an item." << endl;
                      else 
                          cout << "There is no current item." << endl;
                      break;
            case 'C': if (test.is_item( ))
                           cout << "Current item is: " << test.current( ) << endl;
                      else
                          cout << "There is no current item." << endl;
                      break;
            case 'P': show_sequence(test);
                      break;
            case 'S': cout << "Size is " << test.size( ) << '.' << endl;
                      break;
            case 'I': test.insert(get_number( ));
                      break;
            case 'A': test.attach(get_number( ));
                      break;
            case 'R': test.remove_current( );
                      cout << "The current item has been removed." << endl;
                      break;
            case 'H': test.insert_head(get_number());
                      break;
            case 'L': test.remove_head();
                      cout << "The item in the head has been removed." << endl;
                      break;     
            case 'Z': test.insert_tail(get_number());
                      cout << "A new item has been inserted into the tail." << endl;
                      break;
            case 'T': test1.insert(1);
                      test1.insert(2);
                      test1.insert(3);
                      cout << "test:" << endl;
                      show_sequence(test);
                      cout << "test1:" << endl;
                      show_sequence(test1);
                      test2 = test - test1;
                      cout << "test2 = test - test1, its result is" << endl;
                      show_sequence(test2);
                      test -= test1;
                      cout << "test -= test1, its result is" << endl;
                      show_sequence(test);
                      break;
            case 'G': cout << "The item is " << test[get_number()] << endl;
                      break;
            case 'Q': cout << "Ridicule is the best test of truth." << endl;
                      break;
            default:  cout << choice << " is invalid." << endl;
        }
    }
    while ((choice != 'Q'));

    return EXIT_SUCCESS;
}

void print_menu( )
// Library facilities used: iostream.h
{
    cout << endl; // Print blank line before the menu
    cout << "The following choices are available: " << endl;
    cout << " !   Activate the start( ) function" << endl;
    cout << " @   Activate the end() function" << endl;
    cout << " +   Activate the advance( ) function" << endl;
    cout << " ?   Print the result from the is_item( ) function" << endl;
    cout << " C   Print the result from the current( ) function" << endl;
    cout << " P   Print a copy of the entire sequence" << endl;
    cout << " S   Print the result from the size( ) function" << endl;
    cout << " I   Insert a new number with the insert(...) function" << endl;
    cout << " A   Attach a new number with the attach(...) function" << endl;
    cout << " R   Activate the remove_current( ) function" << endl;
    cout << " H   Insert a new item with insert_head() function" << endl;
    cout << " L   Remove an item with remove_head() function" << endl;
    cout << " Z   Insert a new item with insert_tail() function" << endl;
    cout << " T   Test the operator - and -=" << endl;
    cout << " G   Test the operator []" << endl;
    cout << " Q   Quit this test program" << endl;
}

char get_user_command( )
// Library facilities used: iostream
{
    char command;

    cout << "Enter choice: ";
    cin >> command; // Input of characters skips blanks and newline character

    return command;
}

void show_sequence(sequence display)
// Library facilities used: iostream
{
    for (display.start( ); display.is_item( ); display.advance( ))
        cout << display.current( ) << endl;
}

double get_number( )
// Library facilities used: iostream
{
    double result;
    
    cout << "Please enter a real number for the sequence: ";
    cin  >> result;
    cout << result << " has been read." << endl;
    return result;
}

结果:

I have initialized an empty sequence of real numbers.

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: I
Please enter a real number for the sequence: 4
4 has been read.

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: I
Please enter a real number for the sequence: 6
6 has been read.

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: P
6
4

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: I 
Please enter a real number for the sequence: 3.3
3.3 has been read.

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: G
Please enter a real number for the sequence: 2
2 has been read.
The item is 4

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: P
3.3
6
4

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: G
Please enter a real number for the sequence: 1
1 has been read.
The item is 6

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: G
Please enter a real number for the sequence: 0
0 has been read.
The item is 3.3

The following choices are available: 
 !   Activate the start( ) function
 @   Activate the end() function
 +   Activate the advance( ) function
 ?   Print the result from the is_item( ) function
 C   Print the result from the current( ) function
 P   Print a copy of the entire sequence
 S   Print the result from the size( ) function
 I   Insert a new number with the insert(...) function
 A   Attach a new number with the attach(...) function
 R   Activate the remove_current( ) function
 H   Insert a new item with insert_head() function
 L   Remove an item with remove_head() function
 Z   Insert a new item with insert_tail() function
 T   Test the operator - and -=
 G   Test the operator []
 Q   Quit this test program
Enter choice: Q
Ridicule is the best test of truth.

  1. Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p179 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值