Operator Overloading help

Hello everyone,

I m new in C++, and I want to improve myself but I can not understand it.

I create a class
PersonHeight{
int meter=0; // I hold the meter value
int centimeter=0; // I hold the centimeter value ( do I need to create constructor & destructor? )
}
// get/set methods for the my data members

void get m_value() {
return meter;}
void get c_value() {
return centimeter;}
void set m_value(int m) {
meter=m;}
void set m_value(int c) {
centimeter=c;}

how can overload - operator for the difference of two instances of PersonHeight can be calculated and overload > operator to compute if a PersonHeight object is greater than another one.

Also how can I test my functions in the main? is it with the - and > operands?
Could you help me?

Thank you...
you have to learn lot more in c++, it good you started
your class will be

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
class CPersonHeight
{
private:
          int meter; // I hold the meter value
          int centimeter;
public:
        CPersonHeight() {  meter = 0 ; centimeter = 0 ; }
       ~CPersonHeight() { } 
         void get m_value();
         void get c_value();
         void set m_value(int m);
         void set m_value(int c);
}


void CPersonHeight::get m_value() 
{
  return meter;
}
void CPersonHeight::get c_value() 
{
    return centimeter;
}
void CPersonHeight::set m_value(int m) 
{
      meter=m;
}
void CPersonHeight::set m_value(int c) 
{
  centimeter=c;
}
Thank you for your reply.

Could you give me an example with the operator overloading? For example, overloading + operator for the difference of two instances of PersonHeight can be calculated
Something like:
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
#include <iostream>

class CPersonHeight
{
  private:
    int meter;
    int centimeter;
  public:
    CPersonHeight() {  meter = 0 ; centimeter = 0 ; }
   ~CPersonHeight() { }
    int get_meter() { return meter; }
    int get_centimeter() { return centimeter; }
    void set_meter(int m) { meter = m; }
    void set_centimeter(int c) { centimeter = c; }
    const CPersonHeight &operator=(const CPersonHeight &rhs)
    {
      if(this != &rhs) // don't bother setting a copy of yourself
      {
        this->meter = rhs.meter;
        this->centimeter = rhs.centimeter;
      }
      return *this;
    }
    const CPersonHeight &operator+(const CPersonHeight &rhs)
    {
      CPersonHeight retval;
      retval.meter = rhs.meter + this->meter;
      retval.centimeter = rhs.centimeter + this->centimeter;
      return retval;
    }
};

int main()
{
  CPersonHeight cph1, cph2, cph3;
  cph1.set_meter(2);
  cph1.set_centimeter(6);
  cph2.set_meter(1);
  cph2.set_centimeter(4);
  cph3 = cph1 + cph2;
  std::cout << "CPH3.meter = " << cph3.get_meter() << ", CPH3.centimeter = " << cph3.get_centimeter() << std::endl;
  return 0;
}


Program output was: CPH3.meter = 3, CPH3.centimeter = 10
Last edited on
first of all some fixing and improvements:
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
class CPersonHeight
{
private:
          double meter; // I hold the meter value
          int centimeter;
public:
        CPersonHeight() {  meter = 0 ; centimeter = 0 ; }
       ~CPersonHeight() { } 
       CPersonHeight(double a, char b)
       {
             if (b=='c')
             {
                centimeter=int(a);
                meter=a/100.0;
                }
             else
             {
                 meter=a;
                 centimeter=int(meter*100);
                 }
             }
         double get_m_value();
         int get_c_value();
         void set_m_value(int m);
         void set_c_value(int c);
};


double CPersonHeight::get_m_value() 
{
        return meter;
}
int CPersonHeight::get_c_value() 
{
        return centimeter;
}
void CPersonHeight::set_m_value(int m) 
{
        meter=m;
}
void CPersonHeight::set_c_value(int c) 
{
        centimeter=c;
}

and then you delcare the operator:
1
2
3
4
int operator- (CPersonHeight a, CPersonHeight b)
{
         return a.get_c_value()-b.get_c_value();
}

that way you can also make a + operator, or = operato, +=, -=, basicly anything you vant, just by using that operator on the get_c_value() function.
@viliml

The overload operators such as + , - , * , += , -= , *=
should return an object of the class itself!
But if it is a special case, than I have said nothing :-)

And in all cases the overload function must take object of kind const className& ob , const reference.
Last edited on
Thank you for your reply.

But i didnt understand why we did;
1
2
3
4
5
6
7
8
9
const CPersonHeight &operator=(const CPersonHeight &rhs)
    {
      if(this != &rhs) // don't bother setting a copy of yourself
      {
        this->meter = rhs.meter;
        this->centimeter = rhs.centimeter;
      }
      return *this;
    }
I'll try to explain it as simple as possible,
so when you overload = operator you say the operator =, hey you
take two arguments,
the first one is the current object itself, the object that stands at the left side of you ( targetObject= )
the second one, the other object, the object that stands at the right side of you ( =sourceObject )
For instance
I am an object being at the left side ( a )= (b ) I am other object being at the right side
or

targetObejct = sourceObject

so you says the operator , dont change the other object(source), that is the reason you write const CPersonHeight &rh, const tells the function you cant change me

and you tell the overload operator= to send a reference, for this reason you define the return type as CPersonHeight&

hope it helps

Last edited on
thank you so much :)
Hello again,

I m trying to write it.
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
#include <iostream>
using namespace std;

class CPersonHeight
{
  private:
    int meter;
    int centimeter;
  public:
    CPersonHeight() {  meter = 0 ; centimeter = 0 ; }
    CPersonHeight(int m, int c) {  meter = m+c/100 ; centimeter = c%100 ; }
   ~CPersonHeight() { }
   
    int get_meter() { return meter; }
    int get_centimeter() { return centimeter; }
    void set_meter(int m) { meter = m; }
    void set_centimeter(int c) { centimeter = c; }
   
   CPersonHeight operator- (const CPersonHeight &x)
   {
               int m,c;
               m=x.meter-meter;
               centimeter=x.centimeter-centimeter;
               CPersonHeight difference(m,c);
               return difference;
               }

void print_difference()
{
     cout<<"The difference is:"<<meter << "m" <<centimeter<< "cm"<< endl;
}
};

 CPersonHeight:: operator> (const CPersonHeight & x)
{ 
      int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
if(x1>x2)
// what can ı do there? also what can ı do in main?
//my operator is compuing if a CPersonHeight object is greater than another one.
}
 CPersonHeight:: operator== (const CPersonHeight & x)
{ 
      int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
if(x1==x2)
// what can ı do there? also what can ı do in main?
//operator to compute whether two CPersonHeight objects are equal or not. is it acceptable that writing bool func?
}

int main()
{
      CPersonHeight h1(1,80);
      CPersonHeight h2(1,50);
      CPersonHeight h3= h1-h2;
      h1.print_difference();
      system("pause");
      return 0;
}


I wrote this ones but I need a help, I wrote as a comment. Could you help me? Thank you so much.
Hi

operator such as < , > , == , != should be declare as class methods, they may be declared as friend or normal function as well. However I do it as class method.

Each overload operator( < , > , == , != ) should return a type of bool, either true or false

so in your case
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
class CPersonHeight
{

//do some thing
// do other things as usual 

// now declare your overload operator

       bool operator>(const CPersonHeight& x);
       bool operator<(const CPersonHeight& x);
       bool operator==(const CPersonHeight& x);
       bool operator!=(const CPersonHeight& x);
 

};

//now you can define them here
// for example I will take your implemnetation


bool CPersonHeight:: operator== (const CPersonHeight & x)
{ 
      int x1, x2;
x2=x.meter*100+x.centimeter;
x1=meter*100+centimeter;
 return x1==x2 ;

}


bool CPersonHeight:: operator!= (const CPersonHeight & x)
{ 
   
 return !(*this ==x) ;

}



and in your main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int main()
{
      CPersonHeight h1(1,80);
      CPersonHeight h2(1,50);
      CPersonHeight h3= h1-h2;
      h1.print_difference();
    
  if( h1 == h2 ){
   // do some thing  
   }
   else if( h1 < h1 ){
    // do other things
    }
   
  if( h3 != ( h1 - h2 ) ){
 // ooops no my overloaded operator - has some problems
 } 
      system("pause");
      return 0;
}


hope it helps
Last edited on
Thank you for your help. :)
Topic archived. No new replies allowed.