Question

#2 You are going to add more code to carClass.cpp. 0. Make sure you finished the lab part: goForward, turnRight(), getDirecti
7. In our get functions such as getMake, none of the attributes are being changed. We make those functions const. string Car:
9. Make a prototype of showinfo) right above the main. #include // dass definition class Car function implementations Car:Car
Hinclude su using need NORTH, EAST, SOUTH, WEST); enim dire class Car private: string make; string model; int mileage; int X;
void Car::goForward(int blocks) if(direction == NORTH) Y + blocks; else if(direction == EAST) X += blocks; else if(direction
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code for the following question, although you did well but I have to modify and add some functionalities to make the program as required in the question.However, if you feel any doubt, please feel free to ask

Code :


#include <iostream>
#include <string.h>
using namespace std;

enum direc{NORTH, EAST, SOUTH, WEST};

class Car
{
private:
string make;
string model;
int milage;
int X;
int Y;
direc direction;
  
public:
Car(string mk, string md);

string getMake() const;
string getModel() const;
string getDirection() const;
int getX() const;
int getY() const;
int getMilage() const;

void turnRight();
void turnLeft();
void goForward(int blocks);
void setMilage(int milage);

};

//constructor
Car:: Car(string mk, string md)
{
make = mk;
model = md;
milage = 0;
X = 0;
Y = 0;
direction = NORTH;
  
}


//accessors or we can say that getter function
string Car::getMake() const
{
return make;
}
string Car::getModel() const
{
return model;
}

string Car::getDirection() const
{
switch(direction)
{
case NORTH:
return "north";
  
case EAST:
return "east";
  
case SOUTH:
return "south";

case WEST:
return "west";
}
}

int Car::getX() const
{
return X;
}

int Car::getY() const
{
return Y;
}

int Car:: getMilage() const
{
return milage;
}


//method to turn the car in right direction
void Car::turnRight()
{
if(direction==WEST)
{
direction = NORTH;
}
  
else
{
direction = (direc) (direction + 1);
}
}


// method to turn the car in left direction
void Car::turnLeft()
{
if(direction==NORTH)
{
direction = WEST;
}
  
else
{
direction = (direc) (direction - 1);
}
}


// method to move th car in forward way
void Car::goForward(int blocks)
{
if(direction == NORTH)
{
Y += blocks;
}
else if(direction == EAST)
{
X += blocks;
}
else if(direction == SOUTH)
{
Y -= blocks;
}
else
{
X -= blocks;
}
}


//setter method to set the milage of the car when car forward
void Car::setMilage(int mil_age)
{
milage += mil_age;
}

//this is our showInfo() method outside the class having the object of the car;
string showInfo(Car c)
{
string mil = to_string(c.getMilage());

string output = c.getMake() + " " + c.getModel() + " is located at ("+ to_string(c.getX()) + ","
+ to_string(c.getY()) + ") facing " + c.getDirection() + ", it has "
+ to_string(c.getMilage()) + " miles on it\n";
return output;
}


//this is the main function from where execution of program start
int main()
{
  
//object of car class c1 and c2
Car c1("Toyoto", "Camry");
Car c2("Honda", "Accord");

//creating an array which will store all forward move for the car c1
int move1[] = { 3,5,7,6,1};
string turn1[] = {"right", "right", "right", "left"};

int n1 = sizeof(move1)/sizeof(move1[0]);
int m1 = sizeof(turn1)/sizeof(turn1[0]);

for(int i=0; i<n1; i++)
{
c1.setMilage(move1[i]) ;

c1.goForward(move1[i]);

if( i <= m1)
{
if(turn1[i] == "left")
{
c1.turnLeft();
  

}
else if(turn1[i] == "right")
{
c1.turnRight();
}


}
}

//printing the details of car1 by calling showInfo() function
cout << showInfo(c1);


//creating an array which will store all forward move for the car c2
int move2[] = {5,1,2,4,10};
string turn2[] = {"left", "left", "left", "left"};

int n2 = sizeof(move2)/sizeof(move2[0]);
int m2 = sizeof(turn2)/sizeof(turn2[0]);

for(int i=0; i<n2; i++)
{
c2.setMilage(move2[i]) ;

c2.goForward(move2[i]);

if( i <= m2)
{
if(turn2[i] == "left")
{
c2.turnLeft();

}
else if(turn2[i] == "right")
{
c2.turnRight();
}

}
}

//printing the details of car2 by calling showInfo() function
cout << showInfo(c2);
  
return 0;
}

output :

Toyoto Camry is located at (-1,-5) facing south, it has 22 miles on it                                                

Honda Accord is located at (3,13) facing north, it has 22 miles on it

------------------------------------------------------------------------------------------------------------------------------------------------------------

here is the screenshots of the output of the following program :

input Toyoto Camry is located at (-1,-5)_facing south, it has 22 miles on it Honda Accord is located at (3,13) facing north,

Add a comment
Know the answer?
Add Answer to:
#2 You are going to add more code to carClass.cpp. 0. Make sure you finished the...
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...

  • [C++] Please help to understand why this code need to have "void calcSales(CorpData &);" and "void...

    [C++] Please help to understand why this code need to have "void calcSales(CorpData &);" and "void displayDivInfo(CorpData);". Thanks! ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> #include <string> using namespace std; struct CorpData { string diviName; double firstQua, secondQua, thirdQua, fourthQua, annualSales, quaAvg;    CorpData(string d, double fq, double sq, double tq, double frq) { diviName = d; firstQua = fq; secondQua = sq; thirdQua = tq; fourthQua = frq; } }; void calcSales(CorpData &); void displayDivInfo(CorpData); int main() {    CorpData West("West", 10000, 20000,...

  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • I'm trying to finish debugging this C# problem. It will compile, but throws a warning: warning...

    I'm trying to finish debugging this C# problem. It will compile, but throws a warning: warning CS0649: Field `DebugTen2.Street.name' is never assigned to, and will always have its default value `null'. I've tried a few ways to fix it, but the street names are still not coming out properly. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DebugTen2 { class Class { public static void Main() { OneWayStreet oak = new OneWayStreet("Oak Avenue", "east"); TwoWayStreet elm = new TwoWayStreet("Elm...

  • guys need help please im super lost anyone save me do programming exercise top_div_array.cpp: Winning Division...

    guys need help please im super lost anyone save me do programming exercise top_div_array.cpp: Winning Division app (top_div.cpp) revisited to use arrays This is the same program done last week but using and passing arrays instead of individual variables. Start with the following file / cODE BELOW: // Name: top_div_array.cpp // Description: // This app inputs sales for four regional division and displays the highest. // To accomplish this, use two arrays of equal length - one for sales and...

  • Having a rough time getting started with the constructors and the display function of this class,...

    Having a rough time getting started with the constructors and the display function of this class, any advice/assitance is appreciated! Task You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the...

    Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the call to addFuel in the main program with a call to the accessor and mutator for the fuel amount to achieve the same effect (add 5 gallons of fuel to the existing fuel in the Toyota Camry). Test your program again to make sure it works and produces the same output. Part 2: Add functions to remove duplicate code * In the main 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...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

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