这里面改了一些东西。就是利用操作符重载。然后再设为友元。我的这个程序是在DEV
C++里面运行的。我试了,在VC
6.0下,不行。不过,语法是书上说的,没有错。可能是因为那个编译器不支持吧。
#include
#include
using
namespace
std;
class
Person
{
string
name;
int
num;
public:
friend
istream&
operator
>>
(istream
&is,
Person
&p);
void
print();
};
void
Person::print()
{
cout<<"your
name
is:"<
>
(istream
&is,
Student
&s);
void
disp1();
};
void
Student
::disp1()
{
//Person::print();
cout<<"your
score
is:"<
>
(istream
&is,
Person
&p)
{
is
>>
p.name
>>
p.num;
return
is;
}
istream&
operator
>>
(istream
&is,
Student
&s)
{
is
>>
s.score
>>
s.classroom;
return
is;
}
int
main()
{
Person
P;
Student
s;
cout
<<
"input
name
and
num:
";
cin
>>
P;
cout
<<
"input
score
and
classroom:
";
cin
>>
s;
P.print();
s.disp1();
system("pause");
return
0;
}
温馨提示:内容为网友见解,仅供参考