Compare objects

I have this class that saves the hours :

class clockType
{
private:
int h; // hours
int m; // minutes
int s; // seconds
public:
void getTime(int&, int&, int&);
}

I should creat 3 functions that compare 2 objects: 1)Function in the class 2)Function with operator in the class 3)Function out of the class

Can anyone help me ?

Can anyone help me ?

Yeah we can but we won't.

We will help after you post some code what you've tried and came up yourself :)

1) clockType compare(clockType a, clockType b)
{
if(a.h>b.h)
return a;
else
return b;
if(a.h==b.h && a.m>b.m)
return a;
else
return b;
if(a.h==b.h && a.m==b.m && a.s>b.s)
return a;
else
return b;

}
Do you have something more quickly ??

For the second i think i need a bool operator >(clockType a) but i am not sure...

For the last i have no idea
The third should be something with template...

template <class X> X compare3(X a, X b)

Am i wrong ??
3) (Your code is solution to 3rd problem)
Use ternaries or std::tie:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Usual convention is to return if left operand is less than right
//use const references as parameters
bool compare_less(const clockType& lhs, const clockType& rhs) 
{
    return lhs.h != rhs.h ? lhs.h < rhs.h :
           lhs.m != rhs.m ? lhs.m < rhs.m :
                            lhs.s < rhs.s ;
}

//OR
#include <tuple>

bool compare_less(const clockType& lhs, const clockType& rhs) 
{
    return std::tie(lhs.h, lhs.m, lhs.s) < std::tie(rhs.h, rhs.m, rhs.s);
}


2) Yes, you need to create bool operator <(const clockType& a) — it is usual convention to define < operator first and define others through it.
If you are allowed, you can just delegate call to compare_less funtion.

1) function signature should be bool clockType::compare_less(const clockType& other) const
You can delegate call there too.
Last edited on
Sorry but your code seems very complicated to me, i am a beginner... i need something simpler.

Can you be more specific ?

Also in the 3rd which is out of the class, how you use h,m,s ? They are private...
Ok, implementation of (1) (to respect privateness) without ternary operators (which is close to your code actually)

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
class clockType
{
  private:
    int h; // hours
    int m; // minutes
    int s; // seconds
  public:
    void getTime(int&, int&, int&);
    bool compare_less(const clockType& other) const 
}

bool clockType::compare_less(const clockType& other) const
{
         if(this->h != other.h) return this->h < other.h;
    else if(this->m != other.m) return this->m < other.m;
    else                        return this->s < other.s;
}

//Which is same as
bool clockType::compare_less(const clockType& other) const
{
    return this->h != other.h ? this->h < other.h :
           this->m != other.m ? this->m < other.m :
                                this->s < other.s ;
}


2 and 3 should just call lhs.compare_less(rhs) and return result
Last edited on
In this case the 2nd and 3rd functions use the 1st... i think that the exercise asks to creat 3 different ways to compare 2 objects.

Do you agree with this:

class clockType
{
private:
int h;
int m;
int s;
public:
clockType(int h,int m,int s)
{
this->h=h;
this->m=m;
this->s=s;

}
void getTime(int&, int &, int&);

bool compare1(clockType &other)
{
if(this->h != other.h)
return this->h < other.h;
else if(this->m != other.m)
return this->m < other.m;
else
return this->s < other.s;

}

bool operator<(clockType &a)
{
a.compare1(a);
}
};


bool compare3(clockType &a)
{
a.compare1(a);
}
You need to return value of your function call from operator and compare3.

And I would prefer to have non-member operator, but it is just a preference in your case.


It is common in programming to call other functions implementing already existing functionality, as it helps to avoid code duplication and makes code easier to maintain.

In case if you want to implement comparison in each function: to compare two objects you need access to h,m and s variables, but ou cannot access them as they are private. So you have to delegate actual comparison to class facilities. This is encapsulation.
Ok i think i understood, thank you very much !! :)
Topic archived. No new replies allowed.