本文主要是介绍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、队列的实现结果
二、思维导图(继承,多态)
这篇关于0903作业+思维导图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!