0903作业+思维导图

一、作业

1》多态的实现

1、代码

#include <iostream>

using namespace std;
//父类
class Person
{
public:
    string name;
    int age;
public:
    Person(){}
    Person(string n,int a):name(n),age(a){}
    ~Person(){}

    //纯虚函数
    virtual void show() = 0;
};
//子类1
class Stu:public Person
{
private:
    int score;    //成绩
    int ranking;  //排名
public:
    Stu(){}
    Stu(int s,int r,string n,int a):Person(n,a),score(s),ranking(r){}
    ~Stu(){}

    void show()
    {
        cout<<name<<":年龄:"<<age<<",成绩"<<score<<",总排名"<<ranking<<endl;
    }
};
//子类2
class Teacher:public Person
{
private:
    int num;    //所带班级人数
    int wages;  //薪资
public:
    Teacher(){}
    Teacher(int num,int w,string n,int a):Person(n,a),num(num),wages(w){}
    ~Teacher(){}

    void show()
    {
        cout<<name<<":年龄:"<<age<<",薪资"<<wages<<",所带班级总人数"<<num<<endl;
    }
};
//全局函数
void fun(Person &s)
{
    s.show();
}
int main()
{
    Stu s1(90,7,"zhangsan",16);
    Teacher t1(41,5600,"xiaoming",35);
    fun(s1);
    fun(t1);
    return 0;
}

2、运行截图

2》自己封装栈和队列

1、栈的实现代码

#include <iostream>

using namespace std;
//栈类
class Stack
{
private:
    int *ptr; //数据指针
    int top;  //栈顶元素数
    int max;
public:
    Stack():top(-1),max(20){
        ptr = new int[max];   //申请空间
        cout<<"无参构造"<<endl;}
    ~Stack(){
        delete ptr;           //释放空间
        cout<<"析构函数"<<endl;}
    Stack(Stack &s):ptr(new int(*s.ptr)),top(s.top),max(s.max)
    {
        cout<<"拷贝构造函数"<<endl;
    }
    //写入初始栈内容
    void operator=(int a)
    {
        //判满
        if(a>max)
        {
            cout<<"error"<<endl;
            return;
        }
        //写入内容
        for(int i=0;i<a;i++)
        {
            cout<<"请输入第"<<i+1<<"位数为:";
            cin>>ptr[i];
            top++;
        }
    }
    //栈顶元素访问
    int stack_top()
    {
        return ptr[top];
    }
    //判空
    bool empty()
    {
        return top==-1;
    }
    //元素数
    int size()
    {
        return top+1;
    }
    //向栈顶插入元素
    void push(int n)
    {
        //判满
        if(top==max-1)
        {
            cout<<"无法继续插入"<<endl;
            return;
        }
        ++this->top;
        this->ptr[top] = n;
    }
    //删除栈顶元素
    void pop()
    {
        //判空
        if(empty())
        {
            return;
        }
        this->top--;
    }
    void show()
    {
        for(int i=0;i<top+1;i++)
        {
            cout<<ptr[i]<<"    ";
        }
        cout<<endl;
    }
};

int main()
{
    Stack s1;   //实例化一个自定义的栈类
    s1.operator=(5);//初始化
    cout<<s1.stack_top()<<endl;
    cout<<s1.size()<<endl;
    s1.push(7);    //入栈
    s1.show();
    s1.pop();      //出栈
    s1.show();
    cout<<"***************"<<endl;
    Stack s2=s1;
    s2.show();
    return 0;
}

2、栈的运行结果

3、队列的实现代码

#include <iostream>

using namespace std;
//队列类
class Queue
{
private:
    int size;      //队列最大容量
    int num;      //队列内元素数
    int *ptr;     //容器
public:
    Queue():size(20),num(0){
        ptr = new int[this->size];
        cout<<"无参构造"<<endl;
    }
    ~Queue(){
        delete ptr;           //释放空间
        cout<<"析构函数"<<endl;
    }
   Queue(Queue &q):size(q.size),num(q.num),ptr(new int(*q.ptr))    //深拷贝
   {
       cout<<"拷贝构造函数"<<endl;
   }
   int front()
   {
       if(empty())
       {
           cout<<"队列为空无法返回"<<endl;
           return -1;
       }
       return ptr[0];
   }
   int back()
   {
       if(empty())
       {
           cout<<"队列为空无法返回"<<endl;
           return -1;
       }
       return ptr[num-1];
   }
   bool empty()
   {
       return num==0;
   }
   int queue_size()
   {
       return num;
   }
   void push(int n)
   {
       //判满
       if(num>=size)
       {
           cout<<"容器已满无法继续加入"<<endl;
           return;
       }
       this->num++;
       this->ptr[num-1] = n;
       cout<<"添加完毕"<<endl;
   }
   void pop()
   {
       //判空
       if(empty())
       {
           cout<<"容器为空无法删除"<<endl;
           return;
       }
       for(int i=0;i<this->num;i++)
       {
           this->ptr[i]=this->ptr[i+1];
       }
       this->num--;
       cout<<"删除完毕"<<endl;
   }
   void show()
   {
       for(int i=0;i<num;i++)
       {
           cout<<ptr[i]<<"  ";
       }
       cout<<endl;
   }
};

int main()
{
    Queue q1;
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.show();
    q1.pop();
    q1.show();
    cout<<q1.front()<<"  "<<q1.back()<<endl;
    Queue q2=q1;
    q2.show();
    return 0;
}

4、队列的实现结果

二、思维导图(继承,多态)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值