Question

In: Computer Science

3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...

3 files

  • cvehicle.h -- a partially filled-out class declaration for the CVehicle class
  • main.cpp -- the main module that creates and manipulates CVehicle objects
  • cars.dat -- a text file that contains name data for the main module

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

Solutions

Expert Solution

#######################################
            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.


Related Solutions

(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
Can't figure out how to do this code with seperate main.cpp, inventory.cpp, inventory.h files, if you...
Can't figure out how to do this code with seperate main.cpp, inventory.cpp, inventory.h files, if you know how please help! in C++ Functional requirements: Create a linked list that holds a series of primary colors. The user inputs which colors are used and in which order.   Programming Requirements: Design your own dynamic linked list class (using pointers) to hold a series of primary colors The class should have the following member functions: append, insert (at a specific position, return -1...
Assume you are in main.cpp define a function that is NOT SCOPED to any class. It...
Assume you are in main.cpp define a function that is NOT SCOPED to any class. It is NOT a member of the ListInterface nor LinkedList classes. Your function should accept two lists. You will remove all values from the first list that are present in the second list. Example: Lists before call:                                    Lists after call: target: A, B, C, A, D, C, E, F, B, C, A                  target: A, A, D, E, F, A toRemove: C, B, Q, Z                                  ...
In the band diagram of potassium, the valence band would be: a) completely filled b) partially...
In the band diagram of potassium, the valence band would be: a) completely filled b) partially filled c) completely empty d) nonexistent I know the answer is a), but could someone clearly explain to me why this is?
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default    public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this Book...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class variables, map and guess that are two dimensional arrays of ints. Part B: In the constructor of the Player class, initialize the map and guess arrays with the appropriate size: map: 10 by 10 (10 outer arrays with 10 elements each) guess: 5 by 2 (5 outer arrays with 2 elements each) Step 2: Player.readPlayerSheet() Part A The readPlayerSheet method in the Player class...
1.2 A high cylinder with a cross-sectional area of ​​12.0 cm ^ 2 was partially filled...
1.2 A high cylinder with a cross-sectional area of ​​12.0 cm ^ 2 was partially filled with mercury whose density is 13.6 x 10^3 kg / m ^ 3; The surface of the mercury is at a height of 5.00 cm above the base of the cylinder. Water (1.00 x 10 ^ 3 kg / m ^ 3) is slowly poured over the mercury and it is observed that these two liquids do not mix. What volume of water should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT