#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: 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; }
时间: 2023-08-26 20:06:30 浏览: 81
class Plants: public Dates {
public:
Plants(char*n,int y=0,int m=0,int d=0);
~Plants();
char*GetName();
private:
char *name; // 植物名称
};
在类定义的行末加上 ": public Dates" 即可将 Plants 类设置为 Dates 类的子类,继承 Dates 类的成员变量和成员函数。
阅读全文
相关推荐

















