Assignment Help| Declare and implement two member functions and a global function

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 <iostream>
#include <cstring>
#include <iomanip>
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
----------------------------------------

https://filetransfer.io/data-package/Od3TZFsc#link

https://filetransfer.io/data-package/Od3TZFsc#link

https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS03

https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS03

Calculate your order
Pages (275 words)
Standard price: $0.00
Open chat
1
towriteessays.com
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?