Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works.

C++ Objects, Structs and Linked Lists.

Program Design:

You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program.

Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct that is defined in list.h.

Remember that when writing a method outside of the class, you need to specify the class name in the method name.

Program Requirements

You need to define a class that defines a linked list of cars.

Your car class should have the following:

default constructor that sets up a 1910, Black, Ford

overloaded constructor that takes a string make, string color, and integer year

setters and getters for all three variables

Overloaded equality operator matches all three variables, return true if they match, false otherwise

Overloaded << operator that adds year, color, make to the provided output stream

Your list class should have the following:

a link struct that contains a pointer to next and a pointer to a car object

constructor that sets head to nullptr

a destructor that walks down the list and deletes any remaining links

addCar input is make, color, year. Creates a new car, creates a new link that points to that car, adds the link to the head of the list

findCar input is make, color, year. Creates a temporary car with those inputs. Uses the overloaded equality operator to check the list to see if such a car exists. Returns true if found, false otherwise

removeHead if list is empty, returns nullptr. Otherwise returns a pointer to the car at the head of the list and deletes that struct

displayList return a string containing the Cars in the list. Use the overloaded << operator to create this list. There should be a comma and space after each car.

Provided Test Driver:

#include

using namespace std;

//#define TEST_CAR #define TEST_CARLIST

#ifdef TEST_CAR #include "Car.h" #endif

#ifdef TEST_CARLIST #include "Car.h" #include "CarList.h" #endif

int main() { #ifdef TEST_CAR // default constructor (Ford, Black, 1910) cout << "Testing Car class" << endl; Car car1; // overloaded constructors Car car2("Pinto", "Purple", 1968); Car truck1("Toyota", "Blue", 2010); Car truck2("Jeep", "Green", 2008);

// test default constructor cout << "Testing default constructor" << endl; cout << "Should be a: Black 1910 Ford" < cout << car1.getColor() << " " << car1.getYear() << " " << car1.getMake() << endl << endl; // test overloaded constructor cout << "Testing overloaded constructor" << endl; cout << "Should be a: Purple 1968 Pinto" << endl << "Actually is: "; cout << car2.getColor() << " " << car2.getYear() << " " << car2.getMake() << endl << endl; // test setters cout << "Testing setters" << endl; cout << "Should be a Blue 2010 Toyota, is "; cout << truck1.getColor() << " " << truck1.getYear() << " " << truck1.getMake() << endl; truck1.setColor("Green"); truck1.setYear(2008); truck1.setMake("Jeep"); cout << "Now should be a Green 2008 Jeep, is "; cout << truck1.getColor() << " " << truck1.getYear() << " " << truck1.getMake() << endl << endl; // testing extraction operator cout << "Testing extraction operator, next two lines should match" << endl; cout << car2.getYear() << " " << car2.getColor() << " " << car2.getMake() << endl; cout << car2 << endl << endl; // testing overloaded equality cout << "Testing equality" << endl; cout << car1 << ((car1 == truck1)?" is same as ":" is not same as ") << truck1 << endl; cout << truck1 << ((truck1 == truck2)?" is same as ":" is not same as ") << truck2 << endl << endl;

cout << "Done with testing Car class" << endl << endl; #endif // TEST_CAR #ifdef TEST_CARLIST cout << "Testing CarList class" << endl; CarList theList; // testing adding items and displaying them theList.addCar("Taxi", "Yellow", 1992); theList.addCar("Ford", "White", 2017); theList.addCar("Honda", "Blue", 2010); theList.addCar("Chevy", "Green", 2012); cout << "Display list, should be: " << endl; cout << " 2012 Green Chevy, 2010 Blue Honda, 2017 White Ford, 1992 Yellow Taxi, " << endl; cout << "Actualy is: " << endl; cout << " " << theList.displayList() << endl << endl; // testing find cout << "Testing find " << endl; Car car1("Chevy", "Blue", 2018); Car car2("Ford", "White", 2017); cout << car1 << ((theList.findCar("Chevy", "Blue", 2018))?" is ":" is not " ) << "in the list" << endl; cout << car2 << ((theList.findCar("Ford", "White", 2017))?" is ":" is not " ) << "in the list" << endl << endl; // testing removehead cout << "Testing remove head" << endl; cout << "Should be: " << theList.displayList() << endl; cout << "Actually is: "; // this list will terminate when nullptr is returned while ( Car * tempCar = theList.removeHead() ) { cout << *tempCar << ", "; delete tempCar; } // testing find on empty list cout << endl < cout << car2 << ((theList.findCar("Ford", "White", 2017))?" is ":" is not " ) << "in the list" << endl << endl; cout << "Done with testing CarList class" << endl << endl; #endif // TEST_CARLIST return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions