You are asked to implement the following “C++ strings” class Here is the class i
ID: 3603584 • Letter: Y
Question
You are asked to implement the following “C++ strings” class Here is the class interface and an example main program: #include <string> #include <iostream> using namespace std; class StringList { public: StringList(); // Creates a new, empty, list StringList(string init[], int size); // Creates a new list, initialized from the given values StringList(const StringList& other); // Copy constructor void addEntry(string str); // Adds a string to the list void displayAllEntry () const; // Diaplay all strings from the list int countEntry() const; // Returns the number of strings in the list string get(int idx) const; // Returns the string at the given index void set(int idx, string str); // Sets the string at the given index ~StringList(); // Destroys the list, releasing any allocated memory void operator=(const StringList& right_side); // Assignment operator private: string *list; int size; }; int main() { // Make an array and put some names in it int init_names_size = 5; string init_names[init_names_size]; init_names[0] = "Ahmad"; init_names[1] = "Wiggum"; init_names[2] = "Natasha"; init_names[3] = "Layla"; // Make two lists, one with each constructor StringList names(init_names, 5), list; cout << "Initial list:" << endl; names.displayAllEntry(); // Add and remove some names names.addEntry("Iyad"); cout << "After adding a name:" << endl; names.displayAllEntry(); names.addEntry("Muntz"); cout << "After adding another name:" << endl; names.displayAllEntry(); names.addEntry("Burns"); names.addEntry("Sameer"); cout << "After adding three names:" << endl; names.displayAllEntry(); cout << endl << "An empty list created with the default constructor:" << endl; list.displayAllEntry(); list.addEntry("Milhouse"); list.addEntry("Hind"); cout << "After adding two entries:" << endl; list.displayAllEntry(); list.set(0, "Jimbo"); cout << "After changing the first entry:" << endl; list.displayAllEntry(); cout << endl << "After assigning first list to the second list: " << endl; list = names; list.displayAllEntry(); cout << "First list (again): " << endl; names.displayAllEntry(); cout << endl << "A third list, created with the copy constructor: " << endl; StringList list_copy(list); list_copy.displayAllEntry(); return 0; } You are asked to implement the following “C++ strings” class Here is the class interface and an example main program: #include <string> #include <iostream> using namespace std; class StringList { public: StringList(); // Creates a new, empty, list StringList(string init[], int size); // Creates a new list, initialized from the given values StringList(const StringList& other); // Copy constructor void addEntry(string str); // Adds a string to the list void displayAllEntry () const; // Diaplay all strings from the list int countEntry() const; // Returns the number of strings in the list string get(int idx) const; // Returns the string at the given index void set(int idx, string str); // Sets the string at the given index ~StringList(); // Destroys the list, releasing any allocated memory void operator=(const StringList& right_side); // Assignment operator private: string *list; int size; }; int main() { // Make an array and put some names in it int init_names_size = 5; string init_names[init_names_size]; init_names[0] = "Ahmad"; init_names[1] = "Wiggum"; init_names[2] = "Natasha"; init_names[3] = "Layla"; // Make two lists, one with each constructor StringList names(init_names, 5), list; cout << "Initial list:" << endl; names.displayAllEntry(); // Add and remove some names names.addEntry("Iyad"); cout << "After adding a name:" << endl; names.displayAllEntry(); names.addEntry("Muntz"); cout << "After adding another name:" << endl; names.displayAllEntry(); names.addEntry("Burns"); names.addEntry("Sameer"); cout << "After adding three names:" << endl; names.displayAllEntry(); cout << endl << "An empty list created with the default constructor:" << endl; list.displayAllEntry(); list.addEntry("Milhouse"); list.addEntry("Hind"); cout << "After adding two entries:" << endl; list.displayAllEntry(); list.set(0, "Jimbo"); cout << "After changing the first entry:" << endl; list.displayAllEntry(); cout << endl << "After assigning first list to the second list: " << endl; list = names; list.displayAllEntry(); cout << "First list (again): " << endl; names.displayAllEntry(); cout << endl << "A third list, created with the copy constructor: " << endl; StringList list_copy(list); list_copy.displayAllEntry(); return 0; } You are asked to implement the following “C++ strings” class Here is the class interface and an example main program: #include <string> #include <iostream> using namespace std; class StringList { public: StringList(); // Creates a new, empty, list StringList(string init[], int size); // Creates a new list, initialized from the given values StringList(const StringList& other); // Copy constructor void addEntry(string str); // Adds a string to the list void displayAllEntry () const; // Diaplay all strings from the list int countEntry() const; // Returns the number of strings in the list string get(int idx) const; // Returns the string at the given index void set(int idx, string str); // Sets the string at the given index ~StringList(); // Destroys the list, releasing any allocated memory void operator=(const StringList& right_side); // Assignment operator private: string *list; int size; }; int main() { // Make an array and put some names in it int init_names_size = 5; string init_names[init_names_size]; init_names[0] = "Ahmad"; init_names[1] = "Wiggum"; init_names[2] = "Natasha"; init_names[3] = "Layla"; // Make two lists, one with each constructor StringList names(init_names, 5), list; cout << "Initial list:" << endl; names.displayAllEntry(); // Add and remove some names names.addEntry("Iyad"); cout << "After adding a name:" << endl; names.displayAllEntry(); names.addEntry("Muntz"); cout << "After adding another name:" << endl; names.displayAllEntry(); names.addEntry("Burns"); names.addEntry("Sameer"); cout << "After adding three names:" << endl; names.displayAllEntry(); cout << endl << "An empty list created with the default constructor:" << endl; list.displayAllEntry(); list.addEntry("Milhouse"); list.addEntry("Hind"); cout << "After adding two entries:" << endl; list.displayAllEntry(); list.set(0, "Jimbo"); cout << "After changing the first entry:" << endl; list.displayAllEntry(); cout << endl << "After assigning first list to the second list: " << endl; list = names; list.displayAllEntry(); cout << "First list (again): " << endl; names.displayAllEntry(); cout << endl << "A third list, created with the copy constructor: " << endl; StringList list_copy(list); list_copy.displayAllEntry(); return 0; }Explanation / Answer
#include <string>
#include <iostream>
using namespace std;
class StringList
{
public:
StringList();
// Creates a new, empty, list
StringList(string init[], int size);
// Creates a new list, initialized from the given values
StringList(const StringList& other);
// Copy constructor
void addEntry(string str);
// Adds a string to the list
void displayAllEntry () const;
// Diaplay all strings from the list
int countEntry() const;
// Returns the number of strings in the list
string get(int idx) const;
// Returns the string at the given index
void set(int idx, string str);
// Sets the string at the given index
~StringList();
// Destroys the list, releasing any allocated memory
void operator=(const StringList& right_side);
// Assignment operator
private:
string *list;
int size;
};
int main()
{
// Make an array and put some names in it
int init_names_size = 5;
string init_names[init_names_size];
init_names[0] = "Ahmad";
init_names[1] = "Wiggum";
init_names[2] = "Natasha";
init_names[3] = "Layla";
// Make two lists, one with each constructor
StringList names(init_names, 5), list;
cout << "Initial list:" << endl;
names.displayAllEntry();
// Add and remove some names
names.addEntry("Iyad");
cout << "After adding a name:" << endl;
names.displayAllEntry();
names.addEntry("Muntz");
cout << "After adding another name:" << endl;
names.displayAllEntry();
names.addEntry("Burns");
names.addEntry("Sameer");
cout << "After adding three names:" << endl;
names.displayAllEntry();
cout << endl << "An empty list created with the default constructor:"
<< endl;
list.displayAllEntry();
list.addEntry("Milhouse");
list.addEntry("Hind");
cout << "After adding two entries:" << endl;
list.displayAllEntry();
list.set(0, "Jimbo");
cout << "After changing the first entry:" << endl;
list.displayAllEntry();
cout << endl << "After assigning first list to the second list: " << endl;
list = names;
list.displayAllEntry();
cout << "First list (again): " << endl;
names.displayAllEntry();
cout << endl << "A third list, created with the copy constructor: " << endl;
StringList list_copy(list);
list_copy.displayAllEntry();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.