64,646
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Birthday
{
public:
Birthday();
Birthday(const Birthday& b);
friend istream& operator >>(istream& in,Birthday& b);
friend ostream& operator <<(ostream& out,Birthday& b);
private:
size_t year;
size_t month;
size_t day;
};
class People
{
public:
People();
People(const People& p);
friend istream& operator >>(istream& in,People& p);
friend ostream& operator <<(ostream& out,People& p);
protected:
string name;
Birthday bir;
};
class Student: public People
{
public:
Student();
Student(const Student& s);
friend istream& operator >>(istream& in,Student& s);
friend ostream& operator <<(ostream& out,Student& s);
private:
size_t score;
};
Birthday::Birthday(){}
Birthday::Birthday(const Birthday& b):year(b.year),month(b.month),day(b.day){}
istream& operator >>(istream& in,Birthday& b)
{
cout<<"Enter the birthday(year/month/day): ";
in>>b.year>>b.month>>b.day;
return in;
}
ostream& operator <<(ostream& out,Birthday& b)
{
out<<b.year<<"/"<<b.month<<"/"<<b.day<<endl;
return out;
}
People::People(){}
People::People(const People& p):name(p.name),bir(p.bir){}
istream& operator >>(istream& in,People& p)
{
cout<<"Enter the name: ";
in>>p.name;
in>>p.bir;
return in;
}
ostream& operator <<(ostream& out,People& p)
{
out<<"The name is: "<<p.name<<endl;
out<<"The birthday is: "<<p.bir;
return out;
}
Student::Student(){}
Student::Student(const Student& s):People(s),score(s.score){}
istream& operator >>(istream& in,Student& s)
{
cout<<"Enter the name: ";
in>>s.name;
in>>s.bir;
cout<<"Enter the score: ";
in>>s.score;
return in;
}
ostream& operator <<(ostream& out,Student& s)
{
out<<"The name is: "<<s.name<<endl;
out<<"The birthday is: "<<s.bir;
out<<"The score is: "<<s.score<<endl;
return out;
}
void Save(People &p,Student& s)
{
ofstream outfile;
outfile.open("people.txt",ios::out);
if(!outfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
outfile<<p<<endl<<"Stuent message is:"<<endl<<s;
cout<<"Save end!"<<endl;
}
int main()
{
People p;
cin>>p;
cout<<p;
Student s;
cin>>s;
cout>>s;
Save(p,s);
system("pause");
return 0;
}
cout << s;
//改成这样
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Birthday
{
public:
Birthday();
Birthday(const Birthday& b);
friend istream& operator >>(istream& in,Birthday& b)
{
cout<<"Enter the birthday(year/month/day): ";
in>>b.year>>b.month>>b.day;
return in;
}
friend ostream& operator <<(ostream& out,Birthday& b)
{
out<<b.year<<"/"<<b.month<<"/"<<b.day<<endl;
return out;
}
private:
size_t year;
size_t month;
size_t day;
};
class People
{
public:
People();
People(const People& p);
friend istream& operator >>(istream& in,People& p)
{
cout<<"Enter the name: ";
in>>p.name;
in>>p.bir;
return in;
}
friend ostream& operator <<(ostream& out,People& p)
{
out<<"The name is: "<<p.name<<endl;
out<<"The birthday is: "<<p.bir;
return out;
}
protected:
string name;
Birthday bir;
};
class Student: public People
{
public:
Student();
Student(const Student& s);
friend istream& operator >>(istream& in,Student& s)
{
cout<<"Enter the name: ";
in>>s.name;
in>>s.bir;
cout<<"Enter the score: ";
in>>s.score;
return in;
}
friend ostream& operator <<(ostream& out,Student& s)
{
out<<"The name is: "<<s.name<<endl;
out<<"The birthday is: "<<s.bir;
out<<"The score is: "<<s.score<<endl;
return out;
}
private:
size_t score;
};
Birthday::Birthday(){}
Birthday::Birthday(const Birthday& b):year(b.year),month(b.month),day(b.day){}
People::People(){}
People::People(const People& p):name(p.name),bir(p.bir){}
Student::Student(){}
Student::Student(const Student& s):People(s),score(s.score){}
void Save(People &p,Student& s)
{
ofstream outfile;
outfile.open("people.txt",ios::out);
if(!outfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
outfile<<p<<endl<<"Stuent message is:"<<endl<<s;
cout<<"Save end!"<<endl;
}
int main()
{
People p;
cin>>p;
cout<<p;
Student s;
cin>>s;
cout<<s;
Save(p,s);
system("pause");
return 0;
}