I'm trying to create some classes that work together via Operator Overloading. I have the main.cpp and the Distance.h files exactly how I want them, and now I just need help properly setting up my Distance.cpp file to link properly and run accurately with them. Here's a look at what I've got so far. Any help and tips would be GREATLY appreciated. Again, the main.cpp and the Distance.h (header file) do not (CANNOT) be changed, so the focus is on the Distance.cpp file. Thanks in advance for any help!!
// ************************************************
// 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.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;
}
// *************************************************
// Distance.cpp
// *************************************************
#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()
{
inches = feet/12;
return inches;
}
Distance::Distance() : feet(0), inches(0)
{}
Distance::Distance(int initialValueInInches)
{
inches = initialValueInInches;
feet = 0;
Normalize();
}
// used for int x = dist1;
Distance::operator int()
{
//???
return 0;
}
// used for dist1 = dist1 + dist2;
Distance Distance::operator + (Distance & d2) const
{
feet += d2.feet; //add the feet
inches += d2.inches //add the inches
if(inches >= 12) //if total exceeds 12.0,
{ //then decrease inches
d2.inches -= 12; //by 12.0 and
d2.feet++; //increase feet by 1
} //return a temporary Distance
return Distance(d2);
}
// 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)
{
}
// 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) ? true : false;
}
// 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) ? true : false;
}
// 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];
}
As you can see, I haven't filled in all the operator void functions yet, but thats also part of my struggle. Let me know what you guys think!!