Problem: Create a Car class with the following characteristics: It has data memb
ID: 3533176 • Letter: P
Question
Problem:
Create a Car class with the following characteristics:
It has data members to hold its maximum speed (int), its mpg rating (oat), and its make and model (each maximum of
10 characters long).
It has a constructor that takes (in order) make, model, maximum speed, and mpg.
It has a second constructor that takes no parameters. This constructor must call the parent constructor with default values
(blank strings for make and model, 0 for the speed and mpg values).
It has two methods: serialize and deserialize. The serialize method writes the data members to disk in a binary le. The
deserialize method reads them back in. A starter le has been created for you.
#include <iostream>
#include <string>
#import <fstream>
using namespace std;
const int MAX_MODEL = 10;
const int MAX_MAKE = 10;
class Car {
private:
// insert appropriate member variables here
public:
Car(const char model[MAX_MODEL], const char make[MAX_MAKE],int max_speed, float mpg ){
// complete this function
}
Car() : Car("","",0,0.0){};
void serialize(string filename){
// complete this function
}
void deserialize(string filename){
// complete this function
}
void print_values(){
// complete this function
/* required format:
Make: <value here>
Model: <value here>
Max Speed: <value here>
MPG: <value here>
*/
}
};
int main()
{
// DO NOT MODIFY THIS CODE
const string filename = "testfile.dat";
Car first_car("Ford","Mustang",130, 15.5);
first_car.print_values();
first_car.serialize(filename);
cout << "Loading car from data file" << endl;
Car* loaded_car = new Car();
loaded_car->deserialize(filename);
loaded_car->print_values();
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#import <fstream>
using namespace std;
const int MAX_MODEL = 10;
const int MAX_MAKE = 10;
class Car {
private:
// insert appropriate member variables here
const char car_model[MAX_MODEL];
const char car_make[MAX_MAKE];
int car_max_speed;
float car_mpg;
public:
Car(const char model[MAX_MODEL], const char make[MAX_MAKE],int max_speed, float mpg ){
// complete this function
car_model = model;
car_make = make;
car_max_speed = max_speed;
car_mpg = mpg;
}
Car() : Car("","",0,0.0){};
void serialize(string filename){
// complete this function
ofstream myfile (filename, ios::out | ios::binary);
int size;
if(myfile.is_open())
{
myfile.write(car_model,10);
myfile.seekp(10,ios::cur);
myfile.write(car_make,10);
myfile.seekp(10,ios::cur);
myfile.write((char *) &car_max_speed,sizeof(int));
myfile.seekp(sizeof(int),ios::cur);
myfile.write((char *) &car_mpg,sizeof(float));
myfile.seekp(sizeof(float),ios::cur);
}
myfile.close();
}
void deserialize(string filename){
// complete this function
ifstream myfile (filename, ios::in|ios::binary|ios::ate);
if (myfile.is_open())
{
myfile.seekg (0, ios::beg);
myfile.read(car_model,10);
myfile.seekg(10,ios::cur);
myfile.read(car_make,10);
myfile.seekg(10,ios::cur);
myfile.read((char *) &car_max_speed,sizeof(int));
myfile.seekg(sizeof(int),ios::cur);
myfile.read((char *) &car_mpg,sizeof(float));
myfile.seekg(sizeof(float),ios::cur);
}
myfile.close();
}
void print_values(){
// complete this function
/* required format:
Make: <value here>
Model: <value here>
Max Speed: <value here>
MPG: <value here>
*/
cout << "Make: "<< car_make << " Model: " << car_model <<" Max Speed: "<< car_max_speed << " MPG: " << car_mpg ;
}
};
int main()
{
// DO NOT MODIFY THIS CODE
const string filename = "testfile.dat";
Car first_car("Ford","Mustang",130, 15.5);
first_car.print_values();
first_car.serialize(filename);
cout << "Loading car from data file" << endl;
Car* loaded_car = new Car();
loaded_car->deserialize(filename);
loaded_car->print_values();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.