Problem 3
Implement operator[]. The indexer has to search for the element that corresponds to aIndex. Also, aIndex may be out of bounds. Hence the indexer has to throw a out_of_range exception.
You can use #define P3 in Main.cpp to enable the corresponding test driver.
void testP3()
{
using StringList = List<string>;
string s1( "AAAA" );
string s2( "BBBB" );
string s3( "CCCC" );
string s4( "DDDD" );
string s5( "EEEE" );
string s6( "FFFF" );
StringList lList;
lList.push_front( s4 );
lList.push_front( s3 );
lList.push_front( s2 );
lList.push_front( s1 );
lList.push_back( s5 );
lList.push_back( s6 );
cout << "Test of problem 3:" << endl;
try
{
cout << "Element at index 4: " << lList[4] << endl;
lList.remove( s5 );
cout << "Element at index 4: " << lList[4] << endl;
cout << "Element at index 6: " << lList[6] << endl;
cout << "Error: You should not see this text." << endl;
}
catch (out_of_range e)
{
cerr << "\nSuccessfully caught error: " << e.what() << endl;
}
cout << "Completed" << endl;
}
The result should look like this:
Test of problem 3:
Element at index 4: EEEE
Element at index 4: FFFF
Element at index 6:
Successfully caught error: Index out of bounds.
Completed
Students succeed in their courses by connecting and communicating with an expert until they receive help on their questions
Consult our trusted tutors.