c ++在Operator Overloaded函数中创建,分配和比较新变量到两个对象。 [英] c++ create, assign and compare a new variable to two object inside an Operator Overloaded function.

查看:114
本文介绍了c ++在Operator Overloaded函数中创建,分配和比较新变量到两个对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作业:

使用提供的Alien.h文件实现Alien类。在这种情况下,外星人根据他/她的身高,体重和性别来描述。要比较两个外星人,可以使用以下公式来确定外星人的statusPoints值:
statusPoints = weight * height * genderValue
其中,如果外星人是男性,genderValue为2,如果外星人为女性,则为3。状态点应在需要时计算,而不保留为数据成员。这避免了所谓的陈旧数据,其中一个数据成员,例如权重可能改变,并且状态点变量不会被更新。
比较外星人时应使用状态。
您需要重载==,!=,>,<> =和< =运算符来比较外星人。因此,你可能有如下语句:
if(alien1> alien2){// do something}

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 }

显然,alien1将是一个Alien对象,所以会alien2。它们也将被假定具有其数据成员(身高,体重和性别)被初始化。

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.

以下是提供的.h文件。再次,我不能改变这个文件,因为它是为我提供。

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

这里是我的Alien.cpp文件。

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; }

}



如果我无法更改.h文件,或使statusPoints成为一个成员函数,我在哪里在重载运算符的main或者里面创建statusPoints变量?另外...如何将statusPoints var分配给对象进行比较?

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> Alien :: Alien(int h,int w,char g)
{
height = h;
weight = w;
gender = g;
int statusPoints = 0;
}

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

statusPoints

我的建议:在.cpp文件中创建一个帮助函数:

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

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);
}

等。

这篇关于c ++在Operator Overloaded函数中创建,分配和比较新变量到两个对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆