Question
The class must have private data members to include the following information: a) the nationality of an Army, b) the number of units in an
The class must have private data members to include the following information: a) the nationality of an Army, b) the number of units in an Army, and e) the power of an Army.
Declare one C++ constant, namely MAX_NAME_LEN and set it to 50.
Declare three private data members.
- Declare an array to hold the nationality of an Army, not exceeding MAX_NAME_LEN characters.
- Declare an integer variable for the number of units in an Army.
- Declare a double variable for the power of an Army.
Store your class definition in a header file named Army.h and your member function definitions in an implementation file named Army.cpp.
Public Member functions
Declare and implement the following member functions.
setEmpty:
void setEmpty();
This member function initializes the Army object to a safe empty state. An Army is in an empty state when it has an empty nationality and no units.
createArmy:
void createArmy(const char* country, double pow, int troops);
It uses three input parameters to initialize an Army object. It receives the nationality of an Army, the number of units in an Army and the power of the Army. It validates the data values received and uses these data values to set the respective data members, only if all of them are valid.
All the data values are valid if a) the first parameter is not nullptr and the length of the name is greater than zero, b) the number of units is greater than zero c) the power is greater than zero.
Note: Only copy the first MAX_NAME_LEN characters from the input parameter to the cString data member that holds the nationality of an Army. (you could use
If the data values are invalid, the member function initializes the Army object to a safe empty state.
updateUnits:
void updateUnits(int troops); Update your Army by incrementing the units with the amount of provided troops also the power of the Army will be increased by a value equal to 25% of the newly provided troops.
checkNationality:
const char* checkNationality() const;
This query returns the nationality of an Army.
checkCapacity:
int checkCapacity() const;
This query returns the number of units in an Army.
checkPower:
double checkPower() const;
This query returns the power of an Army.
isEmpty:
bool isEmpty() const;
This query returns true if the Army object is in a safe empty state; false otherwise.
isStrongerThan:
bool isStrongerThan(const Army& army)const;
This function returns true if the Army has more power than the incoming Army, and returns false otherwise.
Global (stand-alone) functionsbattle:
void battle( Army& arm1, Army& arm2);
This function will force both armies to fight. If both armies are valid, the losing Army will lose 50% of their unit count PS: - you might want to use the isStrongerThan function to check which Army will win - you might want to use the updateUnits function to update the losing country. Passed value needs to be casted to an int
display:
void displayDetails(const Army* armies, int size);
This function sends the information about an Army array to standard output in the following format if the object holds valid data values.
Armies reporting to battle:
Each Army in the array will be printed in the following format.
Nationality: XXXXXXXXXXX, Units Count: XXXX, Power left:
Testing Program
main.cpp
#include
using namespace std;
#include "Army.h"
using namespace sdds;
int main() {
Army armies[6]{};
armies[0].createArmy("Atlantis", 500.5, 1000);
armies[1].createArmy("", 100.0, 100);
armies[2].createArmy("Ambrosia", 215.0, 520);
armies[3].createArmy("United States of Auradon", 220.5, 751);
armies[4].createArmy("Azmenistan", 250.0, 750);
armies[5].createArmy(nullptr, 250.5, 750);
//Test 1
cout << "----------------------------------------" << endl;
cout << "1. Testing the validation logic." << endl;
cout << "(only 4 armies should be valid (0,2,3,4) )" << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i < 6; i++)
{
cout << "armies[" << i << "]: "
<< (armies[i].isEmpty() ? "not valid" : "valid") << endl;
}
cout << "----------------------------------------" << endl << endl;
//Test 2
cout << "----------------------------------------" << endl;
cout << "2. Testing the display function." << endl;
cout << "----------------------------------------" << endl;
displayDetails(armies, 6);
cout << "----------------------------------------" << endl << endl;
// Test 3
cout << "----------------------------------------" << endl;
cout << "3. Testing the member functions without the update." << endl;
cout << "----------------------------------------" << endl;
Army backup;
backup.createArmy("Foreigners", 100, 100);
cout << backup.checkNationality()
<< ','
<< backup.checkCapacity()
<< ','
<< backup.checkPower() << endl;
cout << "----------------------------------------" << endl << endl;
// Test 4
cout << "----------------------------------------" << endl;
cout << "4. Testing the member functions with the update." << endl;
cout << "----------------------------------------" << endl;
backup.updateUnits(100);
cout << backup.checkNationality()
<< ','
<< backup.checkCapacity()
<< ','
<< backup.checkPower() << endl;
cout << "----------------------------------------" << endl << endl;
// Test 5
cout << "----------------------------------------" << endl;
cout << "5. Testing the battle function." << endl;
cout << "----------------------------------------" << endl;
battle(armies[0],armies[2]);
battle(armies[3], armies[4]);
displayDetails(armies, 6);
cout << "----------------------------------------" << endl << endl;
return 0;
}
Sample Output
----------------------------------------
1. Testing the validation logic.
(only 4 armies should be valid (0,2,3,4) )
----------------------------------------
armies[0]: valid
armies[1]: not valid
armies[2]: valid
armies[3]: valid
armies[4]: valid
armies[5]: not valid
----------------------------------------
----------------------------------------
2. Testing the display function.
----------------------------------------
Armies reporting to battle:
Nationality: Atlantis, Units Count: 1000, Power left: 500.5
Nationality: Ambrosia, Units Count: 520, Power left: 215.0
Nationality: United States of Auradon, Units Count: 751, Power left: 220.5
Nationality: Azmenistan, Units Count: 750, Power left: 250.0
----------------------------------------
----------------------------------------
3. Testing the member functions without the update.
----------------------------------------
Foreigners,100,100.0
----------------------------------------
----------------------------------------
4. Testing the member functions with the update.
----------------------------------------
Foreigners,200,125.0
----------------------------------------
----------------------------------------
5. Testing the battle function.
----------------------------------------
In battle of Atlantis and Ambrosia, Atlantis is victorious!
In battle of United States of Auradon and Azmenistan, Azmenistan is victorious!
Armies reporting to battle:
Nationality: Atlantis, Units Count: 1000, Power left: 500.5
Nationality: Ambrosia, Units Count: 260, Power left: 150.0
Nationality: United States of Auradon, Units Count: 376, Power left: 126.8
Nationality: Azmenistan, Units Count: 750, Power left: 250.0
----------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started