Question

C++ Santa Claus allegedly keeps lists of those who are naughty and those who are nice....

C++

Santa Claus allegedly keeps lists of those who are naughty and those who are nice. On the naughty list are the names of those who will get coal in their stockings. On the nice list are those who will receive gifts. Each object in this list contains a name (a string) and a list of that person’s gifts (an instance of an ADT list). Design an ADT for the objects in the nice list. Specify each ADT operation by stating its purpose, describing its parameters, and writing preconditions, postconditions, and a pseudocode version of its header. Then write a template interface for the ADT. Write implementation of at least the two following methods: 1) Insert a new name along with a list of its gifts, 2) Add one more gift to the list of gifts for a given kid’s name. No need to describe constructors or destructors.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the code for the above question:

Header:

#include <iostream>
#include <iomanip>
#include <string>
#include <list>
#include "children.h"


int main()
{
std::string theKid;
std::string theGift;
std::list<children> niceKids; //the list of objects

std::cout << "name for nice list: ";
getline(std::cin, theKid);

while(theKid!=""){
std::list<std::string> niceGifts; //the list of gifts that will go to the object.

std::cout << "add gifts for " << theKid << std::endl;
std::cout << "gift: ";
getline(std::cin, theGift);

children theChild(theKid, niceGifts); //passing the variables to the class
theChild.pushBack(theGift); //adding the gifts to the list

while(theGift!=""){
std::cout << "gift: ";
getline(std::cin, theGift);
theChild.pushBack(theGift);
}

niceKids.push_back(theChild); //adds the item to the list of the kids with objects

std::cout << "name for nice list: ";
getline(std::cin, theKid);
}

std::cout << "The list contains";
for (std::list<children>::iterator it = niceKids.begin(); it != niceKids.end(); ++it)
{
children child = *it;
std::cout << "\n" << child.getName() << " ";
child.displayList();
}

return 0;
}

Template:

#ifndef CHILDREN_H
#define CHILDREN_H
#include <string>
#include <list>


class children
{
public:
children(std::string name, std::list<std::string> gifts);
void setName(std::string name);
void setList(std::list<std::string> gifts);
void displayList();
void pushBack(std::string gift);

std::list<std::string> getList();
std::string getName();

~children();

private:
std::string name_private;
std::list<std::string> gifts_private;
};

#endif // CHILDREN_H

Executable for part 1 and 2:

#include "children.h"
#include <iostream>
#include <list>

children::children(std::string name, std::list<std::string> gifts)
{
setName(name);
setList(gifts);
}

void children::setName(std::string name){
name_private = name;
}
void children::setList(std::list<std::string> gifts){
gifts_private = gifts;
}

void children::displayList(){ //the function is created to make the code look less complicated in the main file
for (std::list<std::string>::iterator it = gifts_private.begin(); it != gifts_private.end(); ++it)
{
std::cout << (*it) << " ";
}
}

void children::pushBack(std::string gift){ //making this function private for the class
gifts_private.push_back(gift);
}

std::list<std::string> children::getList(){
return gifts_private;
}
std::string children::getName(){
return name_private;
}

Add a comment
Know the answer?
Add Answer to:
C++ Santa Claus allegedly keeps lists of those who are naughty and those who are nice....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Santa Claus needs to automate the compiling of the naughty and nice lists. Your program will...

    Santa Claus needs to automate the compiling of the naughty and nice lists. Your program will read in each child’s name and their count of major and minor good deeds and their major and minor bad deeds. With that information, compute an overall score and determine if they go on the naughty or nice list. Major good deeds might be something like shoveling the walk and driveway for the chronologically challenged lady down the street, a minor good deed would...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT