Skip to content

Instantly share code, notes, and snippets.

@amanuel2
Last active July 25, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amanuel2/f3c0c98ec23046c0a4abec84cda23a46 to your computer and use it in GitHub Desktop.
Save amanuel2/f3c0c98ec23046c0a4abec84cda23a46 to your computer and use it in GitHub Desktop.
/*Main.cpp*/
//============================================================================
// Name : Practice.cpp
// Author : Amanuel Bogale
// Version :
// Copyright : No Copyright
// Description : Hello World in C, Ansi-style
//============================================================================
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Player.h"
ostream& operator<<(ostream &os,const Player& rhs)
{
os << "ATTACK : " << rhs.getAttack() << "\n" << "DEFENSE : " << rhs.getDefense() << "\n"
<< "LIFE : " << rhs.getLife();
return os;
}
int main(void) {
Player p1(4,5,6);
Player p2(6,4,2);
Player p3 = p1+p2;
std::cout << p3 << std::endl;
return EXIT_SUCCESS;
}
/*Player.h*/
#ifndef _PLAYER_H_
#define _PLAYER_H_ 1
#include <sstream>
using std::ostream;
class Player
{
public:
Player(double attack,double defense,double life);
double getAttack() const;
double getDefense() const;
double getLife() const;
Player& operator+(const Player& rhs);
Player& operator=(const Player& rhs);
private:
double attack;
double defense;
double life;
};
#endif
/*Practice.c++*/
//============================================================================
// Name : Practice.cpp
// Author : Amanuel Bogale
// Version :
// Copyright : No Copyright
// Description : Hello World in C, Ansi-style
//============================================================================
#include "Player.h"
Player::Player(double attack,double defense,double life)
{
this->attack = attack;
this->defense = defense;
this->life = life;
}
double Player::getAttack() const
{
return this->attack;
}
double Player::getDefense() const
{
return this->defense;
}
double Player::getLife() const
{
return this->life;
}
// p3 = p1 + p2;
Player& Player::operator+(const Player&rhs)
{
Player *ret;
ret->attack = (this->attack + rhs.attack);
ret->defense= (this->defense + rhs.defense);
ret->life = (this->life + rhs.life);
return *ret;
}
// p1 = p2
Player& Player::operator=(const Player&rhs)
{
if(this == &rhs)
{
return *this;
}
else
{
return *this;
}
}
/*Makefile*/
all:
g++ Player.cpp Practice.cpp -std=c++11 -O2 -W -Wall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment