#include <iostream> #include <cstring> using namespace std; class Date { public: int year; int month; int day; Date() {year = 1900; month = 1; day = 1;} Date(int y, int m, int d) : year(y), month(m), day(d) {} // Date(const Date& b) {year = b.year; month = b.month; day = b.day;} }; class people { char name[11]; char number[7]; char sex[3]; Date birth; char id[16]; public: char* getName() {return name;} char* getNumber() {return number;} char* getSex() {return sex;} Date getBirth() {return birth;} char* getId() {return id;} people() {} people(char* nm, char* no, char* gd, Date b, char* i) : birth(b) { strcpy(name, nm); strcpy(number, no); strcpy(sex, gd); strcpy(id, i); } }; class teacher : virtual public people { char principalship[11]; char department[21]; public: char* getPs() {return principalship;} char* getDpm() {return department;} teacher() {} teacher(char* ps, char* dpm) { strcpy(principalship, ps); strcpy(department, dpm); } teacher(char* nm, char* no, char* gd, Date b, char* i, char* ps, char* dpm) : people(nm, no, gd, b, i) { strcpy(principalship, ps); strcpy(department, dpm); } }; class student : virtual public people { char classNO[7]; public: char* getClassNO() {return classNO;} student() {} student(char* cln) {strcpy(classNO, cln);} student(char* nm, char* no, char* gd, Date b, char* i, char* cln) : people(nm, no, gd, b, i) { strcpy(classNO, cln); } }; class graduate : public student { char subject[21]; teacher adviser; public: char* getSubject() {return subject;} teacher getAdviser() {return adviser;} graduate() {} graduate(char* sbj, teacher adv) : adviser(adv) {strcpy(subject, sbj);} graduate(char* cln, char* sbj, teacher adv) : student(cln), adviser(adv) {strcpy(subject, sbj);} graduate(char* nm, char* no, char* gd, Date b, char* i, char* cln, char* sbj, teacher adv) : student(nm, no, gd, b, i, cln), adviser(adv) {strcpy(subject, sbj);} }; class TA : public graduate, public teacher { public: TA() {} TA(char* nm, char* no, char* gd, Date b, char* i, char* ps, char* dpm, char* cln, char* sbj, teacher adv) : people(nm, no, gd, b, i), teacher(ps, dpm), graduate(cln, sbj, adv) {} }; int main() { /********** Begin **********/ /********** End **********/ cout << "TA name: " << ta.getName() << endl; cout << "No.: " << ta.getNumber() << endl; cout << "Department: " << ta.getDpm() << endl; cout << "Class No.: " << ta.getClassNO() << endl; cout << "Subject: " << ta.getSubject() << endl; cout << "Advisor: " << ad.getName() << endl; return 0; }
时间: 2023-12-14 17:39:01 浏览: 228
在main函数中,需要先创建一个TA对象,然后通过对象调用方法输出TA的信息。
可以按照以下步骤完成:
1. 创建一个TA对象,传入姓名、学号、性别、出生日期、身份证号、职务、所在部门、班级号、研究方向和导师信息。
```
TA ta("张三", "20210001", "男", Date(2000, 1, 1), "1234567890123456", "助教", "计算机科学与技术系", "计算机1901", "机器学习", teacher("李四", "副主任"));
```
2. 输出TA的信息,调用对象的方法即可。
```
cout << "TA name: " << ta.getName() << endl;
cout << "No.: " << ta.getNumber() << endl;
cout << "Department: " << ta.getDpm() << endl;
cout << "Class No.: " << ta.getClassNO() << endl;
cout << "Subject: " << ta.getSubject() << endl;
cout << "Advisor: " << ta.getAdviser().getName() << endl;
```
完整代码如下:
相关问题
#include<iostream> #include<cstring> using namespace std; class Dates{ public: Dates(int y=0,int m=0,int d=0):year(y),month(m),day(d){ } ~Dates(){ } int GetYear(){return year;} int GetMonth(){return month;} int GetDay(){return day;} private: //**********found********** int year; int month; int day; // 开花日期(年月日) }; //**********found********** class Plants: public Dates{ public: Plants(char*n,int y=0,int m=0,int d=0); ~Plants(); char*GetName(); private: char *name; // 植物名称 }; //**********found********** Plants::Plants(char*n,int y,int m,int d): date(y,m,d) { int length; length=strlen(n); name=new char[length+1]; strcpy(name,n); } Plants::~Plants() { delete []name; } char* Plants::GetName() { return name ; } bool IsEqual(Plants &pa,Plants &pb) // 判断pa和pb开花日期是否相同,是返回真,否则返回假 { if(pa.GetYear()==pb.GetYear()) //**********found********** if(pa.GetMonth()==pb.GetMonth()) if(pa.GetDay()==pb.GetDay())return true; return false; } int main() { Plants plantA("桃",2020,3,10); Plants plantB("杏",2020,4,5); cout<<plantA.GetName()<<"开花时间是:"<<plantA.GetYear()<<'-'<<plantA.GetMonth()<<'-'<<plantA.GetDay()<<endl; cout<<plantB.GetName()<<"开花时间是:"<<plantB.GetYear()<<'-'<<plantB.GetMonth()<<'-'<<plantB.GetDay()<<endl; if(IsEqual(plantA,plantB)) cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"在同一天开花。"<<endl; else cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"不在同一天开花。"<<endl; system("pause"); return 0; }
这段代码是一个简单的植物类和日期类的实现,其中植物类继承自日期类。在植物类中,定义了一个植物名称和一个构造函数,用于初始化植物名称和开花日期。在日期类中,定义了一个年月日成员变量和相应的成员函数,用于获取年月日信息。还定义了一个判断两个植物的开花日期是否相同的函数IsEqual。在主函数中,创建了两个植物对象,输出它们的开花时间,并判断它们是否在同一天开花。
#include<iostream> #include<cstring> using namespace std; class Dates{ public: Dates(int y=0,int m=0,int d=0):year(y),month(m),day(d){ } ~Dates(){ } int GetYear(){return year;} int GetMonth(){return month;} int GetDay(){return day;} private: //**********found********** ; // 开花日期(年月日) }; //**********found********** class Plants: { public: Plants(char*n,int y=0,int m=0,int d=0); ~Plants(); char*GetName(); private: char *name; // 植物名称 }; //**********found********** Plants::Plants(char*n,int y,int m,int d): { int length; length=strlen(n); name=new char[length+1]; strcpy(name,n); } Plants::~Plants() { delete []name; } char* Plants::GetName() { return name ; } bool IsEqual(Plants &pa,Plants &pb) // 判断pa和pb开花日期是否相同,是返回真,否则返回假 { if(pa.GetYear()==pb.GetYear()) //**********found********** if() if(pa.GetDay()==pb.GetDay())return true; return false; } int main() { Plants plantA("桃",2020,3,10); Plants plantB("杏",2020,4,5); cout<<plantA.GetName()<<"开花时间是:"<<plantA.GetYear()<<'-'<<plantA.GetMonth()<<'-'<<plantA.GetDay()<<endl; cout<<plantB.GetName()<<"开花时间是:"<<plantB.GetYear()<<'-'<<plantB.GetMonth()<<'-'<<plantB.GetDay()<<endl; if(IsEqual(plantA,plantB)) cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"在同一天开花。"<<endl; else cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"不在同一天开花。"<<endl; system("pause"); return 0; }
在 Dates 类中,需要添加三个私有成员变量来存储开花日期的年、月、日。具体实现可参考如下代码:
```c++
class Dates{
public:
Dates(int y=0,int m=0,int d=0):year(y),month(m),day(d){ }
~Dates(){ }
int GetYear(){return year;}
int GetMonth(){return month;}
int GetDay(){return day;}
private:
int year; // 年
int month; // 月
int day; // 日
};
```
在 Plants 类的构造函数和 IsEqual 函数中,需要使用 Dates 类来存储和比较开花日期。具体实现可参考如下代码:
```c++
class Plants{
public:
Plants(char*n,int y=0,int m=0,int d=0);
~Plants();
char*GetName();
Dates GetDate(){return date;}
private:
char *name; // 植物名称
Dates date; // 开花日期
};
Plants::Plants(char*n,int y,int m,int d):date(y,m,d){
int length;
length=strlen(n);
name=new char[length+1];
strcpy(name,n);
}
bool IsEqual(Plants &pa,Plants &pb){
if(pa.GetDate().GetYear()==pb.GetDate().GetYear() && pa.GetDate().GetMonth()==pb.GetDate().GetMonth() && pa.GetDate().GetDay()==pb.GetDate().GetDay())
return true;
return false;
}
```
在 main 函数中,需要修改输出开花日期的代码,具体实现可参考如下代码:
```c++
int main() {
Plants plantA("桃",2020,3,10);
Plants plantB("杏",2020,4,5);
cout<<plantA.GetName()<<"开花时间是:"<<plantA.GetDate().GetYear()<<'-'<<plantA.GetDate().GetMonth()<<'-'<<plantA.GetDate().GetDay()<<endl;
cout<<plantB.GetName()<<"开花时间是:"<<plantB.GetDate().GetYear()<<'-'<<plantB.GetDate().GetMonth()<<'-'<<plantB.GetDate().GetDay()<<endl;
if(IsEqual(plantA,plantB))
cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"在同一天开花。"<<endl;
else
cout<<plantA.GetName()<<"和"<<plantB.GetName()<<"不在同一天开花。"<<endl;
system("pause");
return 0;
}
```
阅读全文