operator>() question.

I am writing a program that sorts objects by the variable x. I am not sure how to use the operator> function properly. I was hoping to get some help so I can better understand what I am doing wrong and what knowledge I am lacking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 #ifndef _FINALFLOAT_H
#define _FINALFLOAT_H
#include <iostream>
using namespace std;

class myFloat
{
	float x;
	float y;

	

public:
	myFloat();
	myFloat (float, float);
	void setX(float);
	void setY(float);
	float getX();
	float getY();
	ostream friend & operator <<(ostream &, myFloat);
	bool operator>(myFloat);
};


#endif

#include "FinalFloat.h"

myFloat::myFloat()
{
	x = 0; y = 0;

}

myFloat::myFloat(float a, float b)
{
	x = a; y = b;
}

void myFloat::setX(float a)
{
	x = a;
}

void myFloat::setY(float b)
{
	y = b;
}

float myFloat::getX() {return x;}
float myFloat::getY() {return y;}

ostream & operator<< (ostream &stream, myFloat obj)
{
	stream << " x = " << obj.getX() << "y = " << obj.getY() << endl;
	return stream;
}

bool myFloat::operator>(myFloat obj)
{
	if ( x < obj.x )
		return true;
	if ( x == obj.x || y <= obj.y)
		return true;
	return false;
}

#include "FinalFloat.h"

int main ()
{
	myFloat obj1;
	myFloat obj2(2.2,5.1);

	obj1.setX(5.5);
	obj1.setY(6.4);

	
	obj1.getX();
	obj1.getY();

	cout <<"testing" << endl;

	cout << "obj1 is " << obj1;
	cout << "obj2 is " << obj2;

	obj1.operator>(obj2);

	cout << "obj1 is " << obj1;
	cout << "obj2 is " << obj2;
	
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
class myFloat
{
    float x;
    float y;
       
    public:
        // ...
        bool operator< ( const myFloat& that ) const ;
        bool operator> ( const myFloat& that ) const ;
        // ...
};


1
2
3
4
5
6
7
8
9
10
11
bool myFloat::operator< ( const myFloat& that ) const
{
     if( this->x < that.x ) return true ;
     else if( that.x < this->x ) return false ;
     else return this->y < that.y ; 
}

bool myFloat::operator> ( const myFloat& that ) const
{
    return  that < *this ;
}


Last edited on
That worked great!

I am having some trouble with using the overloaded operator<() to sort five objects based on the variable x that all objects have.

#include "FinalFloat.h"

int main ()
{
myFloat obj1;
myFloat obj2(2.2,5.1);
myFloat obj3(4.2, 11.1);
myFloat obj4(13.45, 1.01);
myFloat obj5(3.32, 8.9);

obj1.setX(5.5);
obj1.setY(6.4);

cout << "obj1 is " << obj1;
cout << "obj2 is " << obj2;
cout << "obj3 is " << obj3;
cout << "obj4 is " << obj4;
cout << "obj5 is " << obj5;



if(obj1<obj2) cout << obj1 << obj2 <<endl;
else cout << obj2 << obj1 << endl;
if(obj3<obj2) cout << obj3 << endl;




return 0;
}

I think a for loop would be easier but I am not sure how to use one in this instance.
Last edited on
Topic archived. No new replies allowed.