Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- class game
- {
- char* name;
- string arma;
- int str;
- public:
- int nrRunde = 0;
- int* puncte = NULL;
- static int players;
- const int id = 0;
- //constructor default
- game():id(0)
- {
- this->name = new char[7];
- strcpy(this->name, "Noname");
- arma = "Nici o arma";
- this->str = 0;
- game::players++;
- }
- //constructor 1 parametru
- game(const char* nume_new) :id(players)
- {
- this->name = new char[strlen(nume_new) + 1];
- strcpy(this->name, nume_new);
- arma = "Nici o arma";
- this->str = 0;
- game::players++;
- }
- //constructor cu 2 parametri
- game(const char* nume_new,string arma_new, int str_new) :id(players)
- {
- this->name = new char[strlen(nume_new) + 1];
- strcpy(this->name, nume_new);
- arma = arma_new;
- this->str = str_new;
- game::players++;
- }
- //constructor de copiere
- game(const game& t):id(t.id)
- {
- this->name = new char[strlen(t.name) + 1];
- strcpy(this->name, t.name);
- this->arma = t.arma;
- this->str = t.str;
- this->nrRunde = t.nrRunde;
- if (t.puncte)
- {
- this->puncte = new int[nrRunde];
- for (int i = 0; i < t.nrRunde; i++)
- {
- this->puncte[i] = t.puncte[i];
- }
- }
- game::players++;
- }
- //setter name
- void setName(const char* name)
- {
- if (this->name != NULL)
- {
- delete[] this->name;
- }
- this->name = new char[strlen(name) + 1];
- strcpy(this->name, name);
- }
- //getter name
- char* getName()
- {
- return this->name;
- }
- //setter arma
- void setArma(string numeArma)
- {
- if (numeArma.length() > 0)
- {
- arma = numeArma;
- }
- }
- //getter arma
- string getArma()
- {
- return this->arma;
- }
- //setter str
- void setStr(int str)
- {
- this->str = str;
- }
- //getter str
- int getStr()
- {
- return this->str;
- }
- //setter puncte
- void setPuncte(int nrRunde, int* puncte)
- {
- this->nrRunde = nrRunde;
- this-> puncte = new int[nrRunde];
- for (int i = 0; i < this->nrRunde; i++)
- {
- this->puncte[i] = puncte[i];
- }
- }
- //display
- void display()
- {
- cout << endl << "____" << endl;
- cout << "Id Jucator: " << this->id << endl;
- cout << "Nume jucator: " << this->name << endl;
- cout << "Arma folosita: " << this->arma << endl;
- cout << "Putere jucator: " << this->str << endl;
- if (puncte != NULL)
- {
- cout << "Puncte jucator: ";
- for (int i = 0; i < nrRunde; i++)
- {
- cout << puncte[i] << " | ";
- }
- cout << endl;
- }
- else
- cout << "Jucatorul nu are puncte" << endl;
- cout << "____" << endl;
- }
- ~game()
- {
- if (this->name != NULL)
- {
- delete[] this->name;
- }
- if (this->puncte != NULL)
- {
- delete[] this->puncte;
- }
- game::players--;
- cout << "Destructor" << endl;
- }
- game operator=(const game& t)
- {
- if (this->name != NULL)
- {
- delete[] this->name;
- }
- if (puncte != NULL)
- {
- delete[] this->puncte;
- }
- this->name = new char[strlen(t.name) + 1];
- strcpy(this->name, t.name);
- this->arma = t.arma;
- this->str = t.str;
- this->nrRunde = t.nrRunde;
- if (t.puncte)
- {
- this->puncte = new int[nrRunde];
- for (int i = 0; i < t.nrRunde; i++)
- {
- this->puncte[i] = t.puncte[i];
- }
- }
- else
- {
- nrRunde = 0;
- puncte = NULL;
- }
- game::players++;
- return *this;
- }
- game& operator++()
- {
- this->str++;
- return *this;
- }
- game& operator++(int i)
- {
- game copie = *this;
- this->str++;
- return copie;
- }
- game operator+(int i)
- {
- this->str += i;
- return *this;
- }
- friend game operator+(int,game&);
- friend ostream& operator<<(ostream&, game&);
- friend istream& operator>>(istream&, game&);
- };//final clasa
- int game::players = 0;
- game operator+(int i,game& t)
- {
- t.str += i;
- return t;
- }
- ostream& operator<<(ostream& out, game& t)
- {
- out << endl << "____" << endl;
- out << "Id Jucator: " << t.id << endl;
- out << "Nume jucator: " << t.name << endl;
- out << "Arma folosita: " << t.arma << endl;
- out << "Putere jucator: " << t.str << endl;
- if (t.puncte != NULL)
- {
- cout << "Puncte jucator: ";
- for (int i = 0; i < t.nrRunde; i++)
- {
- cout << t.puncte[i] << " | ";
- }
- cout << endl;
- }
- else
- out << "Jucatorul nu are puncte" << endl;
- out << "____" << endl;
- return out;
- }
- istream& operator>>(istream& in, game& t)
- {
- char* name;
- cout << "Name: ";
- in >> name;
- t.setName(name);
- return in;
- }
- int main()
- {
- game p1;
- /*p1.display();
- p1.setName("Ionel");
- p1.setArma("Cutit");
- p1.setStr(5);
- p1.display();
- cout << endl << endl;
- cout << p1.getName() << endl;
- cout << p1.getArma() << endl;
- cout << p1.getStr() << endl;*/
- game p2("Dracula");
- //p2.display();
- game p3("Vader");
- //p3.display();
- game p4("Kobra", "Topor", 10);
- int pts[] = { 10,15,10 };
- int runde = 3;
- p4.setPuncte(runde, pts);
- p4.display();
- //++p4;
- //p4++;
- p4+2;
- 2 + p4;
- //p4.display();
- cout << p4;
- game p5(p4);
- //p5.display();
- game p6;
- p6 = p5;
- //p6.display();
- game p7;
- cin>> p7;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement