0

課題:
提供された 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変数をオブジェクトに割り当てるにはどうすればよいですか?

どんな助けでも大歓迎です。ありがとう。

4

3 に答える 3

1

関数では

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

statusPoints後で役に立たない関数ローカル変数です。

私の提案: .cpp ファイルにヘルパー関数を作成します。

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

他の機能で使用します。

bool Alien::operator== (const Alien& rhs) const {
  return getStatusPoint(*this) == getStatusPoint(rhs);
}

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

于 2014-12-01T22:18:41.217 に答える
0

メインまたは Alien オブジェクトをインスタンス化する場所int alienStatPts = alien.getHeight() * alien.getWeight() * 3では、女性の例を宣言してから、別の Alien オブジェクトに対して同じ計算を行い、2 つの整数値を比較できます。呼び出された非メンバー関数getStatPts(Alien alien)でそれを行いif(getStatPts(alien1) < getStatPts(alien2))、比較のために行うことができます。

于 2014-12-01T22:18:53.620 に答える