#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; }
时间: 2023-08-26 11:06:30 浏览: 73
这段代码是一个简单的植物类和日期类的实现,其中植物类继承自日期类。在植物类中,定义了一个植物名称和一个构造函数,用于初始化植物名称和开花日期。在日期类中,定义了一个年月日成员变量和相应的成员函数,用于获取年月日信息。还定义了一个判断两个植物的开花日期是否相同的函数IsEqual。在主函数中,创建了两个植物对象,输出它们的开花时间,并判断它们是否在同一天开花。
阅读全文
相关推荐

















