0

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.

C Morgan
  • 3
  • 1
  • 6

3 Answers3

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.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • @CMorgan, the .cpp file in your case. I tend to use .cc files. – R Sahu Dec 01 '14 at 22:27
  • Not inside main? I can create another function somewhere in the Alien.cpp and call from inside the Alien.cpp? without declaring it in the .h file? – C Morgan Dec 01 '14 at 22:29
  • Thanks alot! I really appreciate it. That opens up a whole new box of tools for me. I thought my hands were tied because of the .h limitations. It never even crossed my mind to create a local function. – C Morgan Dec 01 '14 at 22:32
0

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.

A. Duff
  • 3,576
  • 4
  • 33
  • 63
  • I see what you're saying. So by returning this it will automatically assign it to each object? If that's the case, where should I be initiating the genderValue based on 'M' or 'F'? – C Morgan Dec 01 '14 at 22:16
  • By returning this, the compiler knows how to do `if(alien1 == alien2)`. Then you would similarly define an `operator>` function, and the compiler would know how to handle `if(alien1 > alien2)`. R Sahu's suggestion to use a helper function is also a good one. – A. Duff Dec 01 '14 at 22:19
  • I guess I have only overloaded functions to add, subtract or concatenate by passing in parameters. I'm not fully wrapping my head around the comparison. – C Morgan Dec 01 '14 at 22:28
  • Overloading `operator+` is how you tell the compiler what to do when it sees `alien1 + alien2` Overloading `operator==` is how you tell the compiler what to do when it sees `if(alien1 == alien2)` It's the same principal. You're returning a boolean value instead of an Alien object because "Is this thing equal to this other thing?" is a true or false question. – A. Duff Dec 01 '14 at 22:32
0

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.

alacy
  • 4,462
  • 5
  • 25
  • 45