Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With the class (Soccer) and the given SimpleVector class, make a new class called SortableVector which will sort an array of the object Soccer via

With the class (Soccer) and the given SimpleVector class, make a new class called SortableVector which will sort an array of the object Soccer via the number of points scored by the players or by the number of the players.

//Please follow a similair template for SortableVector

//SortableVector_h

#include "SimpleVector.h"

template class SortableVector :public SimpleVector { public: //default SortableVector() :SimpleVector() {}

//Constructor SortableVector(int size) :SimpleVector(size) {}

//Copy SortableVector(const SortableVector&); //Accessor void SelectionSort(); };

//Copy Constructor template SortableVector::SortableVector(const SortableVector& obj) :SearchableVector(obj.getSize()) { for (int count = 0; count < this->getSize(); count++)

this->operator[](count) = obj[count]; }

//template /* void SortableVector::SelectionSort() { int startScan, minIndex; T minValue;

for (startScan = 0; startScan < (this->getSize() - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for (int index = startScan + 1; index < this->getSize(); index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } */

//SimpleVector_h

#ifndef SimpleVector_h #define SimpleVector_h // SimpleVector class template #include #include // Needed for bad_alloc exception #include // Needed for the exit function using namespace std;

template class SimpleVector { private: T* aptr; int arraySize; void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public: // Default constructor SimpleVector() { aptr = 0; arraySize = 0; }

// Constructor declaration SimpleVector(int); // Copy constructor declaration SimpleVector(const SimpleVector&); // Destructor declaration ~SimpleVector(); // Accessor to return the array size int getSize() const { return arraySize; }

// Accessor to return a specific element T getElementAt(int position);

// Overloaded [] operator declaration T& operator[](const int&);

void setValue(T value, int index); }; template SimpleVector::SimpleVector(int s) { arraySize = s; // Allocate memory for the array. try { aptr = new T[s]; } catch (bad_alloc) { memError(); }

// Initialize the array. //for (int count = 0; count < arraySize; count++) // *(aptr + count) = 0; }

//******************************************* // Copy Constructor for SimpleVector class. * //*******************************************

template SimpleVector::SimpleVector(const SimpleVector& obj) { // Copy the array size. arraySize = obj.arraySize;

// Allocate memory for the array. aptr = new T[arraySize]; if (aptr == 0) memError(); // Copy the elements of obj's array. for (int count = 0; count < arraySize; count++) *(aptr + count) = *(obj.aptr + count); }

//************************************** // Destructor for SimpleVector class. * //**************************************

template SimpleVector::~SimpleVector() { if (arraySize > 0) delete[] aptr; }

//******************************************************** // memError function. Displays an error message and * // terminates the program when memory allocation fails. * //********************************************************

template void SimpleVector::memError() { cout << "ERROR:Cannot allocate memory. "; exit(EXIT_FAILURE); }

//************************************************************ // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * //************************************************************

template void SimpleVector::subError() { cout << "ERROR: Subscript out of range. "; exit(EXIT_FAILURE); }

//******************************************************* // getElementAt function. The argument is a subscript. * // This function returns the value stored at the * // subcript in the array. * //*******************************************************

template T SimpleVector::getElementAt(int sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; }

//******************************************************** // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * //********************************************************

template T& SimpleVector::operator[](const int& sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; }

template void SimpleVector::setValue(T value, int index) { aptr[index] = value; }

#endif

/////////////////////////////////////////////////////////////////////////////////////////////////

///Soccer_h

#include #include using namespace std; class Soccer { friend ostream& operator<<(ostream&, const Soccer&); friend istream& operator>>(istream&, Soccer&); private: string name; // Player's name int number; // Player's number int points; // Points scored by a player public: Soccer(); Soccer(string aName, int aNumber, int p); Soccer(const Soccer&); ~Soccer(); void setName(string); void setNumber(int); void setPoints(int); string getName() const; int getNumber() const; int getPoints() const; Soccer& operator=(const Soccer&); Soccer& operator[](const Soccer& obj); }; char temp2[20] = "No player assigned";

Soccer::Soccer() : name(temp2), number(0), points(0) { int number = 10; int points = 30; } Soccer::Soccer(string aName, int aNumber, int p) : name(aName), number(aNumber), points(p) { setNumber(30); setPoints(20); } Soccer::Soccer(const Soccer& obj) : name(obj.name), number(obj.number), points(obj.points) { }

Soccer::~Soccer() {

}

void Soccer::setName(string aName) { name = aName; }

void Soccer::setNumber(int aNumber) { number = aNumber; }

void Soccer::setPoints(int p) { points = p; }

string Soccer::getName() const { return this->name; }

int Soccer::getNumber() const { return this->number; }

int Soccer::getPoints() const { return this->points; }

Soccer& Soccer::operator=(const Soccer& obj) { this->name = obj.getName(); this->number = obj.getNumber(); this->points = obj.getPoints(); return *this; }

Soccer& Soccer::operator[](const Soccer& obj) { this->name = obj.getName(); this->number = obj.getNumber(); this->points = obj.getPoints(); return *this; }

ostream& operator<<(ostream& output, const Soccer& obj) { output << "Player's name: " << obj.name << endl; output << "Player's number: " << obj.number << endl; output << "Player's scored points: " << obj.points << endl; return output; }

istream& operator>>(istream& input, Soccer& obj) { cin.ignore(); cout << "Nombre: "; input >> obj.name; cout << "Numero: "; input >> obj.number; cout << "Puntos acumulados: "; input >> obj.points; return input; }

///////////////////////////////////////////////////////////////////////////////////////////

//main.cpp

#include "Soccer.h" #include #include "SortableVector.h"

int main()

{

int size;

cin >> size;

SortableVector SoccerTable(n);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

Students also viewed these Databases questions