# C++ Operator Overloading HELP!



## balla4eva33 (Nov 2, 2007)

I've got some code that I'm having a hard time getting done. I don't want to (CAN'T) change the header (Distance.h) or main.cpp files, but I need to finish what's left and edit what's wrong in my Distance.cpp file. If anyone could help me with it, specifically the *Operator +* function, that would be awesome! Thanks in advance. Here's what I've got so far:


HEADER:



```
// ************************************************
// Distance.h
// ************************************************
#include <iostream>
using namespace std;


class Distance                    //English Distance class
{
private:
    int feet;
    int inches;

    // Normalize adjusts feet and inches if inches is >= 12;
    void Normalize();

    // utility function to return Distance in inches
    int sizeInInches();
    
public:
    
    // Constructors
    Distance();  // default constructor setting feet=0 and inches=0
    Distance(int initialValueInInches);

    // used for int x = dist1;
    operator int();
    
    // used for dist1 = dist1 + dist2;
    Distance operator + (Distance & d2) const;
	//EXAMP: Distance operator + (Distance) const;

    // used for dist1 += dist2;
    void operator += (Distance & d2);

    // used for dist1 += 22;
    void operator += (int value);

    // used for dist1 = dist2
    void operator = (Distance & d2);

    // used for dist1 = 22
    void operator = (int value);

    // used for if (dist1 == dist2) ...
    bool operator == (Distance & d2)  ;

    // used for if (dist1 < dist2) ...
    bool operator < (Distance & d2) ;

    
    // **************************************

    void getdist()              //get length from user
    {
        cout << "\n   Enter feet: ";  cin >> feet;
        cout << "   Enter inches: ";  cin >> inches;
    } 
    void showdist()
    {
        cout << " feet=" << feet << " inches=" << inches;
    }
};

class ArrDistance
{
    Distance * arr;
    int arrSize;
public:
    // Constructor creates an Array of Distance classes the 
    // size specified in size.  
    ArrDistance(int size);

    // Make sure you free up your dynamic allocation
    ~ArrDistance();



    // This operator should do a sanity check on the index
    Distance & operator [](int index);
};
```

MAIN:


```
// *************************************************
// Main.cpp
// *************************************************
#include <iostream>
using namespace std;
#include "Distance.h"



int main()
{
  Distance d1(20), d2(17), d3;

  d3 = d1 + d2; //<----

  cout << "d1="; d1.showdist();  cout << endl;
  cout << "d2="; d2.showdist();  cout << endl;
  cout << "d3="; d3.showdist();  cout << endl;

  Distance d4(13);
  d3 += d4;
  cout << "d3="; d3.showdist(); cout << endl;

  d3 += 22;
  cout << "d3="; d3.showdist(); cout << endl;

  d3 = d4; //<----
  cout << "d3="; d3.showdist(); cout << endl;

  d3 = 22; //<----
  cout << "d3="; d3.showdist(); cout << endl;
  

  ArrDistance darr(10);
  for (int i=0; i <= 10; i++)
  {
      darr[i] = 13 +i;
      cout << "darr["<<i<<"]="; darr[i].showdist(); cout << endl;
      if (d2 == darr[i]) 
          cout << " Equal to d2 " << endl;
      
      if (darr[i] < d2) 
          cout << " Less than d2 " << endl;
  }

   return 0;
}
```










Then the trouble maker,
Distance.cpp:



```
//LOOK AT FRACTION WITH OPERATOR OVERLOADING
//ON COMMUNICATIONS BOARD FOR HELP AND EXAMPS


#include <iostream>
using namespace std;
#include "Distance.h"



// Normalize adjusts feet and inches if inches is >= 12;
void Distance::Normalize()
{
	if(inches >= 12)
	{
		inches -= 12;
		feet++;
	} 
}

// utility function to return Distance in inches
int Distance::sizeInInches()
{
	int result;
	result = feet * 12;
	result += inches;
	return result;
}

    
Distance::Distance() : feet(0), inches(0)
{} 



Distance::Distance(int initialValueInInches)
{
	inches = initialValueInInches;
	feet = 0;
	Normalize();
}


// used for int x = dist1;
Distance::operator int()
{
	Normalize();
	return sizeInInches();
}
    

// used for dist1 = dist1 + dist2;
Distance Distance::operator + (Distance & d2) const
{
	//d1 = d2 + d3;
	//make a new return value as a copy of d2, then add d3 to it, and then return it.
	Distance d3;
	int f = feet + d2.feet;
	int i = inches + d2.inches;
	if(i >= 12)
	{
		i -= 12;
		f++;
	}
	d3.feet = f;
	d3.inches = i;
	return d3;
}


// used for dist1 += dist2;
void Distance::operator += (Distance & d2)
{
	feet += d2.feet;
	inches += d2.inches;
	Normalize();  
}


// used for dist1 += 22;
void Distance::operator += (int value)
{
	inches += value;
	Normalize();
}



// used for dist1 = dist2
void Distance::operator = (Distance & d2)
{}


// used for dist1 = 22
void Distance::operator = (int value)
{}


// used for if (dist1 == dist2) ...
bool Distance::operator == (Distance & d2)
{
	int b1 = feet + inches/12;
	int b2 = d2.feet + d2.inches/12;
	return (b1 == b2);
}



// used for if (dist1 < dist2) ...
bool Distance::operator < (Distance & d2)
{
	int b1 = feet + inches/12;
	int b2 = d2.feet + d2.inches/12;
	return (b1 < b2);
}








    // Constructor creates an Array of Distance classes the 
    // size specified in size.  
ArrDistance::ArrDistance(int size)
{
	arrSize = size;
	arr = new Distance[arrSize];
}


    // Make sure you free up your dynamic allocation
ArrDistance::~ArrDistance()
{
	delete [] arr;
}




    // This operator should do a sanity check on the index
Distance & ArrDistance::operator [](int index)
{
	if(index < 0 || index >= arrSize)
	{
		cout << "\nIndex out of bounds";
		exit(1);
	}
	return arr[index];
}
```


----------



## balla4eva33 (Nov 2, 2007)

Nevermind, I figured it out!


----------

