課題:
提供された Alien.h ファイルを使用して Alien クラスを実装します。このシナリオでは、エイリアンは身長、体重、性別で説明されます。2 つのエイリアンを比較するには、次の式を使用してエイリアンの statusPoints 値を決定します。ステータス ポイントは、データ メンバーとして保持するのではなく、必要に応じて計算する必要があります。これにより、重みなどの 1 つのデータ メンバーが変更され、ステータス ポイント変数が更新されない、いわゆる古いデータが回避されます。エイリアンを比較するときは、ステータスを使用する必要があります。エイリアンを比較するには、==、!=、>、<、>=、および <= 演算子をオーバーロードする必要があります。したがって、次のようなステートメントを使用できます。
明らかに、エイリアン 1 はエイリアン オブジェクトであり、エイリアン 2 も同様です。また、データ メンバー (身長、体重、性別) が初期化されていると想定されます。
提供される .h ファイルは次のとおりです。繰り返しますが、このファイルは提供されているため、変更できません。
#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
ここに私の Alien.cpp ファイルがあります。
#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; }
}
.h ファイルを変更できない場合、または statusPoints をメンバー関数にすることができない場合、main またはオーバーロードされたオペレーター内のどこに statusPoints 変数を作成すればよいですか? また...比較のためにstatusPoints変数をオブジェクトに割り当てるにはどうすればよいですか?
どんな助けでも大歓迎です。ありがとう。