| :: [intv] Use of function templates in C++ :: | ||||
| HOME |
|
[Date Prev][Date Next][Date Index] [intv] Use of function templates in C++#---------------Makefile --------- CC=g++ COPTS = -g DEPENDS= stack.o CFLAGS= $(COPTS) $(DEFINES) all: stack main stack: stack.cpp stack.h $(CC) $(CFLAGS) -c $@.cpp $(LIBS) main: stack $(CC) $(CFLAGS) -o $@ $@.cpp $(DEPENDS) clean: rm *.o *~ main #---------------Makefile --------- //------------stack.h----------- #ifndef STACK_H #define STACK_H #include <iostream> const int MAXSTACKSIZE = 40;
template< class DataType >
class stack{
private:
DataType stacklist[MAXSTACKSIZE];
int top;
public:
stack(void);
void push(DataType Item);
DataType pop(void);
DataType peek(void);
int isEmpty();
int isFull();
};#include "stack.cpp" #endif //------------stack.h----------- //------------stack.cpp-------------- #ifndef STACK_CPP #define STACK_CPP #include <iostream>
#include "stack.h"
using namespace std;
template < class DataType >
stack< DataType >::stack(){
top = -1;
}template < class DataType >
void stack< DataType >::push(DataType Item){
if (!isFull()){
top++;
stacklist[top] = Item;
}else{
cout << "Stack Overflow" << endl;
}
}template < class DataType >
DataType stack< DataType >::pop(void){
if (!isEmpty()){
DataType tem;
tem = stacklist[top];
top--;
return tem;
}else{
cout << "Stack Underflow" << endl;
}
}template < class DataType >
DataType stack< DataType >::peek(void){
if (!isEmpty()){
return stacklist[top];
}else{
cout << "Attempt to peek into an empty stack" << endl;
}
}template < class DataType >
int stack< DataType >::isEmpty(void){
if (top == -1) return 1; else return 0;
}template < class DataType >
int stack< DataType >::isFull(void){
if (top == (MAXSTACKSIZE-1)) return 1; else return 0;
}#endif //------------stack.cpp-------------- //---------------main.cpp----------- //Problem: Different ways in which arguments can be passed to a function. //Save as: #include <iostream> #include "stack.h" using namespace std;
x = (char *)malloc(sizeof(char)*50); strcpy(x, "/home/shashank/../WWW/../index.html"); cout << x << endl; char *y; y = (char *) malloc(sizeof(char)*10); char *z; z = (char *) malloc(sizeof(char)*100); y = strtok(x, "/");
while(y != NULL){
if (strcmp(y, "..") == 0){
if (!S.isEmpty()) S.pop();
}else{
S.push(y);
}
y = strtok(NULL, "/");} while (!S.isEmpty()){
T.push(S.pop());
} z = strcpy(z, "/");
while (!T.isEmpty()){
strcat(z, T.pop());
strcat(z, "/");
}cout << z << endl; } //---------------main.cpp----------- |