Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing data between two objects of a class

Tags:

c++

So, I've created a class and then construct two separate instances of that class:

disc discOne; // Construct objects
disc discTwo;

The declaration of the class is done separately through a 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 and figure it out.

The problem I'm running in to is that I've used structures in the past, which (if this was the case), I would simply go:

discOne.radius > discTwo.radius

Or something similar. But, that syntax for classes calls a function tied to that class. Sorry for the rambling, but I'm having trouble articulating it - probably why I've struggled to find any guidance through my own searches on the internet.

like image 569
Lennac Avatar asked Feb 15 '18 16:02

Lennac


People also ask

How do you compare two objects in a class?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

Can you compare two objects of the same class?

This can occur through simple assignment, as shown in the following example. Value equality means that two objects contain the same value or values. For primitive value types such as int or bool, tests for value equality are straightforward.

How do you compare two objects in a set?

The equals() method of java. util. Set class is used to verify the equality of an Object with a Set and compare them. The method returns true if the size of both the sets are equal and both contain the same elements.

How do you compare data in an object?

Usually, when you compare data types like int and strings in JavaScript, you use the equality operators ( == and === ). However, comparing objects with == and === will not work. To fix this, one option is to stringify both objects and then use the equality operators.


2 Answers

Three ways:

  1. Use a getter method getRadius() const {return radius;} and just compare as you always did.

  2. Use overload for operator< and operator> if you want to compare two objects directly (but this doesn't seems the right case.

  3. Declare a friend function bool compareRadius(const Disc& discOne, const Disc& discTwo) to perform the comparison without involving directly the objects.

Of course the simpliest way is the first. I just showed some other option you could consider for similar problems.

Edit: Answer #3 is based on Scott Meyer's Effective C++, item 23 (though it specifies non-friend).

like image 158
Moia Avatar answered Nov 15 '22 05:11

Moia


You could add a "getter" to your class (like short getRadius() const) through which you can obtain a value to compare: discOne.getRadius() < discTwo.getRadius().

Alternatively you could add a operator< overload to disc itself, and have it perform the comparison between radii. However, this only makes sense if the radius is the only property of the disc (which it isn't — you have location also), and if comparing radii is equivalent to comparing discs (and I'm not convinced that this would be logical).

Beyond that there are all sorts of clumsy solutions, like adding a bool radiusIsLesserThatThisOtherDiscsRadius(const disc& otherDisc) const member function.

But, that syntax for classes calls a function tied to that class

Actually, that's not true; C++ does not have "structures", so struct introduces a class too, and discOne.radius > discTwo.radius would have worked just fine here if radius were not a private data member. But it being a private data member is appropriate.

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
like image 25
Lightness Races in Orbit Avatar answered Nov 15 '22 07:11

Lightness Races in Orbit