Piotre Ugrumov wrote:
> I have tried to implement the overload of these 2 operators.
> ostream & operator<<(ostream &out, Person &p){
> out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl;
> return out;
> }
> This overload work but I have a curiosty
> If I try to approch to the name or to the surname or to the
> dateofbirth in this way i receive error:
What error?
> ostream & operator<<(ostream &out, Person &p){
> out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
> return out;
> }
Why would you want that instead of the first version? This version
accesses protected members directly and therefore needs to be a friend,
the first doesn't. Anyway, maybe this version doesn't work, because
it's not a friend of your class (the friend operator<<'s signature is
different).
> I don't know how implement the overload of >>, I have tried in this
> way but this system don't work:
What do you mean by "don't work"?
> istream & operator>>(istream &in, Person &p){
> char tmp1[30];
> char tmp2[30];
> char tmp3[30];
You should really use strings instead of arrays of char.
> in>>tmp1;
> in>>tmp2;
> in>>tmp3;
> Person::setName(tmp1);
> Persona::setSurname(tmp2);
> Persona::setDateOfBirth(tmp3);
You have to call those non-static member functions the same way as in
the operator>>, for an object, i.e.:
p.setName(tmp1);
....
> return in;
> }
>
> How can I implement the overload o the operator >>?
>
>
> I have defined these overload in this class:
> #pragma once
> #include <iostream>
> using namespace std;
>
> class Person
> {
> friend ostream & operator<<(ostream &, const Persona &);
> friend istream & operator>>(istream &, Persona &);
> public:
> Persona(char *n, char *c, char *dn);
> void setName(char *n);
> void setSurname(char *c);
> void setDataOBirth(char *dn);
> char* getName();
Make that:
const char* getName() const;
Or even better:
std::string getName() const;
Same for the next two members.
> char* getSurname();
> char* getDateOfBirth();
> ~Persona(void);
> protected:
> char *name, *surname, *dateofbirth;
Why are those member variables protected and not private?
> };
>
> If in a class son of Person
You mean a class that is derived from it?
> I define the overload of << and >>, can I
> do this thing?
> ostream & operator<<(ostream &out, Person &p){
^^^^^^
Don't you mean that to be another class?
> Person:
perator<<
> //do something
> return out;
> }
You can do something like this:
ostream& operator<<(ostream& out, const Manager& p)
{
out << static_cast<Person&>(p);
//do the rest
return out;
}
But note that this won't work correctly with polymorphism, since
non-member-functions cannot work polymorphic. If you need polymorphism,
you need to add a member function to your class, like:
ostream& Person::write(ostream& out) const
{
return out << name << " " << surname << ", " << dateofbirth;
}
declare it virtual in the Person class and override it in your derived
classes. Then write your operator as:
ostream& operator<<(ostream& out, const Person& p)
{
return p.write(out);
}
Then you only need that single one operator<<, and the virtual member
functions will take care of calling the right write() function.