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

女生的网名这么多〃 提交于 2020-01-03 05:16:19

问题


The assignment:
Implement an Alien class using a provided Alien.h file. An alien, in this scenario is described in terms of his/her height, weight, and gender. To compare two aliens, you use the following equation to determine the alien’s statusPoints value: statusPoints = weight * height * genderValue where genderValue is 2 if the alien is male, and 3 if the alien is female. The status points should be calculated when needed, not kept as a data member. This avoids so-called stale data, in which one data member, such as the weight might change and the status points variable wouldn’t get updated. Status should be used when comparing the aliens. You need to overload the ==, !=, >, <, >=, and <= operators to compare aliens. Thus, you might have statements such as the following: if(alien1 > alien2) { //do something }

Obviously, alien1 would be an Alien object, and so would alien2. They would also be assumed to have their data members (height, weight, and gender) initialized.

Here is the provided .h file. Again, I cannot alter this file as it is provided for 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 the 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 is my 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 I can't alter the .h file, or make statusPoints a member function, where do I create the statusPoints variable in main or inside the overloaded operator? Also... How do I assign the statusPoints var to the object for comparison?

Any help is appreciated. Thanks.


回答1:


In the function

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

statusPoints is a function local variable that is not going to be useful for anything later.

My suggestion: create a helper function in the .cpp file:

static int getStatusPoint(Alien const& alien)
{
    return alien.getHeight()*alien.getWeight()*aliean.getGender();
}

and use it 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.




回答2:


Your assignment says you aren't supposed to create a statusPoints variable. You're supposed to compute that value when it's needed.

So in your operator== function, you would do something like:

bool Alien::operator== (const Alien& rhs) const {
  return (height * weight * gender == rhs.height * rhs.weight * rhs.gender);
}

And then something similar for the other comparator functions.




回答3:


In main or wherever you instantiate an Alien object you can declare int alienStatPts = alien.getHeight() * alien.getWeight() * 3 for an example for a female and then do the same calculation for another Alien object and then compare the two integer values. You can do that in a non member function called getStatPts(Alien alien) and then do if(getStatPts(alien1) < getStatPts(alien2)) for the comparison.



来源:https://stackoverflow.com/questions/27238216/c-create-assign-and-compare-a-new-variable-to-two-object-inside-an-operator-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!