c++ - inheritance - use parent's read method in child -
can use parent's read() function read first , last names, , have child's read() read middle name? or have read first, second, , middle in child?
edit:the first answer seems working when read in child using strtok, whole line, not third field. there way around it, or have read first 2 fields dummy variables , read third field?
class parent { protected: char first[80], last[80]; virtual istream& read(istream &is) { char temp[80]; char *f, *l; >> temp; f = strtok(temp, ","); strcpy(first, f); l = strtok(null, ","); strcpy(last, l); return is; } public: friend istream& operator>> (istream &is, parent &parent) { return parent.read(is); } }; class child: public parent { char middle[80]; istream& read(istream &is) { /*inherit first , last parent*/ char temp[80]; char *m; >> temp; m = strtok(temp, ","); strcpy(middle, m); } }; in main() parent *object; ifstream inf("filename.csv"); object = new child(); inf >> *object; cat filename.csv
george,bush,walker
in read function in child class, can call parents read function this:
class child : public parent { // ... std::istream &read(std::istream &is) { parent::read(is); // read more } };
Comments
Post a Comment