Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: In the assignment you will use the vector class to manage a dynamic

ID: 3915923 • Letter: O

Question

Objectives: In the assignment you will use the vector class to manage a dynamic array. After completing this assignmentyou will be able to

• use the vector class to implement programs;

• using iterators to more through a vector;

• use pointers to a vector;

• use member functions of the vector class.

________________________________________

Assignment Description:

Call your driver "vector_test_driver.cpp", your class declaration file “vlist.h” and your class implementation file "vlist.cpp". Define the following behavior for vlist

1. Implement a default constructor. Include the following message, "Default Constructor Invoked" every time the function is called.

2. Implement a copy constructor to perform a deep copy of a vlist object. Include the following message, "Copy Constructor Invoked" every time the function is called. The function should also print the contents of each vlist object on single separate lines.

3. Implement the destructor. Include the following message, "Destructor Invoked" every time the function is called.

4. Implement is_Empty() which returns true if empty; otherwise false.

5. Implement the member function called "Search" to search the vector for an item. The function will return an iterator to the location of the item in the vector it is there; otherwise, the function should print the message, “Item Found” or “Item Not Found”. Which message is printed depends on if the item was found or not found in the vector. Also, print the item (search key) you were looking for.

6. Implement a function called "insert" to add an item to the vector in order (alphabetical order).

7. Implement a function called "remove" to remove an item from the vector if it is there; otherwise prints a message stating it was not in vector; the function should use an iterator and the erase function to remove an item from the vector.

8. Implement the member function called “Print”, to print every item stored in the vector;

Your program should test the operation of the class on C++ strings. For this assignment, put the class declaration in the implementation file “vlist.h" and the implementation file in “vlist.cpp”. Please consider the file “vlist_driver.cpp” and the following skeleton class to help you implement the class vlist:

class vlist { public: //vlist(); //default constructor //vlist(const vlist &); //copy constructor //~vlist(); //destructor //bool isEmpty(); //return true if empty; otherwise false //vector::iterator search(const string &); //returns the location of the string in the dynamic //array //void insert(const string & key); //add key to dynamic array //void remove(const string & key); //removes key from the vector if it is there; otherwise prints a //message stating it was not in vector; the function should use an iterator and the erase //function to //remove an item from the vector. //void print(); //Print every string in the vector //other functions may be implemented if necessary private: // vector DB; //vector //additonal state variables you may wish add }; Document your code well... Good luck... ________________________________________ Submission of Assignment: Submit the files vlist.h, vlist.cpp, and the driver vlist_driver.cpp ONLY in one zip file called “program9_vector” to Canvas before the due date and time. Remember, ask questions in class. You may need clarification on the assignment, so start early. Consider the following pieces of code to help you get started: /********************************************************************************************** Sample skeleton for the file vlist.h. Remember to always add comments. Documentation is very important! **********************************************************************************************/ #include using namespace std; #ifndef vlist_H #define vlist_H class vlist { public: vlist(); vlist(const vlist &); ~vlist(); bool Is_Empty(); vector::iterator Search(const string &); void Insert(const string &); void Remove(const string &); void Print(); private: vector DB; }; #endif /********************************************************************** Sample skeleton for the file vlist.cpp Remember to always add comments. Documentation is very important! ***********************************************************************/ #include #include #include #include "vlist.h" using namespace std; ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Function Name: vlist //Precondition: Constructor has not been invoked. //Postcondition: count=0, vector size is 9. //Description: Constructs an instance of a vlist object. ////////////////////////////////////////////////////////////////////////////////////////////////////////////// vlist::vlist(){} ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Function Name: vlist //Precondition: A vlist object is being passed by reference. //Postcondition: A hard copy of a vlist object has been created. //Description: Creates a hard copy of a vlist object. ////////////////////////////////////////////////////////////////////////////////////////////////////////////// vlist::vlist(const vlist & Org){} ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Function Name: ~vlist //Precondition: Destructor has not been invoked. //Postcondition: array DB deleted. //Description: Deallocates memory of a vlist object. ////////////////////////////////////////////////////////////////////////////////////////////////////////////// vlist::~vlist(){} bool vlist::is_Empty(){return true;} //vector::iterator vlist::Search(const string & key){} void vlist::Insert(const string & key){} void vlist::Remove(const string & key){} void vlist::Print(){} /****************************************************************************** S A M P L E T E S T D R I V E R Remember to always comment. Document is very important! *****************************************************************************/ //***************************************************************************** //P R O G R A M H E A D E R // // Name: Jane Doe // Z#: 000000000 // Instructor: Dr. Bullard // Class: Data Structures // Term: Summer 2015 // Assignment #1 (vlist) // Due Date: May 20, 2015 // Due Time: 11:00PM // Points: 100 // // Description: This program accesses your C++language skills. // After completing this assignment you will be able to perform the following: // // (1) Manage a vector(allocatm and de-allocate memory, insert inorder, remove, etc). // (2) Implement a default constructor and a copy constructor. // (3) Implement an insert function to add a string, in order, into a vector // (4) Implement a remove function to delete a string from a vector //****************************************************************************** #include #include #include "vlist.h" using namespace std; int main() { //Testing default constructor cout << "Test1:Testing the default constructor for string "; vlist String_List; //Testing functionality for string list cout << "Test2: Testing Insert "; String_List.insert("Hello"); String_List.insert("Zebra"); String_List.insert("Good_Bye"); String_List.insert("Space"); String_List.insert("No_One"); String_List.print(); cout << "Test 3: Testing copy constructor for string "; vlist Copied_String = String_List; Copied_String.print(); cout << "Test 4: Testing Remove for string "; cout << "Testing Search and IsEmpty also "; String_List.remove("Zebra"); String_List.remove("Good_Bye"); String_List.remove("Hello"); String_List.remove("No_One"); String_List.remove("Space"); String_List.remove("Hello"); cout<<"When leave main destructor will be called"<

Explanation / Answer

Answer:

Note: User given template is used.

File Name: vlist.h

//Define header

#ifndef vlist_H

#define vlist_H

//Include file

#include <vector>

//Include file

#include <string>

using namespace std;

//Create class

class vlist

{

//Access specifier

public:

//Constructor

vlist();

//Constructor

vlist(const vlist &myvlist);

//Destructor

~vlist();

//Create function

bool is_Empty();

//Create function

vector<string>::iterator Search(const string &str);

//Create function

void insert(const string &str);

//Create function

void remove(const string &str);

//Create function

void Print();

//Access specifier

private:

//Declare variable

vector<string> DB;

//Declare variable

int count;

//Declare variable

int size;

};

//End

#endif

File Name: vlist.cpp

//Include files

#include "vlist.h"

#include <iostream>

using namespace std;

//Constructor

vlist::vlist()

{

//SEt value

count=0;

//Set value

size = 9;

//Print statement

cout<<"Default Constructor invoked."<<endl;

}

//Define constructor

vlist::vlist(const vlist& myvlist)

{

//Empty the vector

DB.empty();

//SEt size

size = myvlist.size;

//SEt count

count = myvlist.count;

//Loop

for(int kk=0;kk<count; kk++)

{

//Add element

DB.push_back(myvlist.DB.at(kk));

}

//Print message

cout<<"Copy constructor is invoked."<<endl;

}

//Define destructor

vlist::~vlist()

{

//Set value

count = 0;

//Call function

DB.empty();

//Print statement

cout<<"Destructor is invoked."<<endl;

}

//Define function

bool vlist::is_Empty()

{

//Check condition

if(count ==0)

//Return

return true;

//Return

return false;

}

//Define function

vector<string>::iterator vlist:: Search(const string& searchKey)

{

//Print message

cout << " Search is invoked. ";

//Check condition

if (!is_Empty())

{

//Loop

for (vector<string>::iterator kk = DB.begin(); kk != DB.end(); kk++)

{

//Check condition

if (searchKey == *kk)

{

//Print message

cout << " Item " << *kk << " found."<<endl;

//Return

return kk;

}

}

}

//Print message

cout << " Item Not Found ";

//Call function and return

return DB.end();

}

//Define function

void vlist::insert(const string & insertKey)

{

//Check condition

if (is_Empty())

{

//Insert element

DB.push_back(insertKey);

//Increase count

count++;

//Return

return;

}

//Check condition

else if (insertKey <= *DB.begin())

{

//Insert value

DB.insert(DB.begin(), insertKey);

//Increase count

count++;

//Return

return;

}

//Else

else

{

//Loop

for (vector<string>::iterator kk = DB.begin(); kk != DB.end(); kk++)

{

//Check condition

if (insertKey <= *kk)

{

//insert value

DB.insert(kk, insertKey);

//Increase count

count++;

//Return

return;

}

}

//insert value

DB.insert(DB.end(), insertKey);

//Increase count

count++;

}

}

//DEfine function

void vlist::remove(const string & removeKey)

{

//Check condition

if (is_Empty())

{

//Print message

cout << " Empty vector ";

//Return

return;

}

//check condition

else if (Search(removeKey) != DB.end())

{

//Call function and delete

DB.erase(Search(removeKey));

//decrease value

count--;

//Print message

cout << " "<< removeKey << " is deleted. ";

//Return

return;

}

//Check condition

else if (removeKey == *DB.end())

{

//delete value

DB.erase(DB.end());

//decrease value

count--;

//Print message

cout << " "<< removeKey << " is deleted. ";

//REturn

return;

}

//Else

else

{

//Print message

cout << " Not found"<<endl;

}

}

//Define function

void vlist::Print()

{

//Check condition

if (is_Empty())

{

//Print message

cout << " Empty vector"<<endl;

}

//Else

else

{

//Loop

for (vector<string>::iterator kk = DB.begin(); kk != DB.end(); kk++)

{

//Print value

cout << (*kk) << endl;

}

//Print new line

cout << endl;

}

}

File Name: vector_test_driver.cpp

#include<iostream>

#include<string>

#include "vlist.h"

using namespace std;

int main()

{

//Testing default constructor

cout << "Test1:Testing the default constructor for string ";

vlist String_List;

//Testing functionality for string list

cout << "Test2: Testing Insert ";

String_List.insert("Hello");

String_List.insert("Zebra");

String_List.insert("Good_Bye");

String_List.insert("Space");

String_List.insert("No_One");

String_List.Print();

cout << "Test 3: Testing copy constructor for string ";

vlist Copied_String = String_List;

Copied_String.Print();

cout << "Test 4: Testing Remove for string ";

cout << "Testing Search and IsEmpty also ";

String_List.remove("Zebra");

String_List.remove("Good_Bye");

String_List.remove("Hello");

String_List.remove("No_One");

String_List.remove("Space");

String_List.remove("Hello");

cout<<"When leave main destructor will be called"<<endl;

//system("pause");

return 0;

}

Sample Output:

Test1:Testing the default constructor for string                           

Default Constructor invoked.                                               

Test2: Testing Insert                                                      

Good_Bye                                                                   

Hello                                                                      

No_One                                                                     

Space                                                                      

Zebra                                                                      

                                                                           

Test 3: Testing copy constructor for string                                

Copy constructor is invoked.

Good_Bye                                                                   

Hello                                                                      

No_One                                                                     

Space                                                                      

Zebra                                                                      

                                                                           

Test 4: Testing Remove for string                                          

Testing Search and IsEmpty also                                            

                                                                           

Search is invoked.

                                                                           

Item Zebra found.                                                         

                                                                           

Search is invoked.                                                         

                                                                           

Item Zebra found.                                                         

                                                                           

Zebra is deleted.                                                          

                                                                           

Search is invoked.                                                         

                                                                           

Item Good_Bye found.                                                      

                                                                           

Search is invoked.                                                         

                                                                           

Item Good_Bye found.

                                                                           

Search is invoked.                                                         

                                                                           

Item Good_Bye found.                                                      

                                                                           

Good_Bye is deleted.                                                       

                                                                           

Search is invoked.                                                         

                                                                           

Item Hello found.                                                         

                                                                           

Search is invoked.                                                         

                                                                           

Item Hello found.                                                         

                                                                           

Hello is deleted.                                                          

                                                                           

Search is invoked.

                                                                           

Item No_One found.                                                        

                                                                           

Search is invoked.                                                         

                                                                           

Item No_One found.                                                        

                                                                           

No_One is deleted.                                                         

                                                                           

Search is invoked.                                                         

                                                                           

Item Space found.                                                         

                                                                           

Search is invoked.                                                         

                                                                           

Item Space found.

                                                                           

Space is deleted.                                                          

                                                                           

Empty vector                                                               

When leave main destructor will be called                                  

Destructor is invoked.                                                     

Destructor is invoked.