Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

S2004 - Data Structures and Algorithms MS2004-2017 Objects and Classes Laborator

ID: 2246454 • Letter: S

Question

S2004 - Data Structures and Algorithms MS2004-2017 Objects and Classes Laboratory 3 Page: 2 of 2 -+Automatic Zoom Shall I compare thee to a summer's day? Question 2 Implement the methods of the class Monster below. 1 #include 2 #include 3 using namespace std; 4 class Monster f 5 public: Monster(const string& n-"Fred", int h-300, int a-50, int d-80); ~Monster O void setName (string n); void setHealth (int h); void setAttack (int a); void setDefense (int d) string getName O int getHealthO int getAttack ; int getDefenseO 6 9 10 12 13 15 6 private: 17 18 string name: int health; int attack; int defense; Laboratory 2 lly sends o that we can improve your experience SAMSUNG

Explanation / Answer

#include<iostream>
#include<string>

using namespace std;

class Monster{
public:
     Monster(const string& n = "Fred", int h = 300, int a=50, int d = 80);
     ~Monster();
     void setName(string n);
     void setHealth(int h);
     void setAttack(int a);
     void setDefense(int d);
     string getName();
     int getHealth();
     int getAttack();
     int getDefense();
     friend ostream &operator<<( ostream &output, const Monster &M );
    
private:
     string name;
     int health;
     int attack;
     int defense;
};

Monster::Monster(const string& n , int h, int a, int d){
      name = n;
      attack = a;
      health = h;
      defense = d;
}

Monster::~Monster(){
   cout << "Object is being deleted" << endl;
}

void Monster::setName(string n){
   name = n;
}
void Monster::setHealth(int h){
   health = h;
}
void Monster::setAttack(int a){
   attack = a;
}
void Monster::setDefense(int d){
   defense = d;
}

string Monster::getName(){
   return name;
}
int Monster::getHealth(){
   return health;
}
int Monster::getAttack(){
   return attack;
}
int Monster::getDefense(){
   return defense;
}
ostream &operator<<( ostream &output, const Monster &M ){
    output << M.name << " " << M.health << " " << M.attack << " " << M.defense;
    return output;
}

int main(void){
    Monster *m1 = new Monster("Ritso",3000,60,100);
    cout << *m1 << endl;
}