Design and code a class named Kingdom in the namespace westeros. The class shoul
ID: 3784150 • Letter: D
Question
Design and code a class named Kingdom in the namespace westeros. The class should have two public members:
m_name: a statically allocated array of characters of size 32 (including '') that holds the name of the kingdom;
m_population: an integer that stores the number of people living in the kingdom.
Add to the westeros namespace, a function a function called display(...) that returns nothing, receives as a parameter a reference to an object of type Kingdom and prints to the screen the parameter in the following format:
KINGDOM_NAME, population POPULATION<ENDL>
Put the class definition and the westeros::display(...) declaration in a header named kingdom.h. Put the implementation of westeros::display(...) in a file named kingdom.cpp.
Complete the implementation of the w2_in_lab.cpp main module shown below (see the parts marked with TODO).
#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 kingdoms 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 kingdoms pointer
for (int i = 0; i < count; ++i)
{
// TODO: add code to accept user input for the kingdoms array
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << "------------------------------" << endl << endl;
// TODO: deallocate the dynamic memory here
return 0;
}
Explanation / Answer
#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 kingdoms 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 kingdoms pointer
for (int i = 0; i < count; ++i)
{
// TODO: add code to accept user input for the kingdoms array
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << "------------------------------" << endl << endl;
// TODO: deallocate the dynamic memory here
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.