6

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Lennac
  • 186
  • 1
  • 2
  • 16

3 Answers3

7

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).

Moia
  • 2,216
  • 1
  • 12
  • 34
6

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
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    @ArnavBorborah: OP is under the mistaken impression that `discOne.radius > discTwo.radius` only doesn't work because he is now writing `class` instead of `struct`, and that the dot operator may only call a member _function_ on classes (this ends up being true in practice only when all your data members are private, which presumably has been hammered in on a course of some kind) - I am showing that the struct/class distinction couldn't be of any less relevance. – Lightness Races in Orbit Feb 15 '18 at 16:21
  • @ArnavBorborah No problem! – Lightness Races in Orbit Feb 15 '18 at 16:22
  • So, my goal with the program isn't necessarily to just compare the radii of the two discs. It's more complex, I just didn't want to ask for everything that I'm doing because I was afraid it would defeat the purpose of me actually learning how to solve these problems myself. Thank you very much for the response though. – Lennac Feb 15 '18 at 16:50
0

Simple solution to overload the > operator on objects like below to compare radius of two objects of the class.

discOne > discTwo;

you can overload > according to your requirement, Here is the simple one

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

Above one is one of the solution to solve your problem. There are other ways by which you can compare two objects as suggested by others. Use separate member function( or friend function) to do the same task.

Achal
  • 11,821
  • 2
  • 15
  • 37