Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在运算符重载函数中创建、分配新变量并将其与两个对象进行比较。_C++_Operator Overloading_Non Member Functions - Fatal编程技术网

C++ c++;在运算符重载函数中创建、分配新变量并将其与两个对象进行比较。

C++ c++;在运算符重载函数中创建、分配新变量并将其与两个对象进行比较。,c++,operator-overloading,non-member-functions,C++,Operator Overloading,Non Member Functions,作业: 使用提供的Alien.h文件实现Alien类。在这个场景中,外星人是根据身高、体重和性别来描述的。要比较两个外星人,请使用以下等式确定外星人的statusPoints值: 状态点=体重*身高*性别值 其中,如果外国人是男性,性别值为2,如果外国人是女性,性别值为3。状态点应在需要时计算,而不是作为数据成员保留。这避免了所谓的过时数据,其中一个数据成员(如权重)可能会更改,而status points变量不会更新。 比较外国人时应使用身份。 您需要重载==,!=,>,=,还有alien2)

作业:
使用提供的Alien.h文件实现Alien类。在这个场景中,外星人是根据身高、体重和性别来描述的。要比较两个外星人,请使用以下等式确定外星人的statusPoints值: 状态点=体重*身高*性别值 其中,如果外国人是男性,性别值为2,如果外国人是女性,性别值为3。状态点应在需要时计算,而不是作为数据成员保留。这避免了所谓的过时数据,其中一个数据成员(如权重)可能会更改,而status points变量不会更新。 比较外国人时应使用身份。 您需要重载==,!=,>,=,还有alien2){//做点什么}

显然,alien1和alien2都是外星物体。他们也会被假定有他们的数据成员(身高、体重和性别)初始化

这里是提供的.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
\ifndef ALIEN\u H
#定义外星生物
类外星生物
{
公众:
外国人();
外国人(整数h、整数w、字符g);
空隙高度(inth);
空隙净重(int w);
无效设置性别(字符g);
int getHeight();
int getWeight();
char getGender();
//操作员:比较外星人
布尔运算符==(常量外来和外来)常量;
布尔运算符!=(常数外星和外星)常数;
bool操作员(常数外星和外星)常数;
私人:
int高度;//英寸
int-weight;//磅
字符性别;//M或F
};
#恩迪夫
这是我的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; }

}
#包括“Alien.h”
#包括
使用名称空间std;
外星人::外星人()
{
高度=60;
重量=100;
性别='M';
int statusPoints=0;
}
外星人::外星人(inth,intw,charg)
{   
高度=h;
重量=w;
性别=g;
int statusPoints=0;
}
无效外星人::设置高度(inth)
{
高度=h;
}
无效外星人::设定重量(整数w)
{
重量=w;
}
无效外星人::设置性别(字符g)
{
性别=g;
}
int Alien::getHeight()
{
返回高度;
}
int Alien::getWeight()
{
返回重量;
}
char Alien::getGender()
{
返回性别;
}
布尔外星::运算符==(常数外星和外星)常数
{
返回值(高度==alien.height&&weight==alien.weight&&gender==alien.gender);
}
接线员=(外星人和外星人)外星人
{
返回值(高度!=alien.height | weight!=alien.weight | gender!=alien.gender);
}

bool Alien::operator您的赋值表示不应该创建statusPoints变量。你应该在需要的时候计算这个值

因此,在
操作符==
函数中,您可以执行如下操作:

bool Alien::operator== (const Alien& rhs) const {
  return (height * weight * gender == rhs.height * rhs.weight * rhs.gender);
}
然后对其他比较器函数执行类似操作。

在函数中

Alien::Alien(int h, int w, char g)
{   
    height = h;
    weight = w;
    gender = g;
    int statusPoints = 0;
}
bool Alien::operator== (const Alien& rhs) const {
  return getStatusPoint(*this) == getStatusPoint(rhs);
}

bool Alien::operator!= (const Alien& rhs) const {
  return getStatusPoint(*this) != getStatusPoint(rhs);
}
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);
}

等等。

在main中或实例化外来对象的任何地方,您可以为女性声明
int alienStatPts=Alien.getHeight()*Alien.getWeight()*3
,然后对另一个外来对象执行相同的计算,然后比较两个整数值。您可以在名为
getStatPts(Alien-Alien)
的非成员函数中执行此操作,然后执行
if(getStatPts(alien1)
进行比较

我明白你的意思。那么,通过返回它,它会自动将其分配给每个对象?如果是这种情况,我应该在哪里基于'M'或'F'启动genderValue?通过返回它,编译器知道如何执行
If(alien1==alien2)
。然后您将类似地定义一个
操作符>
函数,编译器将知道如何处理
if(alien1>alien2)
。R Sahu建议使用helper函数也是一个很好的方法。我想我只有重载函数可以通过传入参数进行加、减或连接。重载
operator+
是当编译器看到
alien1+alien2
时告诉它做什么的方式。重载
operator==
是当编译器看到
if(alien1==alien2)
时告诉它做什么的方式,这是相同的原理。您返回的是一个布尔值而不是外来对象,因为“这个东西等于另一个东西吗?”是一个正确或错误的问题。@CMorgan,您案例中的.cpp文件。我倾向于使用.cc文件。不在main中?我可以在Alien.cpp中的某个位置创建另一个函数,并从Alien.cpp中调用?没有在.h文件中声明?非常感谢!我真的很感激。这为我打开了一个全新的工具盒。我想我的手被束缚是因为.h的限制。我甚至从未想过要创建一个本地函数。