Question: 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
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
//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 << " The vector is empty" << endl;
cout << " Add 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 << " Size of the elements in base class 'MyVector'v1:" << v1.size() << endl; cout << " Remove of the last element in base class 'MyVector'v1:" << endl;
v1.pop_back(); cout << " Size of the elements in base class 'MyVector' after removed:" << v1.size() << endl; cout << " Add 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 << " Size of the elements in base class 'MyVector'v2:" << v2.size() << endl;
cout << " Swap the contents between v1 and v2" << endl; v1.swap(v2); cout << " Elements in the base class 'MyVector'v1 after swapped" << endl;
for (int i = 0; i < v1.size(); i++) cout << v1.at(i) << endl;
cout << " Clear the elements in the base class 'MyVector'v1" << endl; v1.clear(); cout << " Size of the base class 'MyVector'v1: " << v1.size() << endl << endl;
cout << "**********************************Derivied Class************************" << endl;
if (v1.isEmpty()); cout <<" The stack is empty";
cout <<" Add the elements to derivied class 'Stack' " << endl;
//call the push() v1.push_back(9); v1.push_back(8); v1.push_back(7);
cout << " Size of the elements in derivded class 'Stack' " << v1.size() << endl; cout << " The top of the element from the derived class 'Stack' " << v1.peek() << endl; cout << " Remove the last element from the derived class 'Stack' " << endl;
v1.pop_back(); cout << " Size of the elements in derived class 'Stack' after removed" << v1.size() << endl; cout << " The top of the element from the derived class 'Stack' after removed" << v1.peek() << endl; }
int main() { while (true) { system("cls"); cout << " Main 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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
