In: Computer Science
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; }
/* 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 */