In: Computer Science
WRITE ONLY THE TO DO'S in FINAL program IN C++ (necessary cpp and header files are witten below)
"KINGSOM.h " program below:
#ifndef WESTEROS_kINGDOM_H_INCLUDED
#define WESTEROS_KINGDOM_H_INCLUDED
#include <iostream>
namespace westeros {
class Kingdom{
public:
char m_name[32];
int m_population; };
void display(Kingdom&); }
#endif }
Kingdom.cpp Program below:
#include <iostream>
#include "kingdom.h"
using namespace std;
namespace westeros {
void display(Kingdom& pKingdom) {
cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;
FINAL:
#include<iostream>
#include"kingdom.h"
usingnamespace std;
usingnamespace westeros;
int main(void)
{
int count = 0; // the number of kingdoms in the array
// TODO: declare the pKingdoms pointer here (don't forget to initialize it)
cout <<"=========="<< endl
<<"Input data"<< endl
<<"=========="<< endl
<<"Enter the number of kingdoms: ";
cin >> count;
cin.ignore();
// TODO: allocate dynamic memory here for the pKingdoms pointer
for (int i = 0; i < count; ++i)
{
// TODO: add code to accept user input for the pKingdoms array
}
cout <<"=========="<< endl << endl;
// testing that "display(...)" works
cout <<"------------------------------"<< endl
<<"The first kingdom of Westeros"<< endl
<<"------------------------------"<< endl;
display(pKingdoms[0]);
cout <<"------------------------------"<< endl << endl;
// testing that the first overload of "display(...)" works
display(pKingdoms, count);
cout << endl;
// testing that the second overload of "display(...)" works
display(pKingdoms, count, 345678);
cout << endl;
// testing that the third overload of "display(...)" works
display(pKingdoms, count, "Mordor");
cout << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
// TODO: deallocate the dynamic memory here
return0;
}
OUTPUT SAMPLE:
Output Sample:
==========
Input data
==========
Enter the number of kingdoms: 5
Enter the name for kingdom #1: The_Riverlands
Enter the number people living in The_Riverlands: 123456
Enter the name for kingdom #2: The_Vale
Enter the number people living in The_Vale: 234567
Enter the name for kingdom #3: The_Westernlands
Enter the number people living in The_Westernlands: 345678
Enter the name for kingdom #4: The_Stormlands
Enter the number people living in The_Stormlands: 456789
Enter the name for kingdom #5: The_Reach
Enter the number people living in The_Reach: 567890
==========
------------------------------
The first kingdom of Westeros
------------------------------
The_Riverlands, population 123456
------------------------------
------------------------------
Kingdoms of Westeros
------------------------------
1. The_Riverlands, population 123456
2. The_Vale, population 234567
3. The_Westernlands, population 345678
4. The_Stormlands, population 456789
5. The_Reach, population 567890
------------------------------
Total population of Westeros: 1728380
------------------------------
------------------------------
Kingdoms of Westeros with more than 345678 people
------------------------------
The_Westernlands, population 345678
The_Stormlands, population 456789
The_Reach, population 567890
------------------------------
------------------------------
Searching for kingdom Mordor in Westeros
------------------------------
Mordor is not part of Westeros.
------------------------------
------------------------------
Searching for kingdom The_Vale in Westeros
------------------------------
The_Vale, population 234567
------------------------------
//main.cpp
#include <iostream>
#include "kingdom.h"
using namespace std;
using namespace westeros;
int main(void) {
int count = 0; // the number of kingdoms in the array
// TODO: declare the pKingdoms pointer here (don't forget to
initialize it)
Kingdom *pKingdoms;
cout << "==========" << endl
<< "Input data" << endl
<< "==========" << endl
<< "Enter the number of kingdoms: ";
cin >> count;
cin.ignore();
// TODO: allocate dynamic memory here for the pKingdoms
pointer
pKingdoms = new Kingdom[count];
for (int i = 0; i < count; ++i) {
// TODO: add code to accept user input for the kingdoms array
cout << "Enter the name for kingdom #" << i + 1
<< ": ";
cin >> pKingdoms[i].m_name;
cout << "Enter the number people living in " <<
pKingdoms[i].m_name
<< ": ";
cin >> pKingdoms[i].m_population;
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << "------------------------------" << endl
<< endl;
// testing that the first overload of "display(...)" works
display(pKingdoms, count);
cout << endl;
// testing that the second overload of "display(...)"
works
display(pKingdoms, count, 345678);
cout << endl;
// testing that the third overload of "display(...)" works
display(pKingdoms, count, "Mordor");
cout << endl << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
// TODO: deallocate the dynamic memory here
delete[] pKingdoms;
return 0;
}
========================================================================
//kingdom.cpp
// TODO: include the necessary headers
#include "kingdom.h"
#include <cstring>
#include <iostream>
using namespace std;
// TODO: the westeros namespace
namespace westeros {
// TODO:definition for display(...)
void display(Kingdom &kingdom) {
cout << kingdom.m_name << ", population " <<
kingdom.m_population << endl;
}
void display(Kingdom kingdom[], int num) {
int TOTAL_POPULATION = 0;
cout << "------------------------------" << endl;
cout << "Kingdoms of Westeros" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
cout << i + 1 << ". " << kingdom[i].m_name
<< ", population "
<< kingdom[i].m_population << endl;
TOTAL_POPULATION += kingdom[i].m_population;
}
cout << "------------------------------" << endl;
cout << "Total population of Westeros: " <<
TOTAL_POPULATION << endl;
cout << "------------------------------" << endl;
}
void display(Kingdom kingdom[], int num, int MIN_POPULATION)
{
cout << "------------------------------" << endl;
cout << "Kingdoms of Westeros with more than " <<
MIN_POPULATION
<< " people" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
if (kingdom[i].m_population >= MIN_POPULATION) {
cout << kingdom[i].m_name << ", population "
<< kingdom[i].m_population << endl;
}
}
cout << "------------------------------" << endl;
}
void display(Kingdom kingdom[], int num, const char
*KINGDOM_NAME) {
bool kingdom_found = false;
cout << "------------------------------" << endl;
cout << "Searching for kingdom " << KINGDOM_NAME
<< " in Westeros" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
if (!strcmp(kingdom[i].m_name, KINGDOM_NAME)) {
cout << kingdom[i].m_name << ", population "
<< kingdom[i].m_population << endl;
kingdom_found = true;
}
}
if (!kingdom_found) {
cout << KINGDOM_NAME << " is not part of Westeros."
<< endl;
}
cout << "------------------------------" << endl;
}
}
==================================================================
//kingdom.h
// TODO: header safeguards
#ifndef KINGDOM_H
#define KINGDOM_H
// TODO: westeros namespace
namespace westeros {
// TODO: define the class Kingdom in the westeros
namespace
class Kingdom {
public:
char m_name[32];
int m_population;
};
// TODO: add the declaration for the function display(...),
// also in the westeros namespace
void display(Kingdom&);
void display(Kingdom[], int);
void display(Kingdom[], int, int);
void display(Kingdom[], int, const char*);
}
#endif
====================================================================
sample output: