c++ - 将对象推入矢量以手动操作面向对象的程序
问题描述
我有一门课程有一个项目,但我被困住了,迫切需要帮助。我正在为学生招生创建一个面向对象的程序。
- 有他的基班学生,有两个孩子班,国内学生和国际学生。
- 托福成绩是另一门课。
- 该程序为学生的私有成员变量读取一个 .txt 文件 - 字符串 firstname、string lastname、float cgpa 和 int researchscore。
- 子类还有一个私有成员变量 - 国内的字符串省和国际的字符串国家。
我必须基本上操纵变量,然后在没有库的情况下以特定方式手动排列。我一直在尝试将成员变量添加到向量中。
我一直在尝试在类中定义向量并使用 get 和 set 函数将对象推送到它似乎不起作用,我也尝试过朋友。如果有人很好地指出我正确的方向,我将不胜感激。我正在使用旧版本的 C++
setpopulate(student& s1, vector<student> temp) //friend
{
temp.push_back(s1.FirstName)
}
string line;
student s1;
ifstream domesticFile("domestic-stu.txt");
if(!domesticFile.is_open()) {
cout << "Unable to open file domestic-stu.txt" << endl;
return -1;
}
getline(domesticFile, line);
cout << "File format: " << line << endl;
int stu_count = 1;
while( getline(domesticFile, line) ) {
istringstream ss(line);
string firstName, lastName, province, s_cgpa, s_researchScore;
float cgpa;
int researchScore;
getline(ss, firstName, ',');
getline(ss, lastName, ',');
getline(ss, province, ',');
getline(ss, s_cgpa, ',');
cgpa = atof(s_cgpa.c_str());
getline(ss, s_researchScore, ',');
researchScore = atoi(s_researchScore.c_str());
s1.setFirstName(firstName);
s1.setLastName(lastName);
s2.setProvince(province);
s1.setcgpa(cgpa);
s1.setresearchscore(researchScore);
//print the student info to the screen
cout << "Domestic student " << stu_count << " " << firstName << " "
<< lastName << " from " << province << " province has cgpa of "
<< cgpa << ", and research score of " << researchScore << endl;
stu_count++;
}
domesticFile.close();
#ifndef STUDENT_HPP_
#define STUDENT_HPP_
using namespace std;
#include<string>
#include<cstdlib>
#include<vector>
#include<iomanip>
class student
{
public:
student();
student(string newFirstName,string newLastName,float newcgpa,int newresearchscore,vector<student> x);
/*student(const student& copy)
{
string newFirstName,string newLastName,float newcgpa,int newresearchscore)
};*/
string getFirstName() const;
void setFirstName(string newFirstName) ;
string getLastName()const ;
void setLastName(string newLastName);
float getcgpa() const;
void setcgpa(float newcgpa);
int getresearchscore() const;
void setresearchscore(int newresearchscore);
friend void setpopulate(student& s1, string vector<student> temp);
void setvector(string vector<student> x);
string getvector() const;
friend std::ostream& operator<<(std::ostream& output, const student& obj);
friend string compareFirstName(const student& s1, const student& s2);
friend string compareLastName(const student& s1, const student& s2);
friend float comparecgpa(const student& s1, const student& s2);
friend int compareresearchscore(const student& s1, const student& s2);
friend bool operator ==(const student& s1, const student& s2);
friend bool operator >(const student& s1, const student& s2);
friend bool operator <(const student& s1, const student& s2);
private:
string FirstName;
string LastName;
float cgpa;
int researchscore;
vector<student> temp;
};
#endif
解决方案
推荐阅读
- office-js - 如何在office js中添加多个选定的单元格注释?
- window-functions - 分区之间的 clickhouse 移动平均线
- r - 如何在R中的列中打乱值?
- go - Dependabot 用于更新跟踪提交的 go 依赖项
- php - pdo 执行语句中的方括号有什么作用?
- windows - 从调用的批处理文件中接收变量
- python - 如何重新定位极坐标图坐标以匹配地理坐标 - python
- c++ - 您可以仅在某些单词/段落/行上添加颜色吗?
- flutter - setstate 不更新 ui 颤动
- javascript - 如何找出续集快递应用程序超时的原因(POST 状态 504)