c++ create, assign and compare a new variable to two object inside an Operator Overloaded function. -


the assignment:
implement alien class using provided alien.h file. alien, in scenario described in terms of his/her height, weight, , gender. compare 2 aliens, use following equation determine alien’s statuspoints value: statuspoints = weight * height * gendervalue gendervalue 2 if alien male, , 3 if alien female. status points should calculated when needed, not kept data member. avoids so-called stale data, in 1 data member, such weight might change , status points variable wouldn’t updated. status should used when comparing aliens. need overload ==, !=, >, <, >=, , <= operators compare aliens. thus, might have statements such following: if(alien1 > alien2) { //do }

obviously, alien1 alien object, , alien2. assumed have data members (height, weight, , gender) initialized.

here provided .h file. again, cannot alter file provided me.

    #ifndef alien_h     #define alien_h      class alien  { public:     alien();                              alien(int h, int w, char g);          void setheight(int h);      void setweight(int w);     void setgender(char g);      int getheight();     int getweight();     char getgender();      //operators: compare aliens     bool operator==(const alien& alien) const;     bool operator!=(const alien& alien) const;     bool operator<=(const alien& alien) const;     bool operator<(const alien& alien) const;     bool operator>=(const alien& alien) const;     bool operator>(const alien& alien) const;  private:     int height; //inches     int weight; //pounds     char gender; //m or f  }; #endif 

here alien.cpp file.

#include "alien.h" #include <iostream> using namespace std;  alien::alien() {     height = 60;     weight = 100;     gender = 'm';     int statuspoints = 0; }  alien::alien(int h, int w, char g) {            height = h;         weight = w;         gender = g;         int statuspoints = 0; }  void alien::setheight(int h) {     height = h; }  void alien::setweight(int w) {     weight = w; }  void alien::setgender(char g) {     gender = g; }  int alien::getheight() {     return height; }  int alien::getweight() {     return weight; }  char alien::getgender() {     return gender; }  bool alien::operator==(const alien& alien) const {     return (height == alien.height && weight == alien.weight && gender == alien.gender); }  bool alien::operator!=(const alien& alien) const {     return (height != alien.height || weight != alien.weight || gender != alien.gender); }  bool alien::operator<=(const alien& alien) const {     alien temp1;     alien temp2;      int gendervalue = 2;     if(gender == 'f')     {          gendervalue = 3;      }      int statuspoints = 0;       if (statuspoints <= statuspoints)     { return true; }         else { return false; }  } 

if can't alter .h file, or make statuspoints member function, create statuspoints variable in main or inside overloaded operator? also... how assign statuspoints var object comparison?

any appreciated. thanks.

in function

alien::alien(int h, int w, char g) {        height = h;     weight = w;     gender = g;     int statuspoints = 0; } 

statuspoints function local variable not going useful later.

my suggestion: create helper function in .cpp file:

static int getstatuspoint(alien const& alien) {     return alien.getheight()*alien.getweight()*aliean.getgender(); } 

and use in other functions.

bool alien::operator== (const alien& rhs) const {   return getstatuspoint(*this) == getstatuspoint(rhs); }  bool alien::operator!= (const alien& rhs) const {   return getstatuspoint(*this) != getstatuspoint(rhs); } 

etc.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -