【定义并实现一个学生类(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: Java中定义学生student的语法是:public class Student{ // 的属性声明 private String name; private int age; // 的构造器声明 public Student(String name, int age){ this.name = name; this.age = age; } // 的方法声明 public String getName(){ return name; } public int getAge(){ return age; } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } } ### 回答2: 学生Student是一种定义学生对象的模板。它可以包含以下属性和方法: 1. 属性: - 学生姓名(name):表示学生的姓名,数据型为String。 - 学生年龄(age):表示学生的年龄,数据型为int。 - 学生性别(gender):表示学生的性别,数据型为String。 - 学生学号(studentId):表示学生的学号,数据型为String。 2. 方法: - 构造方法(Constructor):用于创建Student对象,可以设置学生的姓名、年龄、性别和学号。 - 获取和设置方法(Getter和Setter):用于获取和设置学生的姓名、年龄、性别和学号。 - 学习方法(study):用于模拟学生学习的行为,输出学生的姓名和学习内容。 - 休息方法(rest):用于模拟学生休息的行为,输出学生的姓名和休息内容。 示例代码如下: ``` public class Student { // 属性 private String name; private int age; private String gender; private String studentId; // 构造方法 public Student(String name, int age, String gender, String studentId) { this.name = name; this.age = age; this.gender = gender; this.studentId = studentId; } // 获取和设置方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } // 学习方法 public void study() { System.out.println(name + "正在学习。"); } // 休息方法 public void rest() { System.out.println(name + "正在休息。"); } } ``` 通过定义学生Student,我们可以方便地创建和管理学生对象,且调用不同的方法来模拟学生的行为。可以根据具体需求进行扩展和使用。 ### 回答3: Java中定义一个学生Student可以包含以下属性和方法: 属性: 1. 学生姓名(name):用String型表示,表示学生的姓名。 2. 学号(id):用String型表示,表示学生的学号。 3. 年龄(age):用int型表示,表示学生的年龄。 4. 性别(gender):用String型表示,表示学生的性别。 方法: 1. 构造方法(Constructor):可以通过构造方法创建一个Student对象,给对象的属性赋初值。 2. 获取姓名(getName):返回学生的姓名。 3. 设置姓名(setName):设置学生的姓名。 4. 获取学号(getId):返回学生的学号。 5. 设置学号(setId):设置学生的学号。 6. 获取年龄(getAge):返回学生的年龄。 7. 设置年龄(setAge):设置学生的年龄。 8. 获取性别(getGender):返回学生的性别。 9. 设置性别(setGender):设置学生的性别。 10. 学习(study):表示学生正在学习。 示例代码如下: ```java public class Student { private String name; private String id; private int age; private String gender; public Student(String name, String id, int age, String gender) { this.name = name; this.id = id; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public void study() { System.out.println(name + "正在学习。"); } } ``` 以上是一个基本的学生定义。可以通过实例化该的对象,设置对象的属性值,调用对象的方法来实现相应的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wdwc2

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

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

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

打赏作者

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

抵扣说明:

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

余额充值