The indexer has to search for the element that corresponds to aIndex
Ask Expert

Be Prepared For The Toughest Questions

Practice Problems

The indexer has to search for the element that corresponds to aIndex

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

problemset-3

Hint
Computer" Indexers allow instances of a class to be indexed just like selections. The indexed value can be fixed or retrieved without clearly specifying a type or occasion member. Indexers resemble belongings except that their accessors take restrictions."...

Know the process

Students succeed in their courses by connecting and communicating with
an expert until they receive help on their questions

1
img

Submit Question

Post project within your desired price and deadline.

2
img

Tutor Is Assigned

A quality expert with the ability to solve your project will be assigned.

3
img

Receive Help

Check order history for updates. An email as a notification will be sent.

img
Unable to find what you’re looking for?

Consult our trusted tutors.

Developed by Versioning Solutions.