Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is C + + programing question Declare and implement two member functions and a global function that will change the state of a Train

This is C++ programing question Declare and implement two member functions and a global function that will change the state of a Train object. These functions will NOT change the state of a Train object if it is in a safe empty state. Note: Review the testing program and the sample output in order to understand the behaviour of these three functions. The member function loadPeople changes the number of people on a train. The value of the input parameter is used to increase or decrease the number of people on a train. It must make sure that the number of people will not be negative or exceed MAX_PEOPLE. It returns true if the operation succeeds. It returns false if the Train object is in a safe empty state. The member function changeSpeed changes the speed of a train. The value of the input parameter is used to increase or decrease the speed of a train. It must make sure that the speed of a train will not be negative or exceed MAX_SPEED. It returns true if the operation succeeds. It returns false if the Train object is in a safe empty state. A global function transfer moves as many passengers as possible from the second Train to the first Train. It has two parameters (first, second) that reference two Train objects. The function must make sure that the number of people on both Train objects will not be negative or exceed MAX_PEOPLE. It returns the number of people that have been moved to the first Train. It returns -1 if any of the Train objects is in a safe empty state. Testing Program #include #include #include using namespace std; #include "Train.h" using namespace seneca; int main(){ Train trains[3]; trains[1].set("Bullet Train", 100,245.95); trains[2].set("VIA Rail Abitibi", 250,115.95); trains[0]= trains[1]; // Watch out! cout <<"----------------------------------------"<< endl; cout <<"1. Testing changeSpeed." << endl; cout <<"----------------------------------------"<< endl; trains[1].changeSpeed(15); trains[2].changeSpeed(-30); trains[1].display(); trains[2].display(); trains[1].changeSpeed(500); trains[2].changeSpeed(-600); trains[1].display(); trains[2].display(); cout <<"----------------------------------------"<< endl << endl; cout <<"----------------------------------------"<< endl; cout <<"2. Testing loadPeople." << endl; cout <<"----------------------------------------"<< endl; trains[1].loadPeople(101); trains[2].loadPeople(-55); trains[1].display(); trains[2].display(); trains[1].loadPeople(1500); trains[2].loadPeople(-2000); trains[1].display(); trains[2].display(); cout <<"----------------------------------------"<< endl << endl; cout <<"----------------------------------------"<< endl; cout <<"3. Testing transfer." << endl; cout <<"----------------------------------------"<< endl; transfer(trains[1], trains[2]); trains[1].display(); trains[2].display(); trains[2].loadPeople(955); transfer(trains[2], trains[0]); trains[2].display(); trains[0].display(); // Watch out! cout <<"----------------------------------------"<< endl << endl; cout <<"----------------------------------------"<< endl; cout <<"4. Testing transfer (safe empty states)."<< endl; cout <<"----------------------------------------"<< endl; trains[0].set(nullptr,-1,-1); // safe empty state cout <<( transfer( trains[0], trains[1]))<< endl; cout <<( transfer( trains[1], trains[0]))<< endl; cout <<"----------------------------------------"<< endl << endl; return 0; } Sample Output ----------------------------------------1. Testing changeSpeed. ---------------------------------------- NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 100 SPEED : 260.95 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 250 SPEED : 85.95 km/h NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 100 SPEED : 320.00 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 250 SPEED : 0.00 km/h --------------------------------------------------------------------------------2. Testing loadPeople. ---------------------------------------- NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 201 SPEED : 320.00 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 195 SPEED : 0.00 km/h NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 1000 SPEED : 320.00 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 0 SPEED : 0.00 km/h --------------------------------------------------------------------------------3. Testing transfer. ---------------------------------------- NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 1000 SPEED : 320.00 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 0 SPEED : 0.00 km/h NAME OF THE TRAIN : VIA Rail Abitibi NUMBER OF PEOPLE : 1000 SPEED : 0.00 km/h NAME OF THE TRAIN : Bullet Train NUMBER OF PEOPLE : 55 SPEED : 245.95 km/h --------------------------------------------------------------------------------4. Testing transfer (safe empty states).-----------------------------------------1-1----------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

Relational Contexts in Organizations

Answered: 1 week ago