a) Assume that the class \"DictionaryEntry\" is defined as follows: class Dictio
ID: 3540597 • Letter: A
Question
a) Assume that the class "DictionaryEntry" is defined as follows:
class DictionaryEntry
{
string word;
string definition;
public:
DictionaryEntry (string word, string definition);
string getWord ();
string getDefinition ();
} ;
(5 points) Declare a class called " Dictionary" that two member variables: an integer called "numWords" and a array of pointers to DictionaryEntry called "entries".
The class should have a constructor that takes in an int (for the number of words) and an array of DictionaryEntry pointers ( for the entries).
The class should also have a destructor.
b) (10 points) Implement the constructor. The constructor should create a new array for the entries memeber and each element of the array should point to a new DictionaryEntry object. The DictionaryEntry objects should have the same word and definition as the corresponding Dictionary Entry object given in the input.
c) Implement the destructor
Explanation / Answer
a)
class Dictionary
{
public:
INT numWords;
DictionaryEntry **entries;
Dictionary(int numWords,DictionaryEntry **entries2 ); // This is the constructor declaration
~Dictionary(); // This is the destructor: declaration
};
b)
Dictionary::Dictionary(int numWords,DictionaryEntry **entries2)
{
Dictionary::numWords=numWords;
Dictionary::entries = new DictionaryEntry[numWords];
for(int i=0; i<numWords; i++)
entries[i] = entries2[i];
}
c)
Dictionary::~Dictionary(void)
{
delete Dictionary::entries;
delete Dictionary::numWords;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.