Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// C++ programming, write an implementation of a class // ***** Write Country.cp

ID: 3686588 • Letter: #

Question

// C++ programming, write an implementation of a class   

// ***** Write Country.cpp file which works with SortingList and Main with a text file contant name, population, area **************************

#ifndef COUNTRY_H

#define   COUNTRY_H

// Country.h

#include <iostream>

#include <string>

using namespace std;

class Country

{

public:

Country();

Country(string n, long p, double a);

string get_name() const;

long get_population() const;

double get_area() const;

static int compareByName(Country C1, Country C2);

static int compareByPopulation(Country C1, Country C2);

friend ostream &operator<<(ostream &out, const Country &C);

friend class SortingList;

private:

string name;

long population;

double area; // square kilometers

};

#endif   /* COUNTRY_H */

// main file

#include <iostream>

#include <fstream>

#include <string>

#include <sstream>

#include "Country.h"

#include "SortingList.h"

using namespace std;

int main(int argc, char** argv) {

ifstream in_file;

  

if (argc != 2)

{

cout << "It needs one command line argument: <input filename>!" << endl;

return 1;

}

  

in_file.open(argv[1]);

if (!in_file.is_open())

{

cout << "Failure in opening file: " << argv[1] << endl;

return 2;

}

  

SortingList country_SL;

while (!in_file.eof())

{

string name;

long population;

double area;

in_file >> name >> population >> area;

if (name != "")

{

Country input_cty(name, population, area);

country_SL.insertAtTail(input_cty);

}

}

  

cout << endl;

cout << "The sequence of objects before sorting: " <<endl;

country_SL.print();

int (*compare)(Country C1, Country C2);

compare = &Country::compareByName;

country_SL.bubble_sort((*compare));

cout << endl;

cout << "The sequence of objects after sorting by name: " <<endl;

country_SL.print();

compare = &Country::compareByPopulation;

country_SL.bubble_sort((*compare));

cout << endl;

cout<< "The sequence of objects after sorting by population: " << endl;

country_SL.print();

in_file.close();

  

return 0;

}

// sortinglist header

#ifndef SORTINGLIST_H

#define   SORTINGLIST_H

#include <iostream>

#include "Country.h"

using namespace std;

const int INITIAL_CAPACITY = 5;

class SortingList

{

public:

SortingList();

SortingList(int incapacity);

SortingList(const SortingList &other);

~SortingList();

void insertAtTail(Country item); // insert a new item at the tail of the list

int GetSize() const { return size; }

Country &Element(int x); // return reference to an element at index of x

void print() const;

void bubble_sort(int (*compare)(Country C1, Country C2));

SortingList &operator=(const SortingList &other); // assignment operator overloading

  

private:

int capacity; // the capacity of the dynamic array(list)

int size; // the total number of elements in the list

Country *p; // pointer pointing to the first element in the list

};

#endif   /* SORTINGLIST_H */

//sortinglist.cpp file

#include <cassert>

#include "Country.h"

#include "SortingList.h"

SortingList::SortingList()

{

capacity = INITIAL_CAPACITY;

size = 0;

p = new Country[capacity];   // create a dynamic array of string objects

assert(p != NULL);

}

SortingList::SortingList(int incapacity)

{

assert(incapacity > 0);

capacity = incapacity;

p = new Country[capacity];   // create a dynamic array of string objects

assert(p != NULL);

size = 0;}

SortingList::SortingList(const SortingList &other) : capacity(other.capacity), size(other.size)

{

p = new Country[capacity]; // create array of base type objects

assert(p != 0);

for (int i = 0; i < size; i++)

{

p[i] = other.p[i];}}

SortingList::~SortingList()

{

delete [] p;   // release the dynamic array

}

void SortingList::insertAtTail(Country item)

{

size = size + 1;

if (size >= capacity)

{

// No more room: allocate more.

capacity *= 2;

Country* copy = new Country[capacity];

assert(copy != NULL);

// Copy over the data

for (int i = 0; i < size; i++)

copy[i] = p[i];

// Free the old copy and point to the new one.

delete[] p;

p = copy;

}

p[size-1] = item;

}

Country &SortingList::Element(int x)

{

assert(x >= 0 && x < size);

return p[x];

}

void SortingList::print() const

{

for (int x = 0; x < size; x++)

{cout << p[x] << endl;

}}

SortingList &SortingList::operator=(const SortingList &other)

{

if (this != &other)

{

assert(size == other.size);

for (int i = 0; i < size; i++)

p[i] = other.p[i];

}

return *this;

}

void SortingList::bubble_sort(int (*compare)(Country C1, Country C2))

{

Country temp; // for swapping

for (int i = 0 ; i < size-1 ; i++)

{

for (int j = 0 ; j < size-1 ; j++)

{

if ( (*compare)(p[j], p[j + 1]) >0 )

{

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

} }}}

Explanation / Answer

// Country.cpp

#include "Country.h"

//default constructor
Country::Country()
{}

//parameterized constructor
   Country::Country(string n, long p, double a)
   {
       name=n;
       population=p;
       area=a;
   }
string Country::get_name() const
{
   return name;
   }
long Country::get_population() const
{
   return population;
   }
double Country::get_area() const
{
   return area;
   }
ostream &operator<<(ostream &out, const Country &C)
{
   out<<endl<<"Country details";
   out<<endl<<"Name: "<<C.name;
   out<<endl<<"Population: "<<C.population;
   out<<endl<<"Area: "<<C.area;
   return out;
}
Country::compareByName(Country C1, Country C2)
{
   int flag=0;
   //checking both country names are same
   if(C1.get_name().compare(C2.get_name())==0)
   {
       flag=1;
   }
   return flag;
}

Country::compareByPopulation(Country C1, Country C2)
{
   int flag=0;
   //checking both country populations are same
   if(C1.get_population()==C2.get_population())
   {
       flag=1;
   }
   return flag;
}