include Student ,h finclude castreamo finclude Students Students (int perm, stud
ID: 3826182 • Letter: I
Question
include Student ,h finclude castreamo finclude Students Students (int perm, stud: :string lastName std: :string first AndMiddleNames) stub construct a Student object from a single line of comma separated text e.g. "1234567,Smith, Mary Kay Student Student (std: :string csvstring) std: :istringstream iss (csvstring): This version of getline takes an input string istream), a string, and a delimiter character (in this case, We read from the stream until the delimiter is encountered std: string permAsst ring getline (iss, permasstring, this- perm std: :stoi (permAsstring): stoi converts std: :string to int get line (iss, this- lastName, getline (iss this-> first In int Student getPerm() const return 999 stub std: string Students const return std: :string ("Stuberson std. string Student get Firs const return std::string ("Stub Toe std string Student get Full Name const return std: :string Stub Toe Stuberson std: string Students const e.g. 12345, Smith, Malory Logan std tostringstream oss; 088 "I" cc this->get Perm() this- get LastName() this- getFirstAndMiddleNames() return oss str();Explanation / Answer
//Below is the answer
// Added some comment in code where changes was not required
//assumed your header file looks similar as below
// added using namespace std to avoid writing it again and again in code.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
class Student
{
public:
int perm;
string lastname;
string firstAndMiddleNames;
Student();
Student(int perm, string lastname, string firstAndMiddleNames);
Student(string csvString);
int getPerm() const;
string getLastName() const;
string getFirstAndMiddleNames() const;
string GetFullName() const;
string toString() const;
};
//no change required
Student::Student(int perm, string lastname, string firstAndMiddleNames)
{
perm = perm;
lastname = lastname;
firstAndMiddleNames = firstAndMiddleNames;
}
//no change required
Student::Student(string csvString)
{
std::istringstream iss(csvString);
string permAsString;
getline(iss, permAsString, ',');
this->perm = std::stoi(permAsString);
getline(iss, this->lastname, ',');
getline(iss, this->firstAndMiddleNames, ' ');
}
int Student::getPerm() const
{
return perm;
}
string Student::getLastName()const
{
return lastname;
}
string Student::getFirstAndMiddleNames() const
{
return firstAndMiddleNames;
}
string Student::GetFullName() const
{
string fname = getFirstAndMiddleNames() + " ";
fname = fname + getLastName();
return fname;
}
//No change required
string Student::toString() const
{
ostringstream oss;
oss << "["
<< this->getPerm() << ","
<< this->getLastName() << ","
<< this->getFirstAndMiddleNames() << "]";
return oss.str();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.