// lab5.cpp : Defines the entry point for the console application. // How can I sort the array object with its distance? I am not so clear on how to use the assignment operator function either... any help would be rally appreciated..
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Leg {
public:
Leg(const char *nameS, const char *nameE,const double totalDis): nameOfstartingCity(nameS), nameOfendingCity(nameE), totalDistance(totalDis) {};
double getDistance(double);
void outPut(ostream &);
Leg &operator=(const Leg&);
private:
const char *nameOfstartingCity;
const char *nameOfendingCity;
const double totalDistance;
};
int main()
{
int arraySize = 0;
const int SIZEOFARRAY = 11;
Leg myArray[SIZEOFARRAY] = { Leg("Pinole", "Richmond", 3),
Leg("Redwood City", "Palo Alto", 21.6),
Leg("Berkeley", "Oakland", 4.3),
Leg("Concord", "Walnet Creek", 8.3),
Leg("San Francisco", "Oakland", 23),
Leg("South San Francisco", "San Mateo",15.3),
Leg("Palo Alto", "Sunnyvale", 11.2),
Leg("San Jose", "Milpitas", 15.2) ,
Leg("Fremont", "Union City", 16.3) ,
Leg("San Leandro", "Piedmont", 19.4),
Leg("Pinole", "San Jose", 40.1) };
arraySize = sizeof(myArray) / sizeof(myArray[0]);
for (int x = 0; x < arraySize; x++) {
for (int j = x + 1; j < arraySize; j++)
if (myArray[j].getDistance < myArray[x].getDistance)
swap(myArray[x], myArray[j]);
}
for (int i = 0; i < arraySize; i++)
{
myArray[i].outPut(cout);
}
cout << "\n\n\n\n" << endl;
system("PAUSE");
return 0;
}
double Leg::getDistance(double x)
{
x = totalDistance;
cout << x << " From getDistance " << endl;
return x;
}
void Leg::outPut(ostream &x)
{
x << "Leg : " << nameOfstartingCity << " to " << nameOfendingCity <<
", " << totalDistance << " miles." << endl;
}
Leg& Leg::operator=(const Leg ©This)
{
nameOfstartingCity = copyThis.nameOfstartingCity;
nameOfendingCity = copyThis.nameOfendingCity;
return *this;
}