Overloaded, inheritance, scope resolution

Hi guys,

Came upon a syntactic problem, and looking for a little help.

I have two classes, Patient which is derived from Person;

1
2
3
4
5
6
7
8
9

class Patient:public Person{

public:
void input();
void print();

};


Then I have this class

1
2
3
4
5
6
7

class Person{

friend std::istream& operator>>(std::istream& input, Person& the_person);

};


Now, if the Person class had a function called...

 
void input();


Inside the Patient class, I could do this...

1
2
3
4
5
6
7
8
9
10
Patient::input(){

Person::input();
/*
more code here

*/

}


However, since Person is using overloading, I am not sure how to syntactically make it work.

So my question is, inside of the Patient::input() function, how do I "input" a person using the overloaded >> operator.

Any help would be greatly appriciated. Of course I could just cheap out and not use the >> operator, but I am trying to learn new techniques.

Thanks a lot,

Mike


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

struct person
{
    // ...
    protected:
        virtual std::istream& read( std::istream& stm )
        { /* read person stuff from stm */ return stm ; }

        virtual std::ostream& write( std::ostream& stm ) const
        { /* write person stuff to stm */ return stm ; }

    friend inline std::istream& operator >> ( std::istream& stm, person& p )
    { return p.read(stm) ; }

    friend inline std::ostream& operator << ( std::ostream& stm, const person& p )
    { return p.write(stm) ; }
};

struct patient : person
{
    // ...
    protected:
        virtual std::istream& read( std::istream& stm )
        { person::read(stm) ; /* read extra patient stuff from stm */ return stm ; }

        virtual std::ostream& write( std::ostream& stm ) const
        { person::write(stm) ; /* write extra patient stuff to stm */ return stm ; }

};

int main()
{
    person a ;
    patient b ;
    std::cin >> a >> b ;
    std::cout << a << '\n' << b ;
}
You wouldn't need a function called input:

1
2
3
Person P;
std::ifstream inf("thefile.dat");
inf>>P;


Then in the implementation of the person you would define where the data goes.

Is this what you are asking?
Thanks pogrady and JLBorges, got it solved.
Topic archived. No new replies allowed.