Question: Solve using C++ Code Use your program 6 Lab exercise, in which you declared an array of class objects, and obtained data for them from
Solve using C++ Code
Use your program 6 Lab exercise, in which you declared an array of class objects, and obtained data for them from an input file. For this lab, you will extend this program to sort the array of class objects acording to their ID data member value.
Modify a sorting function to handle an array of objects (instead of an int array.) After your program does input, call the sort function. Finally, do an output loop to print the characteristics of all the objects, so you can see that they have been sorted.
Source code::
#include
#include
using namespace std;
class carType
{
private:
int mileage;
int year;
double cost;
string color;
string style;
int ID;
public:
void setAll(int m, int y, double c, string col, string s, int i);
void print()const;
void getID(ifstream&inf);
void readInput(carType c[], ifstream& inf, int& size);
};
void readInput(carType c[], ifstream& inf, int& size)
{
int mileage, year, id;
double price;
string color, style;
int index = 0;
char discard;
inf >> id;
while (inf)
{
c[index].setAll(mileage, year, price, color, style, id);
index++; // allows successive indexes to be read/ increments index. allows while test to conclude
inf >> mileage >> year >> price;
inf.get(discard); //inf.ignore(200, ' '); bypassing the newline
getline(inf, color);
getline(inf, style);
inf >> id;
}
size = index;
}
void carType::print()const // print function
{
cout << "Car ID: " << ID;
cout << "Mileage: " << mileage;
cout << "Year: " << year;
cout << " Cost: " << cost;
cout << " Color: " << color;
cout << "Style " << style;
}
void carType::setAll(int m, int y, double c, string col, string s, int i) //define setAll within the class
{
if (m < 0)
mileage = 0; // use default value for negative parameter
else
mileage = m;
}
void carType::getID(ifstream &inf)
{
}
___
int main()
{
carType cars[500];
int numCars;
int mileage, year, id;
double price;
string color, style;
ifstream infile;
readInput(cars, infile, numCars);
for (int i = 0; i { cars[i].print(); } system("PAUSE"); return 0; } _________________________________________________________
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
