Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class GroupProject
- {
- public:
- GroupProject();
- GroupProject(const GroupProject& copyMe); //kominn
- ~GroupProject(); //kominn
- void operator= (const GroupProject ©Me); //kominn
- friend GroupProject operator + (const GroupProject &A, const GroupProject &B);
- friend istream& operator >> (istream &ins, GroupProject &gp);
- friend ostream& operator << (ostream &outs, const GroupProject &gp);
- int get_Count() const;
- private:
- void add_member(string nafn);
- void make_space_for_one_more(string nafn);
- bool is_member(string nafn);
- void sortgroup();
- int n;
- string *p;
- };
- GroupProject::GroupProject()
- {
- n = 0;
- p = NULL;
- }
- GroupProject::GroupProject(const GroupProject& copyMe) //Copy-Constructor
- {
- n = copyMe.n;
- if(n == 0)
- {
- p = NULL;
- }
- else
- {
- p = new string[n];
- for(int i = 0; i < n; i++)
- {
- p[i] = copyMe.p[i];
- }
- }
- }
- GroupProject::~GroupProject() //Destructor
- {
- if(p != NULL)
- {
- delete []p;
- p = NULL;
- }
- }
- void GroupProject::operator = (const GroupProject ©Me)
- {
- if(n != copyMe.n)
- {
- n = copyMe.n;
- if(p != NULL)
- {
- delete [] p;
- }
- if(n == 0)
- {
- p = NULL;
- }
- else
- {
- p = new string [n];
- }
- }
- for (int i = 0; i < n; i++)
- {
- p[i] = copyMe.p[i];
- }
- }
- GroupProject operator + (const GroupProject &A, const GroupProject &B)
- {
- GroupProject C;
- for(int i = 0; i < A.n; i++)
- {
- C.add_member(A.p[i]);
- }
- for(int i = 0; i < B.n; i++)
- {
- C.add_member(B.p[i]);
- }
- return C;
- }
- ostream& operator << (ostream &outs, const GroupProject &gp)
- {
- for(int i = 0; i < gp.n; i++)
- {
- outs << gp.p[i] << endl;
- }
- return outs;
- }
- istream& operator >> (istream &ins, GroupProject &gp)
- {
- int tala;
- string nafn;
- ins >> tala;
- for (int i = 0; i < tala; i++)
- {
- ins >> nafn;
- gp.add_member(nafn);
- }
- return ins;
- }
- int GroupProject::get_Count() const
- {
- return n;
- }
- void GroupProject::add_member(string nafn)
- {
- if ( !is_member(nafn) )
- {
- make_space_for_one_more(nafn);
- sortgroup();
- }
- }
- void GroupProject::make_space_for_one_more(string nafn)
- {
- string *new_p = new string[ n+1 ];
- for (int i = 0; i < n; i++)
- {
- new_p[i] = p[i];
- }
- new_p[n] = nafn;
- n = n+1;
- if (p != NULL)
- {
- delete [] p;
- }
- p = new_p;
- }
- void GroupProject::sortgroup()
- {
- int i, j;
- string tmp;
- for (i = 1; i < n; i++)
- {
- tmp = p[i];
- j = i;
- while (j > 0 && p[j - 1] > tmp)
- {
- p[j] = p[j - 1];
- j--;
- }
- p[j] = tmp;
- }
- }
- void printStudentCount(string title, GroupProject gp);
- int main()
- {
- /*GroupProject A,B;
- cin >> A >> B;
- printStudentCount("Team A", A);
- printStudentCount("Team B", B);
- cout << "Team A:" << endl << A << endl;
- cout << "Team B:" << endl << B << endl;
- cout << "Team A+B:" << endl << A+B << endl;
- cout << "Team A-B:" << endl << A-B << endl;*/
- return 0;
- }
- void printStudentCount(string title, GroupProject gp)
- {
- cout << title << " has ";
- cout << gp.get_Count() << " student";
- if (gp.get_Count() > 1)
- {
- cout << "s";
- }
- cout << "." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement