SlideShare a Scribd company logo
Assignment D
In Problem D1 we will use a file to contain the data which we will read into the program.
In Problem D2 we will build a StringOfCar class which will contain a sequence of Car objects.
Also, an operator= member function will be added to the Car class.
Problem D1
1.Car constructors and destructor within the Car class definition
a.default constructor
b.copy constructor
c.other constructors
d.destructor
2.main
3.Car member functions declared within the Car class but defined later
a.setup
b.output
4.global functions
a.operator== with Car parameters
b.input
In this problem, we will read cars from a file, rather than typing them in from the keyboard. Do
the steps in order to build the solution to this problem.
1.Create a file to use for input. This is good because we will be using the input many
times.Create a file containing the following three lines of data (Omit the heading line).
2.Modify the input function.
Remove the & from the parameters in the function header, so they are all values rather than
references.
Move the parameters from the function header and put them within the input function, so they
are now all local variables.
Remove the parameters from the prototype for the input function, so it matches the function
header.
In the input function declare a string named carType. In the current file all the carType values are
"Car". Later we will see different values.
Open the file. If the open fails, send a message to stderr and exit the program.
Use a loop to process each line from the file.
Hint: you can do this with the following while statement, which loops as long as there is data in
the file to be read:
The peek function will return EOF if there is no more data, so we will leave the loop then.Within
this loop, each line in the file will provide the data for a Car.
In all the reads within the input function, There are two differences from the read activity in
assignment C:
Remove the user promp. We do not want it because we are reading from a file.
read using the inputFile object, rather than using the stdin object.
Read the carType field first. It just indicates that we are building an object of type Car.
Read each of the fields: ARR, number, kind, loaded
Always read the destination field even if the Car is not loaded. The destination will be NONE in
the input file when that is the case.
Hint: We need to use getline when reading the destination.
using >> skips leading white space before reading the data. getline does not skip this leading
whitespace. So, before using getline use the following code:
peek looks at the next character you are about to read. If it is a space, get is used to read the
space character, to get it out of the way.
If the carType is "Car", declare a Car object named temp using the constructor that takes 5
parameters.
Call the output function for the Car named temp.
If the carType is not "Car", send an error message and do nothing.
At the bottom of the input function, close the file.
3.Call the input function from the main function with no arguments.
Problem D2
Copy the solution for problem D1 and change the problem number to D2.
Order of functions in the code:
Note that the member function operator= has been added to the Car class.
Note that the StringOfCars class and its member functions has been added.
1.Car constructors and destructor within the Car class definition
a.default constructor
b.copy constructor
c.other constructors
d.destructor
2.StringOfCars constructors and destructor within the StringOfCar class definition
a.default constructor
b.copy constructor
c.other constructors
d.destructor
3.main
4.Car member functions declared within the Car class but defined later
a.setup
b.output
c.operator=
5.StringOfCars member functions declared within the StringOfCars class but defined later
a.output
b.push
c.pop
6.global functions
a.operator== with Car parameters
b.input
Copy the following operator= overload member function that returns the left hand operator by
reference. Also code a prototype for this function within the Car class.
Several cars coupled together are referred to as a string of cars.
Create another class called StringOfCars, containing:
a pointer to an array of Car objects in the heap, named ptr.
a static const int set to 10, named ARRAY_SIZE.
an int containing the current number of Cars in the array, named carCount.
a default constructor which gets space in the heap for the array of Car elements, and sets the the
carCount to zero.
a copy constructor which gets new space in the heap for an array of Car elements with size
ARRAY_SIZE. It copies the each Car and also copies the carCount.
a destructor which returns the space to the heap.
a push function which adds a car to the string of cars. Exit with an error message if the string of
cars is full.
a pop function which removes a car from the string of cars, Last In First Out (LIFO). The return
type is void. There is one prarameter which is a reference to a Car object. Put the Car object that
was taken out from the array into the parameter Car object. Exit with an error message if the
string is empty.
an output function which prints a heading for each car:
car number n where n is the position in the array starting from 1 for the first car and then uses
the Car output function to print the data for each car Or, if the array is empty prints: NO cars
Make the following changes in the input function:
Change the input function to have a parameter that is a reference to a StringOfCars.
After creating a Car, do not print it.
Instead, push the Car onto the StringOfCars.
Replace the main function to do the following tests:
1.Test the Car operator= function.
Print: TEST 1
Creat a Car object named car1 by using a constructor with the following constants as initial
values:
reportingMark: SP
carNumber: 34567
kind: business
loaded: true
destination: Salt Lake City
Create a Car object named car2 using the default constructor.
Use the = operator to copy the data from car1 to car 2.
Print car2
2.Test the StringOfCar push function.
Add to main:
Print: TEST 2
Create a default StringOfCars object named string1.
Pass string1 to the input function.
Use the same file as in problem D1.
Print: STRING 1
In the main function, print string1.
3.Test the StringOfCars pop function.
Add to main:
Print: TEST 3
Create a car named car3.
Pop one car from string1 into car3.
Print: CAR 3
Print car3.
Print: STRING 1
Then print the contents of string1 again
Solution
#include
#include
#include
//prototype for the input function
void input();
class Car
{
private:
static const int FIELD_WIDTH = 22;
std::string reportingMark;
int carNumber;
std::string kind;
bool loaded;
std::string destination;
public:
//default constructor
Car()
{setUp(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination
="NONE");}
//copy constructor
Car(const Car &userCar)
{setUp(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded,
userCar.destination);}
//input constructor
Car(std::string reportingMark, int carNumber, std::string kind, bool loaded, std::string
destination)
{setUp(reportingMark, carNumber, kind, loaded, destination);}
//destructor
~Car(){};
//prototype for the == operator overload
friend bool operator == (const Car &car1, const Car &car2);
//prototype for the setUp function
void setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string
destination);
//prototype for the output function
void output();
};
int main()
{
input();
return 0;
}
/** setUp()
this functions takes in information given by the user in main().
it then stores this information in variables within the object
*/
void Car::setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded,
std::string destination)
{
this->reportingMark = reportingMark;
this->carNumber = carNumber;
this->kind = kind;
this->loaded = loaded;
this->destination = destination;
}
/** output()
this function uses information stored within the calling object
and displays it to the screen for the user
*/
void Car::output()
{
std::cout << std::setw(FIELD_WIDTH) << std::left << "Reporting Mark: " <<
std::setw(FIELD_WIDTH) << reportingMark << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Car Number: " << std::setw(FIELD_WIDTH)
<< carNumber << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Kind: " << std::setw(FIELD_WIDTH) <<
kind << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Loaded: " << std::setw(FIELD_WIDTH);
if(loaded)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Destination: " << std::setw(FIELD_WIDTH) <<
destination << std::endl;
std::cout << std::endl;
}
/** == operator overload for Car
this operator overload is used to establish
equality between two Car objects based
on their reportingMark and carNumber
*/
bool operator == (const Car &car1, const Car &car2)
{
if(car1.reportingMark == car2.reportingMark && car1.carNumber == car2.carNumber)
return true;
else
return false;
}
/** input()
this functions reads information from a designated file
and places the information within a temporary Car object
if it has a carType of Car. It then prints the data to
the screen.
*/
void input()
{
std::ifstream inputFile;
std::string reportingMark;
std::string kind;
std::string destination;
std::string carType;
std::string isLoaded;
int carNumber;
bool loaded;
inputFile.open("carData.txt");
while(inputFile.peek() != EOF)
{
inputFile >> carType >> reportingMark >> carNumber >> kind >> isLoaded;
if(isLoaded == "true")
loaded = true;
else
loaded = false;
while(inputFile.peek() == ' ')
{
inputFile.get();
}
std::getline(inputFile, destination);
if(carType == "Car")
{
Car temp(reportingMark, carNumber, kind, loaded, destination);
temp.output();
}
else
std::cout << " ERROR: Not a valid vehicle type. ";
}
inputFile.close();
}
sample output
Reporting Mark: CN
Car Number: 819481
Kind: maintenance
Loaded: false
Destination: NONE
Reporting Mark: SLSF
Car Number: 46871
Kind: business
Loaded: true
Destination: Memphis
Reporting Mark: AOK
Car Number: 156
Kind: tender
Loaded: true
Destination: McAlester

More Related Content

Similar to Assignment DIn Problem D1 we will use a file to contain the dat.pdf

U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
Manivannan837728
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
ziyadaslanbey
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Chap 5 c++Chap 5 c++
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
Nico Ludwig
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
ziyadaslanbey
 
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docxassignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
ssuser562afc1
 
Function
FunctionFunction
Function
Rajat Patel
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
YASHU40
 
C# Programming Help
C# Programming HelpC# Programming Help
C# Programming Help
Programming Homeworks Help
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
Mahmoud Samir Fayed
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
raghukatagall2
 
L9
L9L9
L9
lksoo
 
CAR Report - xsl specification
CAR Report - xsl specificationCAR Report - xsl specification
CAR Report - xsl specification
Gregory Weiss
 
Design functional solutions in Java, a practical example
Design functional solutions in Java, a practical exampleDesign functional solutions in Java, a practical example
Design functional solutions in Java, a practical example
Marian Wamsiedel
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 

Similar to Assignment DIn Problem D1 we will use a file to contain the dat.pdf (20)

U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docxassignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
assignment8.DS_Store__MACOSXassignment8._.DS_Storeass.docx
 
Function
FunctionFunction
Function
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
 
C# Programming Help
C# Programming HelpC# Programming Help
C# Programming Help
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
L9
L9L9
L9
 
CAR Report - xsl specification
CAR Report - xsl specificationCAR Report - xsl specification
CAR Report - xsl specification
 
Design functional solutions in Java, a practical example
Design functional solutions in Java, a practical exampleDesign functional solutions in Java, a practical example
Design functional solutions in Java, a practical example
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 

More from fasttrackscardecors

On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdfOn a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
fasttrackscardecors
 
Molecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdfMolecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdf
fasttrackscardecors
 
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdfMissy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
fasttrackscardecors
 
Justify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdfJustify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdf
fasttrackscardecors
 
Inventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdfInventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdf
fasttrackscardecors
 
IncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdfIncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdf
fasttrackscardecors
 
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdfIf you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
fasttrackscardecors
 
Identify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdfIdentify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdf
fasttrackscardecors
 
I have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdfI have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdf
fasttrackscardecors
 
I need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdfI need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdf
fasttrackscardecors
 
How is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdfHow is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdf
fasttrackscardecors
 
Fungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdfFungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdf
fasttrackscardecors
 
For each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdfFor each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdf
fasttrackscardecors
 
For each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdfFor each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdf
fasttrackscardecors
 
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdfExplain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
fasttrackscardecors
 
Find all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdfFind all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdf
fasttrackscardecors
 
Describe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdfDescribe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdf
fasttrackscardecors
 
describe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdfdescribe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdf
fasttrackscardecors
 
Connect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdfConnect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdf
fasttrackscardecors
 
Consider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdfConsider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdf
fasttrackscardecors
 

More from fasttrackscardecors (20)

On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdfOn a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
 
Molecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdfMolecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdf
 
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdfMissy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
 
Justify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdfJustify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdf
 
Inventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdfInventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdf
 
IncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdfIncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdf
 
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdfIf you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
 
Identify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdfIdentify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdf
 
I have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdfI have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdf
 
I need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdfI need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdf
 
How is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdfHow is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdf
 
Fungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdfFungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdf
 
For each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdfFor each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdf
 
For each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdfFor each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdf
 
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdfExplain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
 
Find all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdfFind all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdf
 
Describe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdfDescribe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdf
 
describe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdfdescribe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdf
 
Connect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdfConnect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdf
 
Consider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdfConsider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdf
 

Recently uploaded

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 

Recently uploaded (20)

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 

Assignment DIn Problem D1 we will use a file to contain the dat.pdf

  • 1. Assignment D In Problem D1 we will use a file to contain the data which we will read into the program. In Problem D2 we will build a StringOfCar class which will contain a sequence of Car objects. Also, an operator= member function will be added to the Car class. Problem D1 1.Car constructors and destructor within the Car class definition a.default constructor b.copy constructor c.other constructors d.destructor 2.main 3.Car member functions declared within the Car class but defined later a.setup b.output 4.global functions a.operator== with Car parameters b.input In this problem, we will read cars from a file, rather than typing them in from the keyboard. Do the steps in order to build the solution to this problem. 1.Create a file to use for input. This is good because we will be using the input many times.Create a file containing the following three lines of data (Omit the heading line). 2.Modify the input function. Remove the & from the parameters in the function header, so they are all values rather than references. Move the parameters from the function header and put them within the input function, so they are now all local variables. Remove the parameters from the prototype for the input function, so it matches the function header. In the input function declare a string named carType. In the current file all the carType values are "Car". Later we will see different values. Open the file. If the open fails, send a message to stderr and exit the program. Use a loop to process each line from the file. Hint: you can do this with the following while statement, which loops as long as there is data in the file to be read:
  • 2. The peek function will return EOF if there is no more data, so we will leave the loop then.Within this loop, each line in the file will provide the data for a Car. In all the reads within the input function, There are two differences from the read activity in assignment C: Remove the user promp. We do not want it because we are reading from a file. read using the inputFile object, rather than using the stdin object. Read the carType field first. It just indicates that we are building an object of type Car. Read each of the fields: ARR, number, kind, loaded Always read the destination field even if the Car is not loaded. The destination will be NONE in the input file when that is the case. Hint: We need to use getline when reading the destination. using >> skips leading white space before reading the data. getline does not skip this leading whitespace. So, before using getline use the following code: peek looks at the next character you are about to read. If it is a space, get is used to read the space character, to get it out of the way. If the carType is "Car", declare a Car object named temp using the constructor that takes 5 parameters. Call the output function for the Car named temp. If the carType is not "Car", send an error message and do nothing. At the bottom of the input function, close the file. 3.Call the input function from the main function with no arguments. Problem D2 Copy the solution for problem D1 and change the problem number to D2. Order of functions in the code: Note that the member function operator= has been added to the Car class. Note that the StringOfCars class and its member functions has been added. 1.Car constructors and destructor within the Car class definition a.default constructor b.copy constructor c.other constructors d.destructor 2.StringOfCars constructors and destructor within the StringOfCar class definition a.default constructor b.copy constructor c.other constructors d.destructor
  • 3. 3.main 4.Car member functions declared within the Car class but defined later a.setup b.output c.operator= 5.StringOfCars member functions declared within the StringOfCars class but defined later a.output b.push c.pop 6.global functions a.operator== with Car parameters b.input Copy the following operator= overload member function that returns the left hand operator by reference. Also code a prototype for this function within the Car class. Several cars coupled together are referred to as a string of cars. Create another class called StringOfCars, containing: a pointer to an array of Car objects in the heap, named ptr. a static const int set to 10, named ARRAY_SIZE. an int containing the current number of Cars in the array, named carCount. a default constructor which gets space in the heap for the array of Car elements, and sets the the carCount to zero. a copy constructor which gets new space in the heap for an array of Car elements with size ARRAY_SIZE. It copies the each Car and also copies the carCount. a destructor which returns the space to the heap. a push function which adds a car to the string of cars. Exit with an error message if the string of cars is full. a pop function which removes a car from the string of cars, Last In First Out (LIFO). The return type is void. There is one prarameter which is a reference to a Car object. Put the Car object that was taken out from the array into the parameter Car object. Exit with an error message if the string is empty. an output function which prints a heading for each car: car number n where n is the position in the array starting from 1 for the first car and then uses the Car output function to print the data for each car Or, if the array is empty prints: NO cars Make the following changes in the input function: Change the input function to have a parameter that is a reference to a StringOfCars. After creating a Car, do not print it.
  • 4. Instead, push the Car onto the StringOfCars. Replace the main function to do the following tests: 1.Test the Car operator= function. Print: TEST 1 Creat a Car object named car1 by using a constructor with the following constants as initial values: reportingMark: SP carNumber: 34567 kind: business loaded: true destination: Salt Lake City Create a Car object named car2 using the default constructor. Use the = operator to copy the data from car1 to car 2. Print car2 2.Test the StringOfCar push function. Add to main: Print: TEST 2 Create a default StringOfCars object named string1. Pass string1 to the input function. Use the same file as in problem D1. Print: STRING 1 In the main function, print string1. 3.Test the StringOfCars pop function. Add to main: Print: TEST 3 Create a car named car3. Pop one car from string1 into car3. Print: CAR 3 Print car3. Print: STRING 1 Then print the contents of string1 again Solution #include #include
  • 5. #include //prototype for the input function void input(); class Car { private: static const int FIELD_WIDTH = 22; std::string reportingMark; int carNumber; std::string kind; bool loaded; std::string destination; public: //default constructor Car() {setUp(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination ="NONE");} //copy constructor Car(const Car &userCar) {setUp(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded, userCar.destination);} //input constructor Car(std::string reportingMark, int carNumber, std::string kind, bool loaded, std::string destination) {setUp(reportingMark, carNumber, kind, loaded, destination);} //destructor ~Car(){}; //prototype for the == operator overload friend bool operator == (const Car &car1, const Car &car2); //prototype for the setUp function void setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string destination); //prototype for the output function void output(); }; int main()
  • 6. { input(); return 0; } /** setUp() this functions takes in information given by the user in main(). it then stores this information in variables within the object */ void Car::setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; this->kind = kind; this->loaded = loaded; this->destination = destination; } /** output() this function uses information stored within the calling object and displays it to the screen for the user */ void Car::output() { std::cout << std::setw(FIELD_WIDTH) << std::left << "Reporting Mark: " << std::setw(FIELD_WIDTH) << reportingMark << std::endl; std::cout << std::setw(FIELD_WIDTH) << "Car Number: " << std::setw(FIELD_WIDTH) << carNumber << std::endl; std::cout << std::setw(FIELD_WIDTH) << "Kind: " << std::setw(FIELD_WIDTH) << kind << std::endl; std::cout << std::setw(FIELD_WIDTH) << "Loaded: " << std::setw(FIELD_WIDTH); if(loaded) std::cout << "true" << std::endl; else std::cout << "false" << std::endl; std::cout << std::setw(FIELD_WIDTH) << "Destination: " << std::setw(FIELD_WIDTH) << destination << std::endl;
  • 7. std::cout << std::endl; } /** == operator overload for Car this operator overload is used to establish equality between two Car objects based on their reportingMark and carNumber */ bool operator == (const Car &car1, const Car &car2) { if(car1.reportingMark == car2.reportingMark && car1.carNumber == car2.carNumber) return true; else return false; } /** input() this functions reads information from a designated file and places the information within a temporary Car object if it has a carType of Car. It then prints the data to the screen. */ void input() { std::ifstream inputFile; std::string reportingMark; std::string kind; std::string destination; std::string carType; std::string isLoaded; int carNumber; bool loaded; inputFile.open("carData.txt"); while(inputFile.peek() != EOF) { inputFile >> carType >> reportingMark >> carNumber >> kind >> isLoaded; if(isLoaded == "true") loaded = true;
  • 8. else loaded = false; while(inputFile.peek() == ' ') { inputFile.get(); } std::getline(inputFile, destination); if(carType == "Car") { Car temp(reportingMark, carNumber, kind, loaded, destination); temp.output(); } else std::cout << " ERROR: Not a valid vehicle type. "; } inputFile.close(); } sample output Reporting Mark: CN Car Number: 819481 Kind: maintenance Loaded: false Destination: NONE Reporting Mark: SLSF Car Number: 46871 Kind: business Loaded: true Destination: Memphis Reporting Mark: AOK Car Number: 156 Kind: tender Loaded: true Destination: McAlester