Problem C: 选举班干部了!

Problem C: 选举班干部了!

Description

新学期伊始,某班要通过竞争上岗方式竞选班干部。包括班长、团支书等。现在需要你来设计2个类Student和StudentCadre来模拟这一过程。

1.Student类是学生类,包括

(1)属性:string name——姓名。bool sex——性别。int grade——年级。Student *next——指向下一个学生指针。

(2)方法:

  • 构造函数Student(string _name, bool _sex, int _grade)——初始化一个对象。注意:其中的next初始化为NULL。
  • 析构函数。
  • void showInfo()——输出一个学生的信息。next除外。
  • Student* getNext()——获得next指针。
  • void setNext(Student*)——设置next指针指向对象。

2. StudentCadre类是Student类的派生类,包括:

(1)属性:string position——表示竞聘岗位。

(2)方法:

  • StudentCadre(string _name, bool _sex, int _grade, string pos)——构造函数,初始化各种属性。
  • 析构函数。
  • void showInfo()——显示信息。

构造函数和析构函数都有输出,具体见样例。

Input

输入有多行。

第1行有4部分:姓名、性别(0或1)、年级(整数)和岗位。

第2行是一个正整数N,表示有N个学生支持该人竞聘相应岗位。之后有N行,每行有3个部分,分别是一个学生的姓名、性别和年级。

Output

见样例。

HINT

string类是C++提供的类,在头文件string中,可以用#include <string>来使用这个类。该类的对象可以直接利用赋值运算符(=)进行复制,也可以直接利用cout输出,或利用cin输入。



#include <iostream>
#include <string>

using namespace std;

class Student {
protected:
    string name;
    bool sex;
    int grade;
    Student *next;
public:
    Student(string _name, bool _sex, int _grade) : name(_name), sex(_sex), grade(_grade) {
        next = NULL;
        cout << "A student named by "<<name<<" is created!" << endl;
    }
    ~Student() {
        //delete next;
        cout << "A student named by "<<name<<" is erased." << endl;
    }
    void showInfo() {
        cout << "name = "<<name<<", sex = "<<sex<<", grade = "<<grade<<";";
    }
    void setNext(Student *p) {
        next = p;
    }
    Student *getNext() { return next; }
};

class StudentCadre : public Student {
private:
    string position;
public:
    StudentCadre(string _name, bool _sex, int _grade, string pos) : Student(_name, _sex, _grade), position(pos) {
        cout << "A student cadre with position "<<position<<" is created." << endl;
    }
    ~StudentCadre() {}
    void showInfo() {
        cout << "name = "<<name<<", sex = "<<sex<<", grade = "<<grade<<"; position = "<<position<<".";
    }
};
int main()
{
    int num;
    string name, position;
    bool sex;
    int grade;
    Student *header, *student, *curStudent;

    cin>>name>>sex>>grade>>position;
    header = new StudentCadre(name, sex, grade,position);
    curStudent = header;
    cin>>num;
    for (int i = 0; i < num; i++)
    {
        cin>>name>>sex>>grade;
        student = new Student(name, sex, grade);
        curStudent -> setNext(student);
        curStudent = curStudent -> getNext();
    }
    ((StudentCadre*)header) -> showInfo();
    cout<<endl;
    curStudent = header;
    while (curStudent -> getNext() != NULL)
    {
        curStudent = curStudent -> getNext();
        curStudent->showInfo();
        cout<<endl;
    }

    curStudent = header;
    while (curStudent != NULL)
    {
        student = curStudent;
        curStudent = curStudent -> getNext();
        delete student;
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优秀班委选举系统 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 优秀班委选举 { public partial class Form1 : Form { private ListBox listBox; public Form1() { InitializeComponent(); } public Form1(ListBox listBox) { InitializeComponent(); this.listBox = listBox; } private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { if (listBox1.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取左边listBox1中选中的内容 string cont = listBox1.SelectedItem.ToString(); //左边listBox1中删除选中的内容 listBox1.Items.Remove(cont); //把获得的内容加入到右边的listBox2中 listBox2.Items.Add(cont); } } } private void button2_Click(object sender, EventArgs e) { if (listBox2.Items.Count > 0) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取右边listBox2中选中的内容 string cont = listBox2.SelectedItem.ToString(); //右边listBox2中删除选中的内容 listBox2.Items.Remove(cont); //把获得的内容加入到左边的listBox1中 listBox1.Items.Add(cont); } } } //退出 private void button5_Click(object sender, EventArgs e) { Application.Exit(); } //显示结果 private void button4_Click(object sender, EventArgs e) { //传递listBox2到Form2窗口中 Form2 form2 = new Form2(listBox2); form2.Show(); } //修改名单 private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { string cont = listBox2.SelectedItem.ToString(); int index = listBox2.SelectedIndex; Form3 form3 = new Form3(cont, index, listBox2); form3.Show(); this.Hide(); } } //加载窗口 private void Form1_Load(object sender, EventArgs e) { if (listBox != null) { foreach (string str in listBox.Items) { listBox2.Items.Add(str); } } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值