写一个学生类中包含一个日期类作为子对象的程序,且不能用string类的函数,用char类型的数组来存名字,用char 类型的指针来做形参
时间: 2023-06-06 19:07:12 浏览: 50
好的,这个问题属于编程类问题,可以回答。
以下是一个基本的学生类中包含日期类的程序:
```
#include<iostream>
using namespace std;
class Date{
private:
int year;
int month;
int day;
public:
Date(int y=0,int m=0,int d=0):year(y),month(m),day(d) {}
void setDate(int y,int m,int d){
year=y;
month=m;
day=d;
}
void showDate(){
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
};
class Student{
private:
char name[30];
char* ptr;
Date birthday;
public:
Student(char* n,Date b){
ptr=name;
while((*ptr++=*n++)!='\0'){}
birthday=b;
}
void showStudent(){
ptr=name;
cout<<"姓名:";
while(*ptr!='\0') cout<<*ptr++;
cout<<endl<<"生日:";
birthday.showDate();
}
};
int main(){
Date date(1999,9,9);
Student student("张三",date);
student.showStudent();
return 0;
}
```
注意,这个程序中没有使用string类的函数,用char类型的数组来存储名字,用char类型的指针来做形参。同时,日期类作为学生类的子对象。