64,683
社区成员
发帖
与我相关
我的任务
分享
#include<iostream.h>
class Student1
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
friend ostream& operator<<(ostream &out,Student1 &stud)
{
out<<"number:\n"<<stud.num<<"name:\n"<<stud.name<<"sex:\n"<<stud.sex<<endl;
return out;
}
private:
int num;
char name[20];
char sex;
};
class Student2:public Student1
{
public:
void get_value_1()
{
get_value();
cin>>age>>addr;
cout<<endl;
}
friend ostream& operator<<(ostream & out,Student2 stud)//形参要给出
{
out<<"age:\n"<<stud.age<<"addr:\n"<<stud.addr;
return out;
}
protected: //关键字错了
int age;
char addr[30];
};
int main()
{
Student2 stud;
cout<<"input the Student's number,name,sex,age,addr:\n";
stud.get_value_1();
cout<<stud;
return 0;
}