day06 1.继承和多态
#include "work.h"
Stack:: Stack():size(10)
{
data = new char[size];
top = -1;
cout <<"无参构造"<<endl;
}
Stack:: Stack(const char* s)
{
size = strlen(s);
data = new char[size];
strcpy(data,s);
top = size-1;
cout <<"有参构造"<<endl;
}
Stack:: ~Stack()
{
delete[] data;
cout <<"析构函数" <<endl;
}
Stack &Stack::operator=(const Stack &other)
{
if(this != &other)
{
delete[] data;
size = other.size;
top = size-1;
data = new char(size+1);
strcpy(data,other.data);
}
return *this;
};
const char &Stack::top_t()const
{
return data[size-1];
}
bool Stack::empty()
{
return top == -1;
}
int Stack::size_t()
{
return top+1;
}
void Stack::push_t(char a)
{
data[size] = a;
size = size + 1;
top++;
data[size] = '\0';
return ;
}
void Stack::pop_t()
{
size--;
top--;
data[size] = '\0';
}
Queue:: Queue()
{
data = new char[MAX];
front = 0;
tail = 0;
cout <<"无参构造"<<endl;
}
Queue:: Queue(const char* s)
{
size = strlen(s);
data = new char[MAX];
tail = size;
front = 0;
strcpy(data,s);
cout <<"有参构造"<<endl;
}
Queue:: ~Queue()
{
delete[] data;
cout <<"析构函数" <<endl;
}
Queue &Queue::operator=(const Queue &other)
{
if(this != &other)
{
delete[] data;
size = other.size;
data = new char(size+1);
front = 0;
tail = size;
strcpy(data,other.data);
}
return *this;
};
const char &Queue::front_at()const //访问队列第一个元素
{
return data[front];
}
const char &Queue::back_at()const //访问队列最后一个元素
{
return data[tail-1];
}
bool Queue::empty()
{
return front == tail;
}
int Queue::size_t()
{
return (tail-front)%MAX;
}
void Queue::push_t(char a)
{
//入队逻辑
data[tail] = a;
//队列变化:队尾后移
tail = (tail+1)%MAX;
size++;
}
void Queue::pop_t()
{
front = (front+1)%MAX;
size--;
}