<C++文件读写>文本文件、静态成员、重载<<操作符格式化输出、容器类、类的头文件

C++面向对象程序设计课程的作业,初学者,很多地方做的不好,欢迎指正。

题目要求:

创建一个存有学生信息的文本型文件 student_info.txt,包含至少 10 名学生信息,该文件格式如下:

姓名 性别 学号 成绩

Alice F 11位数字学号 90

Bob M 11位数字学号 87

定义类 Student,声明非静态私有成员变量:姓名、性别、学号、成绩,声明静态成员变量:学生总人数和总成绩、静态成员函数 get_average,支持计算所有学生的平均成绩。该类需重载<<操作符,支持对 Student 类对象的格式化输出(例如:每个属性域宽 15, 各属性均采用左对齐等)Student 类的其他成员变量、成员函数可按需求自行设计添加。编写一个普通函数 student_info_parser(string file_name, vector<Student> &students),读取student_info.txt,填充存储学生类对象的容器 students。创建主函数文件 main.cpp,测试上述要求功能的正确性。

//此代码段为文件2Students.h

#ifndef _STUDENT_H
#define _STUDENT_H

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<vector>
#include<iomanip>
using namespace std;

class Student
{
    public:
        Student();
        Student(string name,char sex,int number,double score);
        static double get_average();//静态成员函数:计算所有学生的平均成绩
        friend void student_info_parser(string file_name, vector<Student> &students);
        friend ostream & operator<<(ostream &, const Student &);
    private:
        string name;
        char sex;
        string number;
        double score;
        static int totalCount;//静态成员变量:总人数
        static double totalScore;//静态成员变量:总成绩
};

#endif
//此代码段为文件2Students.cpp

#include"2Student.h"
using namespace std;

Student::Student(){}

Student::Student(string name,char sex,int number,double score)
    {this->name=name;this->sex=sex;this->number=number;this->score=score;}

double Student::get_average()
    {return totalScore/totalCount;}

//对静态成员变量进行初始化
int Student::totalCount=0;
double Student::totalScore=0;

//重载<<操作符,对Student类对象的格式化输出(格式:左对齐,姓名、性别、学号、成绩的域宽分别为10,6,15,8)
ostream & operator<<(ostream &os, const Student &stu)
{
    os<<setiosflags(ios::left)<<setw(10)<<stu.name<<' '<<setw(6)<<stu.sex<<' '
        <<setw(15)<<stu.number<<' '<<setw(8)<<stu.score;
    return os;
}

//读取student_info.txt,填充存储学生类对象的容器students
void student_info_parser(string file_name, vector<Student> &students)
{
    fstream fstrm;
    Student stu;
    fstrm.open("student_info.txt",ios::in);
    if(!fstrm) printf("Open Error\n");
    while(fstrm>>stu.name)
    {
        fstrm>>stu.sex;
        fstrm>>stu.number;
        fstrm>>stu.score;
        students.push_back(stu);//给容器类添加元素
        stu.totalCount+=1;//统计总人数
        stu.totalScore+=stu.score;//统计总分数
    }
    cout<<"the number of student is "<<stu.totalCount<<endl;
    fstrm.close();
}
//此代码段为主函数文件main.cpp

#include"2Student.h"
#include"2Student.cpp"
using namespace std;

int main()
{
    Student stu;
    vector<Student> students;
    student_info_parser("students.txt",students);
    //从容器类中格式化输出学生信息
    for(int i=0;i<students.size();i++)
    {cout<<students[i]<<endl;}
    //得到平均成绩
    cout<<"The average score is "<<stu.get_average()<<endl;
    return 0;
}

文本文件内容:

 运行结果:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 操作符重载通常也分为头文件和源文件两部分。 在头文件,我们通常只需要声明该操作符的原型,而在源文件则需要实现该操作符的具体功能。 以重载运算符 `+` 为例,操作符重载头文件和源文件的写法如下: 在头文件,声明该操作符的原型: ```c++ // MyClass.h #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass operator+ (const MyClass& other) const; private: int value; }; #endif ``` 在源文件,实现该操作符的具体功能: ```c++ // MyClass.cpp #include "MyClass.h" MyClass MyClass::operator+ (const MyClass& other) const { MyClass result; result.value = this->value + other.value; return result; } ``` 需要注意的是,如果该操作符重载的成员函,我们需要在函名前面加上 `operator` 关键字,并在函实现时使用 `::` 运算符指定该函属于哪个。 而如果该操作符重载的非成员函,则需要在函名前面加上 `friend` 关键字,并在函实现时不需要指定该函属于哪个。 例如,重载运算符 `<<` 时,我们通常会将其定义为一个非成员函,并声明为该的友元函,如下所示: ```c++ // MyClass.h #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> class MyClass { public: friend std::ostream& operator<< (std::ostream& os, const MyClass& obj); private: int value; }; #endif ``` ```c++ // MyClass.cpp #include "MyClass.h" std::ostream& operator<< (std::ostream& os, const MyClass& obj) { os << obj.value; return os; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值