继承和多态

Queue.c

#include <iostream>
 
using namespace std;
 
class Queue
{
private:
    int len;
    int *data; //容器
    int size=15; //最大容量
 
 
public:
    Queue();
    ~Queue();
    Queue &operator=(const Queue &other);
 
    void push(int value);
    void pop();
 
    int get_size();
 
    bool empty();
 
    int front();
 
    int back();
 
    void show();
 
    void junzi();
 
};
Queue::Queue()
{
    this->len = 0;
 
    data = new int [this->size];
    int i = 0;
    while (i<this->size) {
        this->data[i]=0;
        i++;
    }
 
}
Queue::~Queue()
{
    delete []data;
}
Queue &Queue ::operator=(const Queue &other)
{
    int i =0;
    if(this!=&other)
    {
        this->len=other.len;
        this->size = other.size;
 
        while( i<other.len){
            data[i] = other.data[i];
            i++;
        }
        data[other.len]='\0';
 
    }
    return *this;
 
}
 
void Queue::push(int value)
{
    if(this->len>=this->size)
    {
 
        junzi();
    }
    data[this->len++] = value;
}
void Queue::pop()
{
    int i = 0;
    while(i<this->len)
    {
        data[i] = data[i+1];
        i++;
    }
    this->len--;
}
int Queue::get_size()
{
    return this->size;
}
 
 
bool Queue::empty()
{
    return this->data==NULL||this->len==0;
}
int Queue::front()
{
    if(empty())
    {
        return 0;
    }
    else{
        return data[0];
    }
}
 
int Queue::back()
{
    if(empty())
    {
        return 0;
    }
    else{
        return data[this->len-1];
    }
 
}
void Queue::show()
{
    int i = 0;
    while(i<this->len)
    {
        cout<<data[i]<<" ";
        i++;
    }
    cout<<endl;
}
void Queue::junzi()
{
    int *temp = new int[this->size*2];
    int i=0;
    while(i<this->len)
    {
        temp[i] = data[i];
        i++;
    }
    delete []data;
    this->data= temp;//是this->data改变
    this->size *=2;
 
 
 
}
int main()
{
    Queue q;
    q.push(10);
    q.push(12);
    q.push(13);
    cout<<"q.front = "<<q.front()<<endl;
    q.pop();
    cout<<"q.back = "<<q.back()<<endl;
    q.show();
    cout<<"q.size = "<<q.get_size()<<endl;
    Queue q1;
    q1=q;
    q1.show();
    return 0;
}
 

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值