Reputation:
How would I make this work? The professor instructed us to use private variables. Do I have to set up new variables to input Person's private members in the Student constructor? What is the most efficient way of making this work?
Thank you in advance.
class Person
{
friend istream& operator>>(istream&, Person&);
friend ostream& operator<<(ostream&, Person&);
private:
char* m_name;
int m_age;
string m_ssn;
public:
Person() = default;
Person(char*, int, const string&);
Person(const Person&);
~Person();
void setName(char*);
char* getName();
void setAge(int);
int getAge();
void setSSN(string);
string getSSN();
Person& operator=(const Person&);
};
// ...
class Student : public Person
{
friend istream& operator>>(istream&, Student&);
friend ostream& operator<<(ostream&, Student&);
private:
float m_gpa;
public:
Student() = default;
Student(char*, int, const string&, float);
Student(const Student&);
void setGPA(float);
float getGPA();
Student& operator=(const Student&);
};
istream& operator>>(istream &is, Student &iStudent)
{
// ?
return is;
}
Upvotes: 1
Views: 534
Reputation: 206607
operator>>
and operator<<
using the base class.virtual
member functions to redirect the implementation.class Person
{
friend std::istream& operator>>(std::istream&, Person&);
friend std::ostream& operator<<(std::ostream&, Person&);
....
public:
virtual std::istream& read(std::istream&) { ... }
virtual std::ostream& write(std::ostream&) const { ... }
...
};
std::istream& operator>>(std::istream &is, Person& person)
{
return persone.read(is);
}
std::stream& operator<<(std::ostream &os, Person const& person)
{
return persone.write(os);
}
class Student : public Person
{
...
public:
virtual std::istream& read(std::istream&) { ... }
virtual std::ostream& write(std::ostream&) const { ... }
...
};
Upvotes: 2