In: Computer Science
Learning Outcomes
Instructions
Our last program was to maintain a list of Planets in the STL’s vector class. In this assignment, you will be writing your own vector class. Implement class vector consisting of an array of a generic type.
This class will require the following fields:
Your vector class must also support the following public member functions. (Please define everything in a vector.hpp file!):
How to implement operator[]
The operator[] requires that you add TWO methods to your vector class (one which is used for reading and the other for writing). You probably haven’t implemented this before, so you’ll be given the code. Here are the prototypes used in the vector class definition. The int passed to each method represents the index desired by the user.
T&
operator[](int);
// Write operation.
const T& operator[](int) const; // Read operation.
The implementation of these two methods is identical: return the element in your array at the desired index. These examples assume that your data array is named mData.
template <class T>
T& vector<T>::operator[](int index) {
return mData[index];
}
template <class T>
const T& vector<T>::operator[](int index) const {
return mData[index];
}
Once these two methods are added to your vector class, you may now do things such as this (provided that everything else in your vector is written correctly). Don’t put this into your code. This serves as an example of what you can do.
vector<int> list;
list.push_back(10);
list.push_back(20);
list.push_back(30);
std::cout << "Last element: " << list[list.size()-1]
<< std::endl;
Return to the listing of planets.
Return to your Planets library. In your Planets.h file, remove the import statements to <vector> and replace those with the local version of “vector.hpp” that you made. You will have to modify some of methods in this class in order to use your vector instead of the STL vector.
*************************************************************
* Code for Planet.h, Planets.h, and main.cpp *
*************************************************************
Planet.cpp
#include <string>
using namespace std;
class Planet {
private:
string name;
string madeOf;
int alienPopulation;
public:
Planet() {}
Planet(string name, string madeOf, int alienPopulation) {
if(name.empty())
throw "Planet Name Empty";
if(madeOf.empty())
throw "Made of Empty";
if(alienPopulation < 0)
throw "Alien Population Empty";
this->name = name;
this->madeOf = madeOf;
this->alienPopulation = alienPopulation;
}
string getName() {
return name;
}
string getMadeOf() {
return madeOf;
}
int getAlienPopulation() {
return alienPopulation;
}
};
Planets.cpp
#include <vector>
#include <climits>
#include "Planet.cpp"
using namespace std;
class Planets {
private:
vector<Planet> planets;
public:
void addPlanet(Planet p) {
planets.push_back(p);
}
int getCount() {
return planets.size();
}
Planet getMostPopulatedPlanet() {
int mostPop = INT_MIN;
int mostPopPlanet = -1;
for(int i = 0; i < planets.size(); i++) {
if(planets[i].getAlienPopulation() > mostPop) {
mostPop = planets[i].getAlienPopulation();
mostPopPlanet = i;
}
}
return planets[mostPopPlanet];
}
Planet get(int i) {
if(i < 0 || i > getCount() - 1)
throw "ArrayIndexOutOfBound";
return planets[i];
}
};
main.cpp
#include <iostream>
#include "Planets.cpp"
using namespace std;
Planet newPlanet( ) {
cout << "Enter the Name, MadeOf, and Alien Population of the
planet :\n";
string name, madeOf;
int population;
cin >> name >> madeOf >> population;
Planet p(name,madeOf,population);
return p;
}
int main() {
int choice;
bool run = true;
Planet temp;
Planets p;
while(run) {
cout << "*******Planets Menu*********\n";
cout << "****************************\n";
cout << "1. Add Planet\n";
cout << "2. Get Planet Count\n";
cout << "3. Get the Most Populated Planet\n";
cout << "4. Exit\n";
cout << "Enter your choice:\t";
cin >> choice;
switch(choice) {
case 1:
p.addPlanet(newPlanet());
break;
case 2:
cout << "Number of Planet(s): " << p.getCount()
<< endl;
break;
case 3:
temp = p.getMostPopulatedPlanet();
cout << "the most populated planet is: " <<
temp.getName() << " with " << temp.getAlienPopulation()
<< " that is made of " << temp.getMadeOf() <<
endl;
break;
case 4:
run = false;
break;
}
}
return 0;
}
// vector.hpp
#ifndef VECTOR_HPP_
#define VECTOR_HPP_
#include <iostream>
template <class T>
class vector
{
public:
vector(); // default constructor
vector(const vector&); // copy constructor
~vector(); // destructor
int size(); // return size of vector
void push_back(const T); // insert the element at the end of vector
T& operator[](int); // Write operation.
const T& operator[](int) const; // Read operation.
private:
int mCapacity;
int mSize;
T *mData;
};
// default constructor to create an empty vector of capacity 10
template <class T>
vector<T>::vector()
{
mCapacity = 10;
mSize = 0;
mData = new T[mCapacity];
}
// copy constructor to create a vector same as other vector
template <class T>
vector<T>::vector(const vector<T> &other)
{
mCapacity = other.mCapacity;
mSize = other.mSize;
mData = new T[mCapacity];
for(int i=0;i<mSize;i++)
mData[i] = other.mData[i];
}
// function to return the number of elements currently in the vector
template <class T>
int vector<T>::size()
{
return mSize;
}
// function to add an element at the end of vector if there is room, else increase the size to twice its capacity and insert the element
template <class T>
void vector<T>:: push_back(const T val)
{
if(mSize == mCapacity) // check if vector is full, then increment its size
{
T *temp = new T[2*mCapacity];
mCapacity = 2*mCapacity;
for(int i=0;i<mSize;i++)
temp[i] = mData[i];
delete [] mData;
mData = temp;
}
// insert the element at the end
mData[mSize] = val;
mSize++;
}
// function to store the element at index
template <class T>
T& vector<T>::operator[](int index) {
if(index >=0 && index < mSize)
return mData[index];
else
throw std::invalid_argument("Index out of bounds.");
}
// function to return the element at index
template <class T>
const T& vector<T>::operator[](int index) const {
if(index >=0 && index < mSize)
return mData[index];
else
throw std::invalid_argument("Index out of bounds.");
}
// destructor to release the memory allocated to mData dynamically
template <class T>
vector<T>::~vector()
{
delete [] mData;
}
#endif /* VECTOR_HPP_ */
//end of vector.hpp
// Planet.cpp
#include <string>
using namespace std;
class Planet {
private:
string name;
string madeOf;
int alienPopulation;
public:
Planet() {}
Planet(string name, string madeOf, int alienPopulation) {
if(name.empty())
throw "Planet Name Empty";
if(madeOf.empty())
throw "Made of Empty";
if(alienPopulation < 0)
throw "Alien Population Empty";
this->name = name;
this->madeOf = madeOf;
this->alienPopulation = alienPopulation;
}
string getName() {
return name;
}
string getMadeOf() {
return madeOf;
}
int getAlienPopulation() {
return alienPopulation;
}
};
//end of Planet.cpp
// Planets.cpp
//#include <vector>
#include "vector.hpp"
#include <climits>
#include "Planet.cpp"
using namespace std;
class Planets {
private:
vector<Planet> planets;
public:
void addPlanet(Planet p) {
planets.push_back(p);
}
int getCount() {
return planets.size();
}
Planet getMostPopulatedPlanet() {
int mostPop = INT_MIN;
int mostPopPlanet = -1;
for(int i = 0; i < planets.size(); i++) {
if(planets[i].getAlienPopulation() > mostPop) {
mostPop = planets[i].getAlienPopulation();
mostPopPlanet = i;
}
}
return planets[mostPopPlanet];
}
Planet get(int i) {
if(i < 0 || i > getCount() - 1)
throw "ArrayIndexOutOfBound";
return planets[i];
}
};
//end of Planets.cpp
// main.cpp
#include <iostream>
#include "Planets.cpp"
using namespace std;
Planet newPlanet( ) {
cout << "Enter the Name, MadeOf, and Alien Population of the planet :\n";
string name, madeOf;
int population;
cin >> name >> madeOf >> population;
Planet p(name,madeOf,population);
return p;
}
int main() {
int choice;
bool run = true;
Planet temp;
Planets p;
while(run) {
cout << "*******Planets Menu*********\n";
cout << "****************************\n";
cout << "1. Add Planet\n";
cout << "2. Get Planet Count\n";
cout << "3. Get the Most Populated Planet\n";
cout << "4. Exit\n";
cout << "Enter your choice:\t";
cin >> choice;
switch(choice) {
case 1:
p.addPlanet(newPlanet());
break;
case 2:
cout << "Number of Planet(s): " << p.getCount() << endl;
break;
case 3:
temp = p.getMostPopulatedPlanet();
cout << "the most populated planet is: " << temp.getName() << " with " << temp.getAlienPopulation() << " that is made of " << temp.getMadeOf() << endl;
break;
case 4:
run = false;
break;
}
}
return 0;
}
//end of main.cpp
Output: