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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
Carlos Quirarte
CIS 22B
Summer 2015
Assignment C
Problem C3
Description: "The purpose of this program is to apply the following topics: Classes, overloading function names,
abstract data types, friend functions and operator overloading."
*/
#include <iostream> //759
#include <cstring>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car(); //default constructor
Car(const string &, const int &, const string &, const bool &, const string &);
Car(const Car &); //Copy constructor
~Car(); //Destructor
void setUp(string, int, string, bool, string); //Member function
void outPut(); //Member function
Car & operator = (const Car & );
/*{
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return * this;
}*/
};
class StringOfCar
{
private:
Car *ptr;
static const int ARRAY_CARS = 10;
int carCount;
public:
StringOfCar(); //default constructor
StringOfCar(const StringOfCar &); //copy constructor
~StringOfCar(); //destructor
void outPut();
void push(Car &);
void pop(Car &);
};
void input(StringOfCar &);
int main()
{
//1. test the Car= operator
//create a Car object named car1 with the following constants
Car car1("SP", 34567, "business", true, "Salt Lake City");
//create a Car object named car2 using the default constructor
Car car2;
//use the =operator to copy data from car1 to car2
car2 = car1;
//print car2
cout << "TEST 1\n";
car2.outPut();
cout << endl;
//2. test the StringOfCar push function
StringOfCar string1;
input(string1);
cout << "TEST 2\n";
cout << "STRING 1\n";
//3. Test the StringOfCars pop function.
//create a car named car3
Car car3;
//input();
return 0;
}
Car::Car()
{
setUp("", 0, "other", false, "NONE");
}
Car::Car(const Car & )
{
}
Car::Car(const string &mark, const int &number, const string &k, const bool &load, const string &dest)
{
setUp(mark, number, k, load, dest);
}
Car::~Car()
{
//Does nothing.
}
void Car::setUp(string mark, int number, string k, bool load, string dest)
{
reportingMark = mark;
carNumber = number;
kind = k;
loaded = load;
destination = dest;
}
void Car::outPut()
{
cout << setw(4) << right;
cout << "Reporting Mark: " << reportingMark << endl;
cout << "Car Number: " << carNumber << endl;
cout << "Kind: " << kind << endl;
cout << "Loaded: ";
if (loaded == 1)
cout << "true\n";
else
cout << "false\n";
cout << "Destination: " << destination << endl << endl;
}
/* Sets the values in the left hand object from the right hand object */
Car & Car::operator = (const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return * this;
}
//////////////////////////////////////////////////
StringOfCar::StringOfCar()
{
ptr = new Car[ARRAY_CARS];
carCount = 0;
}
StringOfCar::StringOfCar(const StringOfCar & newCar)
{
ptr = new Car[ARRAY_CARS];
}
StringOfCar::~StringOfCar()
{
delete [] ptr;
}
void StringOfCar::push(Car &carPushed)
{
if (carCount < ARRAY_CARS)
{
ptr[carCount] = carPushed;
carCount++;
}
else
{
cout << "The array is full.\n";
exit(1);
}
}
void StringOfCar::pop(Car &carPopped)
{
Car temp;
temp = ptr[carCount];
}
void input(StringOfCar & string1)
{
//local variables
string reportingMark, carType, carLoad;
int carNumber;
string kind;
bool loaded;
string destination;
ifstream inputFile;
inputFile.open("Car.txt");
if (inputFile.good())
{
while (inputFile.peek() != EOF)
{
inputFile >> carType;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kind;
inputFile >> carLoad;
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, destination);
if (carLoad == "true" || carLoad == "True")
loaded = true;
else
loaded = false;
if (carType == "Car")
{
Car temp;
string1.push(temp);
}
}
}
else
{
fprintf(stderr, "error");
exit(1);
}
inputFile.close();
}
|