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
Car(string _reportingMark,int _carNumber,string _kind,bool
_loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = setKind(_kind); //Modified here
loaded = _loaded;
destination = _destination;
}
//Destructor
~Car()
{
}
//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind; //calls setup function
loaded = other.loaded;
destination = other.destination;
}
Car operator=(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this;
}
friend bool operator==(Car c1,Car c2);
void setUp() //Implemented setup() code here
{
string knd;
//cout<<"Implementing Progress\n";
cout<<"Enter Type/Kind of car : ";
cin>>knd;
setKind(knd);
}
void output()
{
cout<<"Reporting Mark:
"<<reportingMark<<endl;
cout<<"Card Number: "<< carNumber<<endl;
cout<<"Kind: "<< KIND_ARRAY[kind]<<endl;
//Modified printing of Kind
cout<<"Loaded: "<< loaded<<endl;
cout<<"Destination: "<< destination<<endl;
}
};
//Friend function
bool operator==(Car c1,Car c2)
{
if(c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
return true;
}
else
{
return false;
}
}
/* ********** StringOfCars member functions ********** */
class StringOfCar
{
private:
Car *cars; //pointer of cars
int carCount; //defined additional data member carCount
public:
//Default constructor
StringOfCar()
{
cars=NULL;
carCount=0;
}
//Parameterized constructor
StringOfCar(Car *_cars)
{
cars = _cars;
carCount=0;
}
//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
carCount=0;
}
//Destructor
~StringOfCar()
{
//deleting each car allocated
for(int i=0;i<carCount;i++)
delete cars;
}
int getCount() //UFD here defined here
{return carCount;}
void output()
{
for(int i=0; i<carCount; i++)
{
cars[i].output();
}
}
void push(Car car)
{
cars[carCount++]=car;
}
Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of
cars
return lastObj;
}
void input()
{
cout<<"In progress!\n";
}
};
int main()
{
/* ********Create a Car object named car1 with the following
constants as initial values:****/
Car car1("SP",34567 ,"business",true,"Salt Lake City ");
cout<<"---------- TEST1 car1 object
content--------------\n";
car1.output();
/* ******** Create a Car object named car2 using the
default constructor. */
Car car2;
//Use the = operator to copy the data from car1 to car
2.
car2 = car1;
cout<<"\n----------car2 object
content--------------\n";
//Print car2
car2.output();
//Create a default StringOfCars object named
string1.
StringOfCar string1;
string1.push(car1);
//Print: STRING 1
cout<<"\n---------- TEST2 string1 object
content--------------\n";
string1.output();
//Create a car named car3.
Car car3;
car3 = string1.pop();
//Print car3.
cout<<"\n----------TEST3 car3 object
content--------------\n";
car3.output();
//Then print the contents of string1 again
cout<<"\n----------string1 object
content--------------\n";
string1.output();
return 0;
}
Problem E3
Copy the solution from problem E2 and make the name E3.
In this problem we will use inheritance to create two new classes, both
of which will inherit from the class Car
Do not change the StringOfCar class .
We are not using a StringOfCars in this problem, but will need it later.
The kind of cars for the three classes will be:
Car: business, maintenance, other
FreightCar: box, tank, flat, otherFreight
PassengerCar: chair, sleeper, otherPassenger
Change private to protected in the Car class only.
Make two classes that inherit from the Car class: FreightCar and
PassengerCar.
No additional member data will be added.
Each class will need a default constructor, a copy constructor, and a
constructor that has five parameters.
Create setKind functions for the FreightCar and PassengerCar classes
that are similar to the setKind function for the Car class, but with
different values.
The setKind function for the FreightCar class uses only the values:
box, tank, flat, otherFreight
The setKind function for the PassengerCar class uses only the values:
chair, sleeper, otherPassenger
Make the setKind and destructor functions virtual in the Car class only.
This is only done in the declaration, not the definition of the functions.
This is only done in the parent class, not the children classes.
Remove all the code from the main function and replace it with the
following.
Create a Car object named car1 with the following data:
|SLSF 46871 wrecker true Memphis|
Print the car.
Create a Freight Car object named car2 with the following data:
|MP 98765 gondola true Saint Louis|
Print the freignt car.
Create a Passenger Car object named car3 with the following data:
|PAPX 145 combine true Tucson|
Print the passenger car.
Problem E4
Copy the solution from problem E3 and make the name E4.
In this problem we will use the StringOfCars class to contain Car,
FreightCar, and PassengerCar objects all in the same string of cars.
This works because a pointer of type Car * can point to Car objects as
well as point to the child FreightCar and PassengerCar objects.
Because we have pointers of type Car * that may point to any one of the
three types of objects, a StringOfCars object does not know which type
object will be encountered until execution time. The mechanism to select
the correct version of the function at execution time, rather than
having it fixed at compile time, is to use virtual functions. That is
why we made the setKind and destructor functions virtual earlier in this
assignment.
In the input function loop read one line from the file each time
throught the loop, look at the Type field in the record. If the type is
Car create a Car object and call the push function to put it in the
StringOfCars object. Similarly if the type is FreightCar, create and
push a FreightCar object. If it is a PassengerCar, create and push a
PassengerCar object.
We have a push member function that accepts a Car parameter, creates a
copy of the Car parameter that is a Car object in the heap, then puts
the pointer to that Car object into the array. Build another member
function, also named push, that takes a FreightCar parameter, creates a
FreightCar in the heap, and then puts the pointer to that FreightCar
object into the array. Also build a similar member function for a
PassengerCar.
The file for this problem should contain:
Type order ARR number kind loaded destination
Car car1 CN 819481 maintenance false NONE
Car car2 SLSF 46871 business true Memphis
Car car3 AOK 156 tender true McAlester
FreightCar car4 MKT 123456 tank false Fort Worth
FreightCar car5 MP 98765 box true Saint Louis
FreightCar car6 SP 567890 flat true Chicago
FreightCar car7 GMO 7878 hopper true Mobile
PassengerCar car8 KCS 7893 chair true Kansas City
PassengerCar car9 PAPX 145 sleeper true Tucson
PassengerCar car10 GN 744 combine false NONE
Remove the code from the main funtion and replace it with the following
Define a StringOfCars object in the stack. Then pass that object to the
input function. Then call the output function for that object.
You may note that the pop member function and copy constructor for a
StringOfCars do not determine what type of car is being retrieved.
Fixing this problem is more than what I want you to do in the
assignment. So, ignore this problem.
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
#include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...
I created a struct for Car and now I need to turn it into a class for Car. Below is the code that I wrote for the structure. For the class, we are suppose to make the data in the class Car private, revise the input so the input function will only read the data from the user, and then it will call an additional function named setUpCar which will put the data into the object. Not sure how to...
//Vehicle.h
#pragma once
#include<iostream>
#include"Warranty.h"
#include<string>
using namespace std;
class Vehicle{
protected:
string make;
int year;
double mpg;
Warranty warranty;
static int numOfVehicles;
public:
Vehicle();
Vehicle(string s, int y, double m, Warranty
warranty);
Vehicle(Vehicle& v);
~Vehicle();
string getMake();
int getYear();
double getGasMileage();
void setMake(string s);
void setYear(int y);
void setYear(string y);
void setGasMileage(double m);
void setGasMileage(string m);
void displayVehicle();
static int getNumVehicles();
Warranty getWarranty();
void setWarranty(Warranty& );
};
//Vehicle.cpp
#include "Vehicle.h"
#include <string>
Vehicle::Vehicle()
{
make = "unknown";
year =...
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...
Here is the code from the previous three steps:
#include <iostream>
using namespace std;
class Student
{
private:
//class variables
int ID;
string firstName,lastName;
public:
Student(int ID,string firstName,string lastName)
//constructor
{
this->ID=ID;
this->firstName=firstName;
this->lastName=lastName;
}
int getID() //getter method
{
return ID;
}
virtual string getType() = 0; //pure virtual function
virtual void printInfo() //virtual function to print basic details
of a student
{
cout << "Student type: " << getType() <<
endl;
cout << "Student ID: " << ID...
C++
#include <iostream>
using namespace std;
bool checkinventoryid(int id)
{
return id > 0;
}
bool checkinventoryprice(float price)
{
return price > 0;
}
void endProgram()
{
system("pause");
exit(0);
}
void errorID(string str)
{
cout << "Invalid Id!!! " << str
<< " should be greater than 0" << endl;
}
void errorPrice(string str)
{
cout << "Invalid Price!!!" << str
<< " should be greater than 0" << endl;
}
int inputId()
{...
Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
Example program
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
vector<int> factor(int n)
{
vector <int> v1;
// Print the number of 2s that divide n
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
v1.push_back(2);
}
// n must be odd at this point. So we can
skip
// one element (Note i = i +2)
for (int i = 3; i <=...