Question

Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

Language = C++

How to complete this code?

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

///////////// Car.h

#ifndef CAR_H
#define CAR_H

#include<iostream>
#include<string>

using namespace std;


class Car{
   private:
       string make;
       string color;
       int year;
   public:
       // constructor
      
       Car();
       //constructor with parameters
       Car(string a,string b,int c);
      
       // setter and getter functions
       void setMake(string a);
      
       void setColor(string a);
      
       void setYear(int a);
      
       string getMake();
      
       string getColor();
      
       int getYear();
      
      
       // friend functions
       friend ostream& operator<<(ostream &out,Car &c){
          
           out<<c.getYear()<< " "<<c.getColor()<< " "<<c.getMake();
          
           return out;
       }
      
       friend bool operator==(Car &c1,Car &c2){
           bool b=false;
          
           if(c1.getColor()==c2.getColor() && c1.getMake()==c2.getMake() && c1.getYear()==c2.getYear())
               b=true;
          
           return b;
       }
};


#endif

/////////////// Car.cpp

#include<iostream>
#include<sstream>
#include<string>
#include"Car.h"


using namespace std;


Car::Car(){
   make="Ford";
   color="Black";
   year=1910;
}

Car::Car(string a,string b,int c){
   make=a;
   color=b;
   year=c;
}
      
void Car::setMake(string a){
   make=a;
}
      
void Car::setColor(string a){
   color=a;
}
      
void Car::setYear(int a){
   year=a;
}
      
string Car::getMake(){
   return(make);
}
      
string Car::getColor(){
   return(color);
}
      
int Car::getYear(){
   return(year);
}
      


///////////// CarList.h

#ifndef CARLIST_H
#define CARLIST_H

#include<string>

using namespace std;


class List{
   private:
       //car node
       Car *car;
       // andnext link
       List *next;
   public:
       // constructor
       List();
      
       // cosntructor with parameters
       List(string a,string b,int c);
       // setter and getter methods
       void setCar(string a,string b,int c);
       void setCar(Car c);
       void setNext(List *n);
       Car *getCar();
       List *getNext();
};


// carlist class
class CarList{
   private:
       //head
       List *root;
   public:
       // constructor
       CarList();  
      
       // add car  
       void addCar(string a,string b,int c);
      
       // display result
       string displayList();
      
       // finding car
       bool findCar(string a,string b,int c);
      
       // remove head node
       Car *removeHead();
      
};


#endif

//////////// CarList.cpp

#include<iostream>
#include<sstream>
#include<string>
#include"CarList.h"

using namespace std;

List::List(){
   car=new Car();
   next=nullptr;
}

List::List(string a,string b,int c){
   car=new Car(a,b,c);
   next=nullptr;
}
  
void List::setCar(string a,string b,int c){
   car->setMake(a);
   car->setColor(b);
   car->setYear(c);
}
      
void List::setCar(Car c){
   car->setMake(c.getMake());
   car->setColor(c.getColor());
   car->setYear(c.getYear());
}
      
      
void List::setNext(List *n){
   next=n;  
}
      
Car* List::getCar(){
   return(car);
}

List* List::getNext(){
   return(next);  
}


CarList::CarList(){
   root=nullptr;
}

      
void CarList::addCar(string a,string b,int c){
   List *l=new List(a,b,c);
  
   // set car as head  
   if(root==nullptr){
       root=l;
   }
   else{
          
       l->setNext(root);
       root=l;
   }
}
      
string CarList::displayList(){
   string ret="";
          
   List *cur=root;
              
   while(cur!=nullptr){
      
       // integer to string conversion
       stringstream ss;
       ss<<cur->getCar()->getYear();
       string s;
       ss>>s;
       ret+=s;
      
      
       ret+=" ";
       ret+=cur->getCar()->getColor();
       ret+=" ";
       ret+=cur->getCar()->getMake();
       ret+=", ";
                  
       cur=cur->getNext();
   }
          
   return ret;
}
      
bool CarList::findCar(string a,string b,int c){
   bool ret=false;
          
   List *cur=root;
              
   while(cur!=nullptr){
      
       // if car found
       if(a==cur->getCar()->getMake() && b==cur->getCar()->getColor() && c==cur->getCar()->getYear())
       {
           ret=true;
           break;  
       }  
       cur=cur->getNext();
   }
          
   return ret;
}
      
      
// remove head node
Car* CarList::removeHead(){
   if(root==nullptr){
       return nullptr;
   }
   else{
       List *cur=root;
          
       root=root->getNext();
       return cur->getCar();
   }
}

//////////// main.cpp

#include<iostream>
#include<sstream>
#include<string>

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" <<endl;
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 NULL 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
  

system("pause");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

    Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year : int ----------------------- + Car() + setMake(m: string) : void + getMake() : string + setYear(y: int) : void + getYear() : int ---------------------- */ #ifndef CAR_H #define CAR_H #include <iostream> using namespace std; class Car { private: string make; int year; public: Car(); Car(string); Car(int); Car(string, int); void setMake (string); string getMake() {return make;} void setYear (int); int getYear() {return year;} }; #endif #include...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • This is assignment and code from this site but it will not compile....can you help? home...

    This is assignment and code from this site but it will not compile....can you help? home / study / engineering / computer science / computer science questions and answers / c++ this assignment requires several classes which interact with each other. two class aggregations ... Question: C++ This assignment requires several classes which interact with each other. Two class aggregatio... (1 bookmark) C++ This assignment requires several classes which interact with each other. Two class aggregations are formed. The program...

  • CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

    CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle class by making the following changes: • Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class. • Add the copy constructor • Increment numVehicles in all of the constructors • Decrement numVehicle in destructor • Add overloaded versions of setYear and setMpg that...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT