2022.11.10work

定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法 (无参构造、有参构造、析构函数、show函数)

再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法 (无参构造、有参构造、析构函数、show函数)。

由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参 构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后 调用其show函数进行测试

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:
    string name;
    int age;
    float score;
public:
    Student(){}
    Student(string s, int a, float f):name(s),age(a),score(f){}
    ~Student(){}

    void show()
    {
        cout<<"姓名:"<<name<<endl;
        cout<<"年龄:"<<age<<endl;
        cout<<"分数:"<<setw(1)<<score<<endl;
    }
};

class Part
{
private:
    string active;
    string organized;
public:
    Part(){}
    Part(string a, string o):active(a),organized(o){}
    ~Part(){}

    void show()
    {
        cout<<"党组织活动:"<<active<<endl;
        cout<<"组织:"<<organized<<endl;
    }
};

class Cadres:public Student,public Part
{
private:
    string post;
public:
    Cadres(){}
    Cadres(string s, int a, float f,string act, string o,string p):Student(s,a,f),Part(act,o),post(p){}

    void show()
    {
        Student::show();
        Part::show();
        cout<<"职位:"<<post<<endl;
    }
};


int main()
{
    Cadres c1("张三",24,114.5,"专题报告","支部委员会","宣传委员");
    c1.show();
    return 0;
}

用类封装队列

#include <iostream>
#include <string.h>
using namespace std;

#define MAX 8
typedef int datatype;

class Queue
{
private:
    datatype data[MAX];
    int front;
    int tail;
public:
    Queue(){
        front=0;
        tail=0;
        memset(data,0,sizeof(datatype)*MAX);
    }

    bool empty()
    {
        return front==tail?true:false;
    }
    bool full()
    {
        return (front+1)%MAX==tail?true:false;
    }
    int size()
    {
        return (MAX+tail-front)%MAX;
    }
    void push(datatype e)
    {
        if(full()){
            cout<<"入队失败,队列已满"<<endl;
            return ;
        }
        data[front]=e;
        front=(front+1)%MAX;
    }
    datatype pop()
    {
        if(empty()){
            cout<<"出队失败,队列已空"<<endl;
            return 0;
        }
        datatype t=data[tail];
        tail=(tail+1)%MAX;
        return t;
    }
    void show()
    {
        for(int i=tail;i!=front;i=(i+1)%MAX)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }
};


int main()
{
    Queue q1;
    cout<<q1.empty()<<endl;
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.push(4);
    q1.push(5);
    q1.push(6);
    q1.push(7);
    q1.push(8);
    q1.full();
    q1.show();
    cout<<q1.pop()<<q1.pop()<<q1.pop()<<q1.pop()<<q1.pop()<<q1.pop()<<q1.pop()<<endl;
    cout<<q1.pop();
    return 0;
}

 

 

用类封装堆栈

#include <iostream>

using namespace std;

class Stack
{
private:
    union
    {
        int data;
        int len;
    };
    Stack *next;
public:
    Stack(){
        len=0;
        next=NULL;
    }


    bool empty()   // 判空
    {
        return this->next?false:true;
    }

    int push(int e)     // 入栈
    {
        Stack *cp=new Stack;
        cp->data=e;
        cp->next=this->next;
        this->next=cp;
        this->len++;
        return 0;
    }

    int pop()      // 出栈
    {
        if(NULL==this->next){
            cout<<"出栈失败,空栈"<<endl;
            return -1;
        }
        Stack *cp=this->next;
        this->next=this->next->next;
        this->len--;
        int t=cp->data;
        delete cp;
        return t;
    }

    int show()     // 遍历
    {
        if(NULL==this->next){
            cout<<"遍历失败,空栈"<<endl;
            return -1;
        }

        Stack *cp=this->next;
        while(cp){
            cout<<cp->data<<" ";
            cp=cp->next;
        }
        cout<<endl;

        return 0;
    }

    int free()      // 销毁堆栈
    {               // 写在析构函数内会影响出栈
        while(this->next){
            this->pop();
        }
        return 0;
    }
};

int main()
{
    Stack *s1=new Stack();
    s1->empty();
    s1->push(4);
    s1->push(1);
    s1->push(7);
    s1->push(8);
    s1->push(2);
    s1->push(0);
    s1->show();
    cout<<s1->pop()<<s1->pop()<<s1->pop()<<s1->pop()<<s1->pop()<<s1->pop()<<s1->pop()<<endl;
    s1->free();
    delete s1;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值