Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- #include <cstring>
- //inicializirasht spisuk, po-burzo
- CPlayer::CPlayer():score(0),lives(2),state(1),nick(new char[6]){
- //tui kato ne move v spisuka
- strcpy_s(nick, 5, "Gosho");
- }
- //trqbva da se dobavqt vsichki chlen danni
- CPlayer::CPlayer(char * nick): score(0), lives(2), state(1), nick(new char [strlen(nick)+1] ){
- //zashtoto ima kolizii izpolzvame this->
- strcpy_s(this->nick, strlen(nick), nick);
- }
- CPlayer::CPlayer(unsigned score, unsigned lives, bool state, char* nick): nick(new char[strlen(nick) + 1]) {
- //nqma da slagame drugite v spisuka, za da moje da napravim proverka dali sa validni danni
- this->score = (score >= 0) ? score : 0;
- this->lives = (lives >= 0) ? lives : 2;
- this->state = (state ==false && lives>0) ?true : state;
- strcpy_s(this->nick, strlen(nick), nick);
- }
- CPlayer::CPlayer(bool, char*):score(0), lives(2), nick(new char[strlen(nick) + 1])
- {
- this->state = (state == false && lives > 0) ? true : state;
- strcpy_s(this->nick, strlen(nick), nick);
- }
- //zapochvame cpy construktor
- //CPlayer ivan; ima defaulten construktor
- //cplayer joe, prisvoqva stoinosti na ivan(defaultnatite)
- CPlayer::CPlayer(const CPlayer &rhs): score(rhs.score),lives(rhs.lives),state(rhs.state), nick(new char[strlen(rhs.nick) + 1]) {
- strcpy_s(this->nick, strlen(rhs.nick),rhs. nick);
- }
- //ne e konstruktor nqma spisuk
- //neka imame CPlayer ivan,joe,gosho;
- //joe=ivan=gosho
- //dokato ima funkciqta sushtestvuva i obekta, ako nqmashe & v nachaloto shteshe da e tmp
- CPlayer& CPlayer::operator=(const CPlayer &rhs){
- if (this != &rhs) {
- if (nick != nullptr) {
- delete[] nick;
- nick = nullptr;
- }
- }
- score = rhs.score;
- lives = rhs.lives;
- state = rhs.state;
- nick=new char[strlen(rhs.nick) + 1];
- strcpy_s(this->nick, strlen(rhs.nick), rhs.nick);
- //funkciq e za tova trqbva da vurne rezultat
- //vrushtame obekt za tova e s *
- return *this;
- }
- CPlayer::~CPlayer(){}
- int CPlayer::setScore(unsigned score){
- this->score = (score > 0) ? score : 0;
- return 0;
- }
- int CPlayer::setLives(unsigned lives){
- this->lives = (lives > 0) ? lives : 0;
- return 0;
- }
- //nqma proverka za state
- int CPlayer::setState(bool state){
- this->state = state;
- return 0;
- }
- int CPlayer::setNick(char * nick){
- if (this->nick != nullptr) {
- delete[]this-> nick;
- this->nick = nullptr;
- }
- this->nick = new char[strlen(nick) + 1];
- strcpy_s(this->nick, strlen(nick), nick);
- return 0;
- }
- unsigned CPlayer::getScore() const{
- return score;
- }
- unsigned CPlayer::getLives() const{
- return lives;
- }
- bool CPlayer::getState() const{
- return state;
- }
- char * CPlayer::getNick() const {
- return nick;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement