Question

In: Computer Science

In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...

In C++

14.22 A dynamic class - party list

Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=).

//main.cpp

#include <iostream>

using namespace std;

#include "Party.h"

int main()
{
return 0;
}

//party.h

#ifndef PARTY_H
#define PARTY_H

class Party
{
private:
string location;
string *attendees;
int maxAttendees;
int numAttendees;
  
public:
Party();
Party(string l, int num); //Constructor
Party(/*parameters*/); //Copy constructor
Party& operator=(/*parameters*/);
//Add destructor
void addAttendee(string name);
void changeAttendeeAt(string name, int pos);
void print();
string getAttendeeAt(int pos);
int getMaxAttendees() const;
int getNumAttendees() const;
string getLocation() const;
};

#endif

//party.cpp

#include <iostream>
#include <string>

using namespace std;

#include "Party.h"

Party::Party()
{
//Default: 10 attendees, location = home
location = "Home";
maxAttendees = 10;
numAttendees = 0;
attendees = new string[maxAttendees];
}

Party::Party(string l, int num)
{
//Complete constructor with parameters
//If num<1, set maxAttendees = 10
}

Party::Party(/*parameters*/)
{
//Complete copy constructor
}

Party& Party::operator=(/*parameters*/)
{
//Complete assignment
}

//Add destructor


//The following functions are provided
//Do not change

void Party::addAttendee(string name)
{
if(numAttendees < maxAttendees)
{
attendees[numAttendees] = name;
numAttendees++;
}
else
cout << "Your party is already full!\n";
}

void Party::changeAttendeeAt(string name, int pos)
{
if(pos>=0 && pos<numAttendees)
attendees[pos] = name;
else
cout << "Invalid index.";
}

void Party::print()
{
if(numAttendees > 0)
{
cout << "Attendees list:\n";
for(int i = 0; i<numAttendees; i++)
cout << attendees[i] << endl;
}
else
cout << "List is empty! Invite more people to your party.\n";
}

string Party::getAttendeeAt(int pos)
{
if(pos>=0 && pos<numAttendees)
return attendees[pos];
else
return "Invalid index.";
}

int Party::getNumAttendees() const
{ return numAttendees; }

int Party::getMaxAttendees() const
{ return maxAttendees; }

string Party::getLocation() const
{ return location; }

Solutions

Expert Solution

/* party.h */

#ifndef PARTY_H
#define PARTY_H

class Party
{

private:

string location;

string *attendees;

int maxAttendees;

int numAttendees;

public:

Party();

Party(string l, int num); //Constructor

Party(const Party &obj); //Copy constructor

Party& operator=(const Party &obj);

//Add destructor

~Party(); // destructor

void addAttendee(string name);

void changeAttendeeAt(string name, int pos);

void print();

string getAttendeeAt(int pos);

int getMaxAttendees() const;

int getNumAttendees() const;

string getLocation() const;

};

#endif

/* party.cpp */

#include <iostream>

#include <string>

using namespace std;

#include "Party.h"

Party::Party()

{

//Default: 10 attendees, location = home

location = "Home";

maxAttendees = 10;

numAttendees = 0;

attendees = new string[maxAttendees];

}

Party::Party(string l, int num)

{

location = l;

numAttendees = 0;

if(num < 1){

maxAttendees = 10;

}else{

maxAttendees = num;

}

attendees = new string[maxAttendees];

}

Party::Party(const Party &obj)

{

numAttendees = obj.numAttendees;

maxAttendees = obj.maxAttendees;

  attendees = new string[maxAttendees];

  for(int i = 0 ; i < numAttendees; i++){

    attendees[i] = obj.attendees[i];

  }

}

Party& Party::operator=(const Party &obj)

{

if (this != &obj) {

numAttendees = obj.numAttendees;

maxAttendees = obj.maxAttendees;

attendees = new string[maxAttendees];

for (int i=0; i<numAttendees; i++)

attendees[i] = obj.attendees[i];

}

return *this;

}

//Add destructor

Party::~Party(){ // destructor

delete [] attendees;

}

//The following functions are provided

//Do not change

void Party::addAttendee(string name)

{

if(numAttendees < maxAttendees)

{

attendees[numAttendees] = name;

numAttendees++;

}

else

cout << "Your party is already full!\n";

}

void Party::changeAttendeeAt(string name, int pos)

{

if(pos>=0 && pos<numAttendees)

attendees[pos] = name;

else

cout << "Invalid index.";

}

void Party::print()

{

if(numAttendees > 0)

{

cout << "Attendees list:\n";

for(int i = 0; i<numAttendees; i++)

cout << attendees[i] << endl;

}

else

cout << "List is empty! Invite more people to your party.\n";

}

string Party::getAttendeeAt(int pos)

{

if(pos>=0 && pos<numAttendees)

return attendees[pos];

else

return "Invalid index.";

}

int Party::getNumAttendees() const

{ return numAttendees; }

int Party::getMaxAttendees() const

{ return maxAttendees; }

string Party::getLocation() const

{ return location; }

/* main.cpp */ // I added code to check copy constructor

#include <iostream>

using namespace std;

#include "Party.h"

int main()

{

cout<<"Attendence List party p1\n";

Party p1("usa",3);

p1.addAttendee("George");

p1.addAttendee("zeo");

p1.addAttendee("tyson");

p1.print();

Party p2 = p1;

cout<<"Attendence List party p2 using copy Constructor\n";

p2.print();

return 0;

}

/* OUTPUT */

/* PLEASE COMMENT IF DOUBT OTHERWISE PLEASE UPVOTE */


Related Solutions

C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the h, m and s into separate int values that can be used to set the time of a new time object. int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it. std::string...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT