in c++ language, I already solved the problem and ran it fine. But I got a pile of warning, I wonder if it will affect my code to run. Here’s the problem from the exercise. (Implement a stack class using inheritance) In listing 12.4, GenericStack is implemented using arrays. Create a new stack class that extends vector. Implement it.
[UPDATED]: Just ignore all header files and problems. All I want you to do is on prog 15_5().
MyVector.h
#ifndef MYVECTOR
#define MYVECTOR
#include
template
class MyVector
{
public:
MyVector();
void push_back(T);
void pop_back();
unsigned int size();
bool isEmpty();
T at(int index);
T peek();
int clear();
void swap(MyVector v2);
private:
T elements[100];
int vectorSize;
};
#endif
MyVector.cpp
#include “MyVector.h”
template
// no-argument generic constructor definition
MyVector::MyVector()
{
vectorSize = 0;
}
template
bool MyVector::isEmpty()
{
return (vectorSize == 0);
}
template
T MyVector::at(int index)
{
return elements[index];
}
template
void MyVector::push_back(T value)
{
elements[vectorSize++] = value;
}
template
void MyVector::pop_back()
{
elements[–vectorSize];
}
template
T MyVector::peek()
{
return elements[0];
}
template
unsigned int MyVector:: size()
{
return vectorSize;
}
template
int MyVector::clear()
{
vectorSize = 0;
return vectorSize;
}
template
void MyVector::swap(MyVector v2)
{
T temp[100];
int tempSize=v2.size();
for(int i=0;i temp[i]=v2.at(i);
this->clear();
for(int i=0;i this->push_back(temp[i]);
/*T temp[100];
int tempSize = v2.size();
for (int i = 0; i < v2.size(); i++)
temp.push_back(at(i));
clear();
for (int i = 0; i this->push_back(temp[i]);*/
}
main.cpp
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include “Triangle.h”
#include “GeometricObject.h”
#include “MyVector.cpp”
#include “Person.h”
#include “MyDate.h”
#include “Faculty.h”
#include “Staff.h”
#include “Calendar.h”
#include “ImprovedMyPoint.h”
#include “ThreeDPoint.h”
#include “NewAccount.h”
#include “CheckingAcc.h”
#include “SavingAcc.h”
#include “MyVector.h”
using namespace std;
//to solve a problem #15_5
void Prog15_5()
{
MyVector v1;
MyVector v2;
cout << “**********************************Base Class************************” << endl;
//call the isempty() and return true if the vector is empty
if (v1.isEmpty())
cout << “nThe vector is empty” << endl;
cout << “nAdd the elements to base class ‘MyVector’v1” << endl;
//call the push_bacl()
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << endl;
}
cout << “nSize of the elements in base class ‘MyVector’v1:” << v1.size() << endl;
cout << “nRemove of the last element in base class ‘MyVector’v1:” << endl;
v1.pop_back();
cout << “nSize of the elements in base class ‘MyVector’ after removed:” << v1.size() << endl;
cout << “nAdd the elements to the base class’MyVector’v2” << endl;
v2.push_back(4);
v2.push_back(5);
v2.push_back(6);
v2.push_back(7);
for (int i =0; i < v2.size(); i++)
cout << v2.at(i) << endl;
cout << “nSize of the elements in base class ‘MyVector’v2:” << v2.size() << endl;
cout << “nSwap the contents between v1 and v2” << endl;
v1.swap(v2);
cout << “nElements in the base class ‘MyVector’v1 after swapped” << endl;
for (int i = 0; i < v1.size(); i++)
cout << v1.at(i) << endl;
cout << “nClear the elements in the base class ‘MyVector’v1” << endl;
v1.clear();
cout << “nSize of the base class ‘MyVector’v1: ” << v1.size() << endl << endl;
cout << “**********************************Derivied Class************************” << endl;
if (v1.isEmpty());
cout <<“nThe stack is empty”;
cout <<“nAdd the elements to derivied class ‘Stack’ ” << endl;
//call the push()
v1.push_back(9);
v1.push_back(8);
v1.push_back(7);
cout << “nSize of the elements in derivded class ‘Stack’ ” << v1.size() << endl;
cout << “nThe top of the element from the derived class ‘Stack’ ” << v1.peek() << endl;
cout << “nRemove the last element from the derived class ‘Stack’ ” << endl;
v1.pop_back();
cout << “n Size of the elements in derived class ‘Stack’ after removed” << v1.size() << endl;
cout << “nThe top of the element from the derived class ‘Stack’ after removed” << v1.peek() << endl;
}
int main()
{
while (true)
{
system(“cls”);
cout << “nMain Menu – Chapter 15” << endl;
cout << “==============================” << endl;
cout << ” 1: Programming Exercise 15.1″ << endl;
cout << ” 2: Programming Exercise 15.2″ << endl;
cout << ” 3: Programming Exercise 15.3″ << endl;
cout << ” 4: Programming Exercise 15.4″ << endl;
cout << ” 5: Programming Exercise 15.5″ << endl;
cout << “other: Exit” << endl;
cout << “==============================” << endl;
cout << “Enter an exercise: “;
char exercise[2];
cin >> exercise;
cout << endl;
switch (atoi(exercise))
{
case 1: Prog15_1(); break;
case 2: Prog15_2(); break;
case 3: Prog15_3(); break;
case 4: Prog15_4(); break;
case 5: Prog15_5(); break;
default: exit(0);
}
cout << endl;
system(“pause”);
cin.clear();
}
return 0;
}
Expert Answer
#include <vector>
#include “MyVector.h”
using namespace std;
template <class T>
MyVector::MyVector()
{
vectorSize = 0;
}
template <class T>
bool MyVector::isEmpty()
{
return data.empty();
}
template <class T>
T MyVector::at(int index)
{
return data.at(index);
}
template <class T>
void MyVector::push_back(T value)
{
data.push_back(value);
}
template <class T>
void MyVector::pop_back()
{
data.pop_back();
}
template <class T>
T MyVector::peek()
{
return data.at(data.size()-1);
}
template <class T>
unsigned int MyVector:: size()
{
return vectorSize;
}
template <class T>
int MyVector::clear()
{
vectorSize = 0;
data.clear();
return vectorSize;
}