写了个关于友元成员函数的 运行的时候出错了.各位参谋下.
#include<iostream>#include<string>
using namespace std;
class Student;
class Date
{
private:
int year,month,day;
public:
Date(int y,int m,int d);
void display(const Student &stu);
};
class Student
{
private:
char *specialty;
public:
Student (char *pspec);
~Student();
friend void Date::display(const Student &);
};
Date::Date(int y,int m,int d)
{
cin>>y>>" ">>m>>" ">>d;
year=y;
month=m;
day=d;
}
void Date::display(const Student &stu)
{
cout<<stu.specialty<<endl;
cout<<year<<" "<<month<<" "<<day<<endl;
}
Student::Student(char *pspec)
{
if(pspec)
{
specialty=new char[strlen(pspec)+1];
strcpy(specialty,pspec);
}
else specialty=0;
}
Student::~Student()
{
if(specialty)
delete []specialty;
else specialty=0;
}
int main()
{
int y,m,d;
cin>>y>>" ">>m>>" ">>d;
Student zhang ("computer");
Date birthday (y,m,d);
birthday. display (zhang);
return 0;
}
运行的时候我输入日期 按回车 然后就突然中止了