// ***** Write Country.cpp file which works with SortingList and Main with a tex
ID: 3686915 • Letter: #
Question
// ***** 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;
} }}}
// countryinfo. text test file
Italy 61016804 116305
United-States 313232044 3718691
France 65312249 211208
Mexico 113724226 761602
Spain 46754784 194896
Japan 126475664 145882
United-Kingdom 62698362 94525
Greece 10760136 50942
Vietnam 90549390 127243
Sudan 45047502 728215
Zambia 13881336 290584
Switzerland 7639961 15942
China 1336718015 3705386
Denmark 5529888 16639
Fiji 883125 7054
Turkey 78785548 302382
Australia 21766711 2967893
Write The Class Country.cpp With SortingList.cpp and Main.cPp! This program takes one command line argument, which represents the name of the input file. It reads the country information from the input file and stores into a dynamic array, which is one of the private data from the class named SortingList. Each element in the array is an object of the class Country. By calling the member function bubble sort( ) of class SortingList, it will sort all the elements in the array in the certain order defined by a function which passes in to the function bubble_sort() as a parameter. In C++, you can use "pointer to functions" to pass a function as an argument to another function, so that it can simplity code by providing a simple way to select a function r member function execute based on runtime values. For example, in Lab10, we pass in either member function compareByPopulation() of class Country, to the member function bubble_ sort() of class SortingList, to sort all the Country objects in the lexicographical order by name or in the ascending order by population respectively. In order to do this, the bubble_sort() function of Sorting List class has the following prototype: void bubble sort(int ("compare)(Country C1, Country C2)); When this function is called in the main function, it passes in the member function compareByName() as the argument first, then the second time it passes in the member function compareByPopulation() as the argument. Since member functions are usually called using either an object or a this pointer, and we do not really need it to be associated to an object of the class for our sorting procedure, hence we can declare these two member functions of class Country to be static member functions. You need to provide the implementations of these two member functions in Country.cpp so that they should match the corresponding sorting requirement (either in lexicographical order by name or in ascending order by population). You also need to provide your definition of the insertion operator " for the class Country (operatorExplanation / 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.