Comparing data between two class objects

So, I created a class, and then create two separate instances of this class:

disc discOne; // Construct objects
disc discTwo;

The class declaration is executed separately through the header file:

class disc
{
public:
    disc();
    ~disc();
    void changeRadius(short);
    void throwDisc(short, short);
    void printLocation() const;
    void printInfo() const;
private:
    short radius;
    short xlocation;
    short ylocation;
};

I can use the printInfo () and changeRadius () functions, for example, but how can I compare (for example) the radius between these two objects? I want to do something more complex than this, but if I understand the basics, I want to try to understand this.

The problem I ran into is that I used structures in the past that (if that were the case), I would just go:

discOne.radius > discTwo.radius

- . , . , - , - .

+6
3

"getter" (, short getRadius() const), : discOne.getRadius() < discTwo.getRadius().

operator< disc . , ( ), ( , ).

, , bool radiusIsLesserThatThisOtherDiscsRadius(const disc& otherDisc) const.

,

, ; ++ "", struct , discOne.radius > discTwo.radius , radius . .

struct disc
{
public:
    disc();
    ~disc();
    void changeRadius(short);
    void throwDisc(short, short);
    void printLocation() const;
    void printInfo() const;
private:
    short radius;
    short xlocation;
    short ylocation;
};

// ^ Exactly the same thing; your approach still won't work, for the same reason
+6

:

  • getter getRadius() const {return radius;} , .

  • operator< operator>, ( .

  • bool compareRadius(const Disc& discOne, const Disc& discTwo) .

, - . , .

: №3 ++, 23 ( ).

+7

> objects, , .

discOne > discTwo;

> .

    bool disc :: operator > (disc my_disc)
    {
            if(radius > my_disc.radius)
                    return true;
            else
                    return false;
    }

The above is one of the solutions to solve your problem. There are other ways in which you can compare two objects, as suggested by others. Use a separate function member(or friend) to accomplish the same task.

0
source

All Articles