学生教务系统(录入并且输出学生信息表)

使用C++创建一个程序,实现录入学生的基本信息以及输出信息表

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

class Student
{
private:
    string name;
    int ID;
    double score;

public:
    Student() {};
    Student(string name1, int ID1, double score1)
    {
        name = name1;
        ID = ID1;
        score = score1;
    }
    //内联函数(用于输出)
    inline void display()
    {
        cout << "姓名:" << name << " 学号:" << ID << " 成绩:" << score << endl;
    }
    ~Student() {}
};


int main() {
    int n;
    cout << "请输入学生总数:";
    cin >> n;
    Student* s = new Student[n];
    //输入并且存储学生信息表
    for (int i = 0; i < n; i++)
    {
        string name;
        int ID;
        double score;
        cout << "请输入第 " << i + 1 << " 个学生的姓名:" << endl;
        cin >> name;
        cout << "请输入第 " << i + 1 << " 个学生的学号:" << endl;
        cin >> ID;
        cout << "请输入第 " << i + 1 << " 个学生的成绩:" << endl;
        cin >> score;
        s[i] = Student(name, ID, score);
    }
    cout << "《学生信息表》" << endl;
    for (int i = 0; i < n; i++)
    {
        s[i].display();
    }
    delete[]s;
    return 0;
}

class Student 

定义了一个名为Student的类;

私有部分

private:
    string name;
    int ID;
    double score;

定义了三个成员变量:name (姓名)、ID(学号)、score(成绩);目的在于:

1.数据保护:私有成员变量只能在类内访问,可以防止其他代码直接修改这些变量。

2.封装性:使得类的使用者只能通过公共接口(如成员函数)来操作数据。

3.控制访问权限:可以更精准地控制对类内部数据的访问。

4.安全性、灵活性、可维护性。 

公有部分

默认构造函数
public:
    Student() {};

带参数的构造函数

用于初始化类的成员变量

Student(string name1, int ID1, double score1)
    {
        name = name1;
        ID = ID1;
        score = score1;
    }
内联函数(用于输出)
 
inline void display()
    {
        cout << "姓名:" << name << " 学号:" << ID << " 成绩:" << score << endl;
    }
 
析构函数
    ~Student() {}

主函数
   

关于动态内存:

 Student* s = new Student[n];
 delete[]s;


动态分配的内存。
 


遇到的问题

解决了就行了总结那么认真搞啥子,切~

创建数组类必须得有默认构造函数;

new和delete的基本格式得用;

多用VS,codeblocks这个破编译软件阴人

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值