【求助】【急】一个C++问题,关于类以及类的继承
现在有三个类,Person,Student,Employee,其中Person类有成员name(string),birthyear(int),Student类继承于Person并且有level(string),GPA(double),major(string),Employee类也继承于Person并且有salaryamount(string),employmentminute (double),现在从txt文件中读取信息(其中txt文件里的格式是student#Jose Smith#1994#junior#3.0#MA
employee#Mary Hutter#1982#35,000#31 student表示该行是学生信息,employee表示该行是职工信息)并且将txt文件的信息按照要求保存到对应的student类或者是employee类里,然后对这些数据进行增删改查等处理。现在有帮助函数如下:
#include <stdexcept>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "helpers.h"
using namespace std;
// Split a string on the # character. Returns a vector of the parts, in order.
vector<string> splitString(string input)
{
vector<string> pieces;
// Start of the current field
int field_start = 0;
// Location of the next at sign.
int next_at;
do {
// Find the #. Returns the index, or string::npos if it wasn't
// found.
next_at = input.find('#', field_start);
// If it wasn't found, add the last piece.
if (next_at == string::npos)
pieces.push_back(input.substr(field_start));
// Or it was found, so add this piece
else
pieces.push_back(input.substr(field_start, next_at - field_start));
// The next field starts just after the #
field_start = next_at + 1;
// If there was no #, we're done.
} while(next_at != string::npos);
return pieces;
}
// Convert a numeric string to an int. Throws an exception if it is
// not a number.
int stringToInt(string str)
{
int num;
istringstream instr(str);
instr >> num;
if (instr.fail())
throw runtime_error("invalid number " + str);
// Check for junk after the number.
string remaining = "junk";
getline(instr, remaining);
// End of file or an empty string means there was no junk.
if (instr.eof() || remaining == "")
return num;
else
throw runtime_error("garbage after number " + str);
}
// Convert a numeric string to a double. Throws an exception if it is
// not a number.
double stringToDouble(string str)
{
double num;
istringstream instr(str);
instr >> num;
if (instr.fail())
throw runtime_error("invalid number " + str);
// Check for junk after the number.
string remaining = "junk";
getline(instr, remaining);
// End of file or an empty string means there was no junk.
if (instr.eof() || remaining == "")
return num;
else
throw runtime_error("garbage after number " + str);
}
// Helper function to parse a line from the person list file.
//
// TODO: adjust this to match your classes, and add support for the
// other type of person.
//
// NOTE: You should cut and paste this function into your PersonList
// class source file and declare the function prototype in the header
// file.
/*
Person *parsePerson(string line)
{
vector<string> columns = splitString(line);
if (columns.size() < 3)
throw runtime_error("not enough columns in: " + line);
// Columns 1-3: type, name, and year
string type = columns[0];
string name = columns[1];
int year = stringToInt(columns[2]);
if (year < 1900 || year > 2014)
throw runtime_error("year out of range");
if (type == "student") {
if (columns.size() < 6)
throw runtime_error("not enough columns in: " + line);
string level = columns[3];
double gpa = stringToDouble(columns[4]);
string major = columns[5];
if (gpa < 0.0 || gpa > 4.0)
throw runtime_error("GPA out of range");
// Adjust this to match your class name and parameters.
return new Student(name, year, level, gpa, major);
}
// TODO: add case for "employee"
if (type == "employee")
{
if (columns.size() < 5)
throw runtime_error("not enough columns in: " + line);
string salaryamount = columns[3];
double employmentminute = stringToDouble(columns[4]);
if (employmentminute<0)
throw runtime_error("employmentminute out of range");
// Adjust this to match your class name and parameters.
return new Employee(name, year, salaryamount, employmentminute);
}
// If we got here, we didn't handle the person type.
throw runtime_error("unknown person type " + type);
}
*/
现在怎么通过Person *parsePerson(string line)这个函数来将txt里的信息保存到相对应的类里?