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.
In the function
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:
and use it in other functions.
etc.