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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sstream>
#include <fstream>
using namespace std;
class Car{
//set data of the class as private
private:
string reportingMark; // string variable to store car member reportingMark
int carNumber; // int variable to store car member carNumber
string kind; // string variable to store car member kind
bool loaded; // boolean variable to store car member loaded
string destination; // string variable to store car member destination
public:
//Constructors
// Default constructor
Car()
{
reportingMark = ""; // an empty string
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
// Copy constructor
Car(const Car &obj)
{
reportingMark = obj.reportingMark;
carNumber = obj.carNumber;
kind = obj.kind;
loaded = obj.loaded;
destination = obj.destination;
}
// A constructor that accepts five constant reference parameters and calls setUp member function
Car(string mark, int num, string make, bool state, string dest);
// destructor
~Car()
{
};
// mutator member functions
void setReportingMark(string mark);
void setCarNumber(int num);
void setKind(string make);
void setLoaded(bool state);
void setDestination(string dest);
// accessor member functions
string getReportingMark() const;
int getCarNumber() const;
string getKind() const;
bool getLoaded() const;
string getDestination() const;
// member functions
void setUp(string, int, string, bool, string, Car);
void output(Car);
// Car operator=
Car &operator=(const Car &carB);
};
//function definition
void Car::setReportingMark(string mark)
{
reportingMark = mark;
};
void Car::setCarNumber(int num)
{
carNumber = num;
};
void Car::setKind(string make)
{
kind = make;
};
void Car::setLoaded(bool state)
{
loaded = state;
};
void Car::setDestination(string dest)
{
destination = dest;
};
string Car::getReportingMark() const
{
return reportingMark;
};
int Car::getCarNumber() const
{
return carNumber;
};
string Car::getKind() const
{
return kind;
};
bool Car::getLoaded() const
{
return loaded;
};
string Car::getDestination() const
{
return destination;
};
// Car operator= **************************************************
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber = carB.carNumber;
kind = carB.kind;
loaded = carB.loaded;
destination = carB.destination;
return * this;
}
// function prototypes
void input ();
void output (Car);
void setUp (string, int, string, bool, string, Car&);
class StringOfCars{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE];
int currentCar;
// default constructor
StringOfCars();
// copy constructor
StringOfCars(const Car &);
// destructor
~StringOfCars();
// push function
// pop function
// output function
};
/* ********** StringOfCars member functions ********** */
// default constructor
StringOfCars()
{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE];
int curNum = 0;
}
// copy constructor
StringOfCars(const StringOfCars &obj)
{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE] = new object[ARRAY_MAX_SIZE];
}
// destructor
~StringOfCars()
int main()
{
// call input function to read the data from user
input();
return 0;
}
/* ******************** output ********************
print all data
*/
void output(Car car)
{
cout << "ReportingMark:\t ";
// convert string to character array
char u;
char str[(car.getReportingMark()).length()];
strcpy(str, (car.getReportingMark()).c_str());
// change the lowercase to uppercase letter by letter
for(int n = 0; n < (car.getReportingMark()).length(); n++)
{
u = str[n];
putchar (toupper(u));
}
cout << endl;
cout << "CarNumber:\t " << car.getCarNumber() << endl;
cout << "Kind:\t\t " << car.getKind() << endl;
if(car.getLoaded() == true)
{
cout << "Loaded:\t\t true\n";
cout << "Destination:\t " << car.getDestination() << endl;
}
else if(car.getLoaded() == false)
{
cout << "Loaded:\t\t flase\n";
if(car.getDestination() == "none" || car.getDestination() == "NONE")
cout << "Destination:\t NONE\n";
else
cout << "Destination:\t " << car.getDestination() << endl;
}
cout << endl;
}
/* ******************** setUp ********************
pass data of variables got from input function to variables declared in class car
*/
void setUp(string mark, int num, string make, bool state, string dest, Car &car1)
{
// call the mutator member functions to pass the data to the class
car1.setReportingMark(mark);
car1.setCarNumber(num);
car1.setKind(make);
car1.setLoaded(state);
car1.setDestination(dest);
};
/* ******************** input ********************
read data from a file
*/
void input()
{
// define variables
string mark;
int num;
string make;
bool state;
string dest;
// declare the string "Type" to get the first column of the data out of the way
string Type;
// declare a string for reading boolean type data
string st;
// declare and open the file
ifstream inputFile;
inputFile.open("inputFile.txt");
// declare a Car object
Car temp;
// print error message and exit the program if the file does not exist
if(!inputFile) // if the file does not exist
{
fputs("Fail to open the file", stderr);
exit(0); // exit the program
}
else
{
// use while loop to read the file line by line;
// the peek function returns EOF is no next character exists
while(inputFile.peek() != EOF)
{
// read data from file
inputFile >> Type;
inputFile >> mark;
inputFile >> num;
inputFile >> make;
// declare what false and true readings from the file means
inputFile >> st;
if(st == "false")
state = false;
else
state = true;
// get rid of the leading whitespace before using getline function to read the destination
while(inputFile.peek() == ' ')
{
inputFile.get();
}
getline(inputFile, dest);
// call the setUp function to pass the data to the class
setUp(mark,num,make,state,dest,temp);
// call the output function to print the data
output(temp);
}
}
// close the file
inputFile.close();
cout << endl;
}
|