【定义并实现一个学生类(Student)】

【问题描述】定义并实现一个学生类(Student),类中的数据成员有:学生姓名、学生学号、人数和班级号。至少有一个公有成员函数(Print)。Print负责将相关信息输出。在类中定义静态数据成员,记录当前学生人数。在类中定义常数据成员,记录班级号,假设比较号为1001。添加一个友元函数,可对学生类中的私有数据成员进行访问。假设1001班的初始学生人数为45人。在主程序中测试新加入2个学生,并分别通过成员函数和友元函数输出输出2个学生的信息及班级总人数。

   掌握静态数据成员、常数据成员和静态成员函数、常成员函数的基本使用方法。

   理解友元和友元函数的作用,掌握其使用方法。

【输入形式】新加入班级的学生姓名
【输出形式】加入学生的信息及班级总人数。

【样例输入】

    张三  王五

【样例输出】

    名字:张三

    学号:1

    班级:1001

    班级人数:47

    

    名字:王五

    学号:2

    班级:1001

班级人数:47

【样例说明】
【评分标准】

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<istream>
#include<iomanip>
#include<ostream>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<fstream>
#include<stack>
#include<ctime>
#include<deque>
#include<queue>
#include <sstream>
#include <numeric>
#pragma warning (disable:4996)

using namespace std;

// 学生类的定义
class Student {
public:
    Student(string name) : name(name), id(++student_count), class_no(1001) {
        total_students++;
    }

    void Print() {
        cout << "名字:" << name << endl;
        cout << "学号:" << id << endl;
        cout << "班级:" << class_no << endl;
        cout << "班级人数:" << total_students << endl;
    }

    

private:
    string name;
    int id;
    const int class_no;
    static int student_count;
    static int total_students;

    friend void Output(Student s);
};

// 学生类的实现
int Student::student_count = 0;
int Student::total_students = 45;

void Output(Student s) {
    cout << "名字:" << s.name << endl;
    cout << "学号:" << s.id << endl;
    cout << "班级:" << s.class_no << endl;
    cout << "班级人数:" << Student::total_students << endl;
}

int main() {
    string name1, name2;
    cin >> name1 >> name2;

    Student a(name1);
    Student b(name2);

    a.Print();
    cout << endl;

    Output(b); //通过友元函数输出学生b的信息
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 以下是一个简单的Python实现: ```python class Student: # 构造函数,初始化学生的姓名和学号 def __init__(self, name, id): self.name = name self.id = id # 打印学生的信息 def print_info(self): print("Name:", self.name) print("ID:", self.id) ``` 这个有两个属性,即学生的姓名和学号,以及一个方法,用于打印学生的信息。可以按照以下方式使用: ```python s = Student("张三", "20180101") s.print_info() ``` 输出结果为: ``` Name: 张三 ID: 20180101 ``` ### 回答2: 学生类是指用于描述学生的一对象,具有特定的属性和行为。 首先,学生类的属性可以包括学生的基本信息,比如学号、姓名、性别、年龄、班级等。这些属性可以作为学生对象的特征,用于区分不同的学生。 其次,学生类还可以有一些行为或方法。例如,学生可以参加课程,可以提交作业,可以参加考试等。这些行为可以是学生对象所特有的,通过方法来实现。 进一步地,学生类也可以包含一些其他的方法,例如输出学生的基本信息、计算学生的平均成绩等。这些方法可以用于对学生对象进行操作和处理。 另外,学生类还可以与其他进行关联,比如班级、课程等。通过与其他的关联,可以更好地描述学生的身份和角色。 总之,学生类是用于描述学生的一种抽象概念,通过属性和方法来描述学生的特征和行为,可以用于创建学生对象、操作学生对象以及与其他进行关联,从而实现对学生的管理和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武帝为此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值