#include <iostream>
using namespace std;
template <typename T>
class Stack {
private:
T* mData;
int mMaxSize, mTop;
public :
Stack();
Stack(int _size);
Stack(const Stack& _data);
~Stack();
void push(T _data);
T pop();
bool isOperator(char _rhs);
char* changeToPostfix();
bool isFull();
bool isEmpty();
};
int main(){
cout << "Hello world!" << endl;
return 0;
}
template <typename T>
Stack<T>::Stack()
: : mData(nullptr)
, , mMaxSize(0)
, , mTop(-1) {}
template <typename T>
Stack<T>::Stack(int _size)
: : mMaxSize(_size)
, , mTop(-1) {
mData = new int[_size];
}
template <typename T>
Stack<T>::Stack(const Stack& _data) {
}
template <typename T>
Stack<T>::~Stack() {
if (mData != nullptr) delete[] mData;
}
template <typename T>
void Stack<T>::push(T _data) {
}
template <typename T>
bool Stack<T>::isOperator(char _rhs) {
return (_rhs == '*' || _rhs == '/' ||
_rhs == '+' || _rhs == '-' ||
_rhs == '(' || _rhs == ')');
}
template <typename T>
char* Stack<T>::changeToPostfix() {
char* arithExp, postfixExp;
cin >> arithExp;
Stack<char> tempStack(strlen(arithExp));
for (int i = 0; i < strlen(arithExp); i++) {
}
return postfixExp;
}
template <typename T>
bool Stack<T>::isFull() {
return (mTop == mMaxSize - 1);
}
template <typename T>
bool Stack<T>::isEmpty() {
return (mTop == -1);
}
You are about to create a class stack with a class template When defining a Stack class, if you define it with a template, when you define all member functions as shown above, Do I have to write a template?
class template
Yes, you have to write it down.
Because when you define a function, you have to tell us what the letter "T" means.
template <typename T> // If T is the type name of the template like this. If you don't tell me,
bool Stack<T>::isEmpty() { // How does the compiler know what <T> stands for here?
return (mTop == -1);
}
If you define the member function of the class template internally, you don't have to tell me.
template <typename T> // This declaration is the scope below {} Applies to the whole.
class Stack {
private:
T* mData;
int mMaxSize, mTop;
public :
It's okay because we already know what T is here.
return (mTop == -1);
}
}; // That's all it takes.
// So when you implement a function definition outside,
// You need to write template <typename T> again.
© 2024 OneMinuteCode. All rights reserved.