In: Computer Science
3 files
4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in
This was the test drive:
carOne = Hyundai Sonata
carTwo = Hyundai Sonata
carThree =
Enter the make and model of a vehicle: toyota corolla
You entered: toyota corolla
Testing the assignment operator...
carOne == Hyundai Sonata
carTwo == Hyundai Sonata
carThree == Hyundai Sonata
Here are the contents of the car file:
Lamborghini Miura
Ferrari Enzo
Porsche GT2
Ford Mustang
Chevrolet Corvette
Kia Rio
Nissan Versa
Here is what's in the files:
main.cpp
#include
#include
#include
#include
#include "cvehicle.h"
using namespace std;
// ==== main
==================================================================
//
//
============================================================================
int main()
{
// test the constructors
CVehicle carOne("Hyundai", "Sonata");
const CVehicle carTwo = carOne;
CVehicle carThree;
// display the contents of each newly-constructed object...
// should see "Hyundai Sonata"
cout << "carOne = ";
carOne.WriteVehicle();
cout << endl;
// should see "Hyundai Sonata" again
cout << "carTwo = ";
carTwo.WriteVehicle();
cout << endl;
// should see nothing
cout << "carThree = ";
carThree.WriteVehicle();
cout << endl;
// try the "read" function and the overloaded insertion
operator
cout << "Enter the make and model of a vehicle: ";
carThree.ReadVehicle();
cout << "You entered: " << carThree << endl
<< endl;
// try the assignment operator
carOne = carThree = carTwo;
cout << "Testing the assignment operator..." <<
endl;
cout << "carOne == " << carOne << endl;
cout << "carTwo == " << carTwo << endl;
cout << "carThree == " << carThree << endl
<< endl;
// open the car data file
ifstream carFile("cars.dat");
if (carFile.fail())
{
cerr << "Error opening the input file..." <<
endl;
exit(EXIT_FAILURE);
}
cvehicle.h
#ifndef CVEHICLE_HEADER
#define CVEHICLE_HEADER
#include
#include
using namespace std;
// constants
const int BUFLEN = 256;
// class declaration
class CVehicle
{
public:
// ???
private:
char m_make[BUFLEN];
char m_model[BUFLEN];
};
// non-member functions
istream& operator>>(istream &inStream, CVehicle
&rhs);
ostream& operator<<(ostream &outStream, const
CVehicle &rhs);
#endif // CVEHICLE_HEADER
cars.dat
Lamborghini Miura
Ferrari Enzo
Porsche GT2
Ford Mustang
Chevrolet Corvette
Kia Rio
Nissan Versa

#######################################
            cars.dat
#######################################
Lamborghini Miura
Ferrari Enzo
Porsche GT2
Ford Mustang
Chevrolet Corvette
Kia Rio
Nissan Versa
#######################################
        cvehicle.cpp
#######################################
#include "cvehicle.h"
CVehicle::CVehicle() {
        strcpy(m_make, "");
        strcpy(m_model, "");
}
CVehicle::CVehicle(string make, string model) {
        strcpy(m_make, make.c_str());
        strcpy(m_model, model.c_str());
}
void CVehicle::WriteVehicle() const {
        cout << m_make << " " << m_model;
}
void CVehicle::ReadVehicle() {
        string make; string model;
        cin >> make >> model;
        strcpy(m_make, make.c_str());
        strcpy(m_model, model.c_str());
}
CVehicle& CVehicle::operator=(const CVehicle &t) {
        strcpy(m_make, t.m_make);
        strcpy(m_model, t.m_model);
        return *this;
}
// non-member functions
istream& operator>>(istream &inStream, CVehicle &rhs) {
        string make; string model;
        inStream >> make >> model;
        strcpy(rhs.m_make, make.c_str());
        strcpy(rhs.m_model, model.c_str());
        return inStream;
}
ostream& operator<<(ostream &outStream, const CVehicle &rhs) {
        cout << rhs.m_make << " " << rhs.m_model;
        return outStream;
}
#######################################
          cvehicle.h
#######################################
#ifndef CVEHICLE_HEADER
#define CVEHICLE_HEADER
#include<iostream>
#include<cstring>
using namespace std;
// constants
const int BUFLEN = 256;
// class declaration
class CVehicle
{
        public:
        CVehicle();
        CVehicle(string make, string model);
        void WriteVehicle() const;
        void ReadVehicle();
        CVehicle& operator=(const CVehicle &t); 
        // non-member functions
        friend istream& operator>>(istream &inStream, CVehicle &rhs);
        friend ostream& operator<<(ostream &outStream, const CVehicle &rhs);
        private:
        char m_make[BUFLEN];
        char m_model[BUFLEN];
};
#endif // CVEHICLE_HEADER
#######################################
            main.cpp
#######################################
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>
#include "cvehicle.h"
using namespace std;
// ==== main ===============
// =========================
int main()
{
        // test the constructors
        CVehicle carOne("Hyundai", "Sonata");
        const CVehicle carTwo = carOne;
        CVehicle carThree;
        // display the contents of each newly-constructed object...
        // should see "Hyundai Sonata"
        cout << "carOne = ";
        carOne.WriteVehicle();
        cout << endl;
        // should see "Hyundai Sonata" again
        cout << "carTwo = ";
        carTwo.WriteVehicle();
        cout << endl;
        // should see nothing
        cout << "carThree = ";
        carThree.WriteVehicle();
        cout << endl;
        // try the "read" function and the overloaded insertion operator
        cout << "Enter the make and model of a vehicle: ";
        carThree.ReadVehicle();
        cout << "You entered: " << carThree << endl << endl;
        // try the assignment operator
        carOne = carThree = carTwo;
        cout << "Testing the assignment operator..." << endl;
        cout << "carOne == " << carOne << endl;
        cout << "carTwo == " << carTwo << endl;
        cout << "carThree == " << carThree << endl << endl;
        // open the car data file
        ifstream carFile("cars.dat");
        CVehicle testCar;
        if (carFile.fail())
        {
                cerr << "Error opening the input file..." << endl;
                exit(EXIT_FAILURE);
        }
        cout << "Here are the contents of the car file:" << endl;
        while(carFile >> testCar) {
                cout << testCar << endl;
        }
        carFile.close();
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.