完善下面的代码class Date { int Year, Month, Day; public: Date(int y = 2000, int m = 1, int d = 1)//A,带参数,且所有参数都有默认值 friend ostream& operator<<(ostream & out, Date & d) //插入运算符重载 void SetData(int y, int m, int d) //设置数据信息 }; class Person { char* Name; // 姓名,注意:用指针实现 Date Birth; //出生日期 public: Person(const char* namep = 0, int y = 0, int m = 0, int d = 0) // 构造函数,注意成员对象初始化 virtual~Person() // 因为在构造函数中动态申请了空间,则在析构函数中,需释放空间 void show(ostream& out) //显式信息,注意形参是输出流对象 void SetData(const char* namep , int y , int m , int d) //设置数据信息 }; class Student:public Person { float math; //数学成绩 float eng; //英语成绩 public: Student(const char* namep=0, int y = 0, int m = 0, int d = 0,float math = 0, float eng = 0) // 构造函数,注意基类成员初始化 friend ostream& operator<<(ostream& out, Student & s) //插入运算符重载 void SetData(const char* namep, int y, int m, int d, float math, float eng) //设置数据信息 }; int main() { Student * ps = new Student[N]; ps[0].SetData("张三", 2002, 1, 2, 89, 79); ps[1].SetData("李四", 2002, 12, 21, 67, 78); ps[2].SetData("王五", 2003, 10, 12, 87, 95); ofstream of("out.txt"); for (int i = 0; i < N; i++) { of << ps[i] << endl; //输出到文件 cout << ps[i] << endl; //输出到屏幕 } return 0; } 参考输出 Name:张三 Birth:2002.1.2 Math:89 English:79 Name:李四 Birth:2002.12.21 Math:67 English:78 Name:王五 Birth:2003.10.12 Math:87 English:95
时间: 2024-03-09 09:45:45 浏览: 72
完善的代码如下:
```c++
#include<iostream>
#include<fstream>
using namespace std;
const int N = 3;
class Date {
int Year, Month, Day;
public:
Date(int y = 2000, int m = 1, int d = 1) : Year(y), Month(m), Day(d) {} //A,带参数,且所有参数都有默认值
friend ostream& operator<<(ostream& out, Date& d); //插入运算符重载
void SetData(int y, int m, int d) { Year = y; Month = m; Day = d; } //设置数据信息
};
class Person {
char* Name; // 姓名,注意:用指针实现
Date Birth; //出生日期
public:
Person(const char* namep = 0, int y = 0, int m = 0, int d = 0) : Birth(y, m, d) // 构造函数,注意成员对象初始化
{
if (namep) {
Name = new char[strlen(namep) + 1];
strcpy_s(Name, strlen(namep) + 1, namep);
}
else {
Name = new char[1];
Name[0] = '\0';
}
}
virtual ~Person() { delete[] Name; } // 因为在构造函数中动态申请了空间,则在析构函数中,需释放空间
void show(ostream& out) //显式信息,注意形参是输出流对象
{
out << "Name:" << Name << " Birth:" << Birth;
}
void SetData(const char* namep, int y, int m, int d) //设置数据信息
{
if (Name) delete[] Name;
Name = new char[strlen(namep) + 1];
strcpy_s(Name, strlen(namep) + 1, namep);
Birth.SetData(y, m, d);
}
};
class Student :public Person {
float math; //数学成绩
float eng; //英语成绩
public:
Student(const char* namep = 0, int y = 0, int m = 0, int d = 0, float math = 0, float eng = 0) : Person(namep, y, m, d), math(math), eng(eng) {} // 构造函数,注意基类成员初始化
friend ostream& operator<<(ostream& out, Student& s); //插入运算符重载
void SetData(const char* namep, int y, int m, int d, float math, float eng) //设置数据信息
{
Person::SetData(namep, y, m, d);
this->math = math;
this->eng = eng;
}
};
ostream& operator<<(ostream& out, Date& d)
{
out << d.Year << "." << d.Month << "." << d.Day;
return out;
}
ostream& operator<<(ostream& out, Student& s)
{
s.show(out);
out << " Math:" << s.math << " English:" << s.eng;
return out;
}
int main() {
Student* ps = new Student[N];
ps[0].SetData("张三", 2002, 1, 2, 89, 79);
ps[1].SetData("李四", 2002, 12, 21, 67, 78);
ps[2].SetData("王五", 2003, 10, 12, 87, 95);
ofstream of("out.txt");
for (int i = 0; i < N; i++)
{
of << ps[i] << endl; //输出到文件
cout << ps[i] << endl; //输出到屏幕
}
delete[] ps;
return 0;
}
```
输出结果为:
```
Name:张三 Birth:2002.1.2 Math:89 English:79
Name:李四 Birth:2002.12.21 Math:67 English:78
Name:王五 Birth:2003.10.12 Math:87 English:95
```
注意,为了避免内存泄漏,我们需要在析构函数中释放动态分配的内存,同时还需要在 SetData 函数中释放旧的 Name 内存并重新分配内存。此外,我们还需要重载 Date 的插入运算符,以便在输出 Student 对象时可以正常输出 Birth 成员。
阅读全文