In a previous c++ language tutorial we looked at C++ overloading of binary operators. In the comment section of this tutorial the user ‘prince’ asked for a binary operator overloading example of greater than. This tutorial is the answer to his question.

## Unary or Binary Operator

First let’s find out if the greater than (>) and less than (<) operators are of the type unary or binary operators.
If you look-up the in the unary and binary operator table you’ll see that the two operators are of the type binary operators.

The binary operators greater than (>) and less than (<) operators are mostly used in if statements, so the source code example below is also using if statements. Let’s look at the operator overloading source code:

``````
#include<iostream>
using namespace std;

class Box {
public:
Box(double boxLength, double boxWidth, double boxHeight)
:length(boxLength), width(boxWidth), height(boxHeight) {}

// Compute the volume of a box.
double volume() const {
return length*width*height;
}

// First inline function
inline bool operator<(const Box& someBox) const {
return volume() < someBox.volume();
}

// Second inline function
inline bool operator<(const double someValue) const {
return volume() < someValue;
}

// Third inline function
inline bool operator>(const Box& someBox) const {
return volume() > someBox.volume();
}

// Fourth inline function
inline bool operator>(const double someValue) const {
return volume() > someValue;
}

private:
double length;
double width;
double height;
};

int main() {
Box myBox(15.0, 10.0, 5.0);
Box myBox2(15.0, 5.0, 5.0);

cout << "The myBox volume is: " << myBox.volume() << "\n";
cout << "The myBox2 volume is: " << myBox2.volume() << "\n";

// Trying the less than binary operator
if(myBox < myBox2) {
cout << "The myBox volume is less than myBox2 volume!\n";
}else{
cout << "The myBox volume is not less than myBox2 volume!\n";
}

// Trying the less than binary operator
if(myBox < 1000) {
cout << "The myBox volume is less than 1000!\n";
}else{
cout << "The myBox volume is not less than 1000!\n";
}

// Trying the greater than binary operator
if(myBox > myBox2) {
cout << "The myBox volume is greater than myBox2 volume!\n";
}else{
cout << "The myBox volume is not greater than myBox2 volume!\n";
}

// Trying the greater than binary operator
if(myBox > 500) {
cout << "The myBox volume is greater than 500!\n";
}else{
cout << "The myBox volume is not greater than 500!\n";
}

return 0;
}

``````

First we declare two boxes (myBox and myBox2) of the type box, each with a specific volume. Then we use the volume function from the Box class to compute the volume of each box and print the volume size on the screen.

The first if statement is using the first inline function of the Box class. The less than operator is used to ask the question if myBox volume is smaller than myBox2 volume.

The second if statement in main is using the second inline function of the Box class. The less than operator is used to ask the question if myBox volume is smaller than 1000.

The third if statement is using the third inline function of the Box class. The greater than operator is used to ask the question if myBox volume is larger than myBox2 volume.

The fourth if statement in main is using the fourth inline function of the Box class. The greater than operator is used to ask the question if myBox volume is larger than 500.

These are just four easy examples of binary operator overloading of (greater than). We hope that this answer the users comment question and that the rest of the users find it also helpful.

That’s all for this tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.

### There are currently 5 responses to “C++ Binary Operator Overloading Greater or Less than”

Why not let us know what you think by adding your own comment!

1. Santana Tessler on December 10th, 2010:

your post really aids, nowadays i get the exact same problems, and i have no idea on how to solve the problem. thankgod i appear yahoo and discovered your post, it assists me get rid of my trouble. thanks once againjust one thing, may i paste your write-up on my weblog? i will add the source and credit to your web page.regards!

2. kunle on May 5th, 2011:

Hi, I have learnt alot from all of these tutorials and believe they are lovely. could you please include an example on how to use #include”filename”. What I meant is how to use you own files with include statement and how to make use of it in the main program. Thanks

3. admin on May 14th, 2011:

@Kunle – Just take a look at the following C++ tutorial : C++ Preprocessor Directives

This tutorial also covers the preprocessor directive #include, which you are looking for.

Hope this helps!

4. poonam on March 5th, 2012:

very nice wbdsite it having all the solutions what ever i want i like it…………:)

5. Nishesh gupta on December 14th, 2012:

Thank you Sir .This post proved very helpful in my assignment..