using namespace std;
enum AMPM{AM=1,PM}; class Building{ public: Building(char *); void ShowBuilding(){ cout< protected: char Name[30]; }; Building::Building(char *name){ strcpy(Name,name); }; class Teach_Building: public Building { public: Teach_Building(char *,int,int,int,int); void ShowTeach_Building(){ Building::ShowBuilding(); cout<<\ cout<<\ cout<<\ cout<<\ } protected: int No,Floors,ClassRooms,Area; }; Teach_Building::Teach_Building(char *name,int no,int fl,int cr,int ar):Building(name){ No=no;Floors=fl;ClassRooms=cr;Area=ar; } class Dorm_Building:public Building { public: Dorm_Building(char *,int,int,int,int,int); void ShowDorm_Building(){ Building::ShowBuilding(); cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ } protected: int No,Floors,DormRooms,Area,StudentNum; }; Dorm_Building:: Dorm_Building(char *name,int no,int fl,int dr,int ar,int sn):Building(name){ No=no;Floors=fl;DormRooms=dr;Area=ar;StudentNum=sn; } int main(){ Teach_Building tb(\主教学楼\ Dorm_Building db(\北苑男生宿舍\ tb.ShowTeach_Building(); db.ShowDorm_Building(); return 0; } 5.3定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。 (这题做得不太满意) #include protected: int Legs; }; class Circle{ public: Circle(int radius){ Radius=radius; } protected: int Radius; }; class RoundTable:public Table,public Circle{ public: RoundTable(int legs,int radius):Table(legs),Circle(radius){} protected: }; int main(){ return 0; } 5.4利用第四章习题5.2 Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。 #include enum AMPM{AM=1,PM}; class Clock{ public: Clock(int=0, int=0, int s=0); void ShowTime(){ cout< Clock::Clock(int h, int m, int s){ Hour=h;Minute=m;Second=s; }; class NewClock: public Clock { public: NewClock(); NewClock(Clock c,AMPM ap):Clock(c){ Ap=ap; } void ShowTime(){ Clock::ShowTime(); cout<<(Ap==AM ? \ } private: AMPM Ap; }; int main(){ NewClock nc(Clock(8,23,34),AMPM(2)); nc.ShowTime(); return 0; } 5.5利用NewClock类与Date类定义一个带日期的时钟类 ClockwithDate。对该类对象能进行增加减少秒数操作。 #include enum AMPM{AM=1,PM}; class Clock{ public: Clock(int=0, int=0, int s=0); void ShowTime(){ cout< protected: int Hour,Minute,Second; }; Clock::Clock(int h, int m, int s){ Hour=h;Minute=m;Second=s; }; class NewClock: public Clock { public: NewClock(Clock c,AMPM ap):Clock(c){ Ap=ap; } void ShowTime(){ Clock::ShowTime(); cout<<(Ap==AM ? \ } protected: AMPM Ap; }; class Date{ protected: int Year,Month,Day; static const int days[]; bool LeapYear(); bool EndofMonth(); public: Date(int=1900,int=1,int =1); void Increment(int ); void Decrement(int); void SetDate(int,int,int); void ShowDate(); }; const int Date::days[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; Date::Date(int y,int m,int d){ SetDate(y,m,d); } void Date::SetDate(int y,int m,int d){ Year=(y>=1900 && y<=9999)?y:1900; Month=(m>=1 &&m<=12)?m:1; if(Month==2 && LeapYear()) Day=(d>=1 && d<=29)?d:1; else Day=(d>=1 && d<=days[Month])?d:1; } bool Date::LeapYear(){ return ((Year@0 == 0) || (Year%4==0 && Year0!=0))? true:false; } bool Date::EndofMonth(){ if(Month==2 && LeapYear()) return Day==29; else return Day==days[Month]; } void Date::Increment(int n){ int i; for(i=1;i<=n;i++) if(EndofMonth() && Month==12){ Year++; Month=Day=1; } else if(EndofMonth()){ Month++;Day=1; } else Day++; } void Date::Decrement(int n){ int i; for(i=1;i<=n;i++) if(Day==1){ if(Month==1){ Year--; Month=12; Day=31; } else if(Month==3){ Day=LeapYear()?29:28;Month=2; } else Day=days[--Month]; } else Day--; }