1. 【问题描述】 自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。 class DateTimeType { //自定义的日期时间类 DateTimeType DateType date; //类 DateType 的类对象 date 作为其数据成员 TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员 public: DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0); //构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值 DateType& GetDate(){ return date; } //返回本类的私有数据对象 data TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time void IncrementSecond(int s); //增加若干秒,注意“进位”问题 void PrintDateTime(); //屏幕输出日期时间对象的有关数据 }; 注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。 之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数: void main() { DateTimeType dttm1(1999,12,31,23,59,59), dttm2; (dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数 cout<<endl; dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime dttm2.PrintDateTime(); dttm1.IncrementSecond(30) ; //调用本派生类成员函数 dttm1.PrintDateTime(); } 【输入形式】 【输出形式】 【样例输入】 无 【样例输出】 1999-12-31 1999-12-31 23:59:59 1-1-1 0:0:0 2000-1-1 0:0:29
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; class DateType { private: int y0,m0,d0; public: DateType(int y0_,int m0_,int d0_):y0(y0_),m0(m0_),d0(d0_){} void PrintDate() { cout<<y0<<"-"<<m0<<"-"<<d0; } void Incre(int h_) { if(h_<24) return ; d0=d0+h_/24; int g[13]={0,31,28,31,30,31,30,31,31,30,31,31}; if(y0%4==0&&y0%100||y0%400==0) g[2]=29; if(d0>g[m0]) { d0=1; m0++; if(m0>12) m0=1,y0++; } } }; class TimeType { private: int hr,mi,se; public: TimeType(int hr_,int mi_,int se_):hr(hr_),mi(mi_),se(se_){} int Incre(int s) { se=se+s; if(se>=60) { mi=mi+se/60; se=se%60; if(mi>=60) { hr=hr+mi/60; mi=mi%60; int tp=hr; hr=hr%24; return tp; } } } void PrintTime() { cout<<hr<<":"<<mi<<":"<<se; } }; class DateTimeType { private: DateType date; TimeType time; public: DateTimeType(int y0=1,int m0=1,int d0=1,int hr0=0,int mi0=0,int se0=0):date(y0,m0,d0),time(hr0,mi0,se0){} DateType& GetDate(){return date;}//********** TimeType& GetTime(){return time;} void IncrementSecond(int s) { date.Incre(time.Incre(s)); //******** } void PrintDateTime() { date.PrintDate(); cout<<" "; time.PrintTime(); cout<<endl; } }; int main() { DateTimeType dttm1(1999,12,31,23,59,59),dttm2; (dttm1.GetDate()).PrintDate();//************ cout<<endl; dttm1.PrintDateTime(); dttm2.PrintDateTime(); dttm1.IncrementSecond(30); dttm1.PrintDateTime(); return 0; }
2. 【问题描述】 对本章示范题的用于管理商店商品的实现程序进行完善:完成Wardrobe立柜类的具体定义与使用,并添加“帽子仓库类”以及“立柜仓库类”的定义及使用,以使程序能够对商店的这三种商品(衬衣、帽子、立柜)进行简单的管理与应用。 要对商品实现的操作有:商品的进库(增加某类商品及其库存量),商品的出库(减少某类商品及其库存量),以及某类商品总价格的计算。 【输入形式】 根据提示输入 【输出形式】 把处理后的数据输出 【样例】注意:红色为输入部分,黑色为输出部分。 5 * shirt data in: price/place/material =>60 Tianjin Cotton 3 * shirt data in: price/place/material =>80 Beijing Wool 60 Tianjin Cotton 60 Tianjin Cotton 60 Tianjin Cotton 60 Tianjin Cotton 60 Tianjin Cotton 80 Beijing Wool 80 Beijing Wool 80 Beijing Wool shiSto.TotalPrice()=540 60 Tianjin Cotton 60 Tianjin Cotton 60 Tianjin Cotton 60 Tianjin Cotton shiSto.TotalPrice()=240 5 * Cap data in: price/place/material/style =>40 Suzhou Cotton M 3 * Cap data in: price/place/material/style =>30 Wuxi Wool S 40 Suzhou Cotton M 40 Suzhou Cotton M 40 Suzhou Cotton M 40 Suzhou Cotton M 40 Suzhou Cotton M 30 Wuxi Wool S 30 Wuxi Wool S 30 Wuxi Wool S capSto.TotalPrice()=290 40 Suzhou Cotton M 40 Suzhou Cotton M 40 Suzhou Cotton M 40 Suzhou Cotton M capSto.TotalPrice()=160 5 * Wardrobe data in: price/place/material/color =>160 Guangzhou Pine Yellow 3 * Wardrobe data in: price/place/material/color =>200 Suzhou Oak Brown 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 200 Suzhou Oak Brown 200 Suzhou Oak Brown 200 Suzhou Oak Brown WarSto.TotalPrice()=1400 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow 160 Guangzhou Pine Yellow WarSto.TotalPrice()=640
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; class Base { private: int price;char place[10],material[10]; public: Base(int pri,char *pla,char *mat) { price=pri; strcpy(place,pla); strcpy(material,mat); } int GetPrice(){return price;} void Display() { cout<<price<<" "<<place<<" "<<material; } }; class shirt:public Base { public: shirt(int pri,char *pla,char *mat):Base(pri,pla,mat){} void shirtDisplay(){Base::Display();} int GetPrice(){return Base::GetPrice();} }; class cap:public Base { private: char style[10]; public: cap(int pri,char *pla,char *mat,char *sty):Base(pri,pla,mat) { strcpy(style,sty); } int GetPrice(){return Base::GetPrice();} void Display() { Base::Display(); cout<<" "<<style; } }; class Wardrobe:public Base { private: char color[10]; public: Wardrobe(int pri,char *pla,char *mat,char *col):Base(pri,pla,mat) { strcpy(color,col); } int GetPrice(){return Base::GetPrice();} void Display() { Base::Display(); cout<<" "<<color; } }; int main() { int pri;char pla[10],mat[10],sty[10],col[10]; cout<<"5 * shirt data in: price/place/material =>"; cin>>pri>>pla>>mat;cout<<endl; shirt s1(pri,pla,mat); cout<<"3 * shirt data in: price/place/material =>"; cin>>pri>>pla>>mat;cout<<endl; shirt s2(pri,pla,mat); for(int i=1;i<=5;i++){s1.Display();cout<<endl;} for(int i=1;i<=3;i++){s2.Display();cout<<endl;} cout<<"shiSto.TotalPrice()="<<(s1.GetPrice()*5+s2.GetPrice()*3)<<endl; for(int i=1;i<=4;i++){s1.Display();cout<<endl;} cout<<"shiSto.TotalPrice()="<<(s1.GetPrice()*4)<<endl; cout<<"5 * Cap data in: price/place/material/style =>"; cin>>pri>>pla>>mat>>sty;cout<<endl; cap c1(pri,pla,mat,sty); cout<<"3 * Cap data in: price/place/material/style =>"; cin>>pri>>pla>>mat>>sty;cout<<endl; cap c2(pri,pla,mat,sty); for(int i=1;i<=5;i++){c1.Display();cout<<endl;} for(int i=1;i<=3;i++){c2.Display();cout<<endl;} cout<<"capSto.TotalPrice()="<<(c1.GetPrice()*5+c2.GetPrice()*3)<<endl; for(int i=1;i<=4;i++){c1.Display();cout<<endl;} cout<<"capSto.TotalPrice()="<<(c1.GetPrice()*4)<<endl; cout<<"5 * Wardrobe data in: price/place/material/color =>"; cin>>pri>>pla>>mat>>col;cout<<endl; Wardrobe w1(pri,pla,mat,col); cout<<"3 * Wardrobe data in: price/place/material/color =>"; cin>>pri>>pla>>mat>>col;cout<<endl; Wardrobe w2(pri,pla,mat,col); for(int i=1;i<=5;i++) {w1.Display();cout<<endl;} for(int i=1;i<=3;i++){w2.Display();cout<<endl;} cout<<"WarSto.TotalPrice()="<<(w1.GetPrice()*5+w2.GetPrice()*3)<<endl; for(int i=1;i<=4;i++){w1.Display();cout<<endl;} cout<<"WarSto.TotalPrice()="<<(w1.GetPrice()*4)<<endl; return 0; }
3. 【问题描述】 利用继承性与派生类来管理学生教师档案:由Person(人员)类出发(作为基类),派生出Student(学生)及Teacher(教师)类;而后又由Student(学生)类出发(作为基类),派生出GraduateStudent(研究生)类。可假定这几个类各自具有的数据成员为: Person(人员)类: 姓名、性别、年龄; Student(学生)类: 姓名、性别、年龄、学号、系别; Teacher(教师)类: 姓名、性别、年龄、职称、担任课程; GraduateStudent(研究生)类: 姓名、性别、年龄、学号、系别、导师。 为简化起见,每个类可只设立构造函数以及显示类对象数据的成员函数Print。而后编制简单的主函数,说明上述有关的类对象,并对其类成员函数进行简单使用(调用)。 【输入形式】 【输出形式】 【样例输入】 无 【样例输出】 == per1.Display() => name,age,sex sun 42 M == stu1.Display() => name,age,sex,Reg_Number,department guo 22 F 1001 comp == teach1.Display() => name,age,sex,course,post fang 38 M english professor == gStu.Display() => name,age,sex,Reg_Number,department,advisor wu 25 M 1021 comp wei
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; class Person { private: string name,age,sex; public: Person(string name_,string age_,string sex_) { name=name_;age=age_;sex=sex_; } void Display() { cout<<name<<" "<<age<<" "<<sex; } }; class Student:public Person { private: string Reg_Number,department; public: Student(string name_,string age_,string sex_,string Reg_Number_,string department_):Person(name_,age_,sex_) { Reg_Number=Reg_Number_;department=department_; } void Display() { Person::Display(); cout<<" "<<Reg_Number<<" "<<department; } }; class Teacher:public Person { private: string course,post; public: Teacher(string name_,string age_,string sex_,string course_,string post_):Person(name_,age_,sex_) { course=course_;post=post_; } void Display() { Person::Display();//************** cout<<" "<<course<<" "<<post; } }; class GraduateStudent:public Student { private: string advisor; public: GraduateStudent(string name_,string age_,string sex_,string Reg_Number_,string department_,string advisor_):Student(name_,age_,sex_,Reg_Number_,department_) { advisor=advisor_; } void Display() { Student::Display();//********* cout<<" "<<advisor; } }; int main() { cout<<"== per1.Display() => name,age,sex"<<endl; Person per1("sun","42","M"); per1.Display();cout<<endl; cout<<"== stu1.Display() => name,age,sex,Reg_Number,department"<<endl; Student stu1("guo","22","F","1001","comp"); stu1.Display();cout<<endl; cout<<"== teach1.Display() => name,age,sex,course,post"<<endl; Teacher teach1("fang","38","M","english","professor"); teach1.Display(); cout<<endl; cout<<"== gStu.Display() => name,age,sex,Reg_Number,department,advisor"<<endl; GraduateStudent gStu("wu","25","M","1021","comp","wei"); gStu.Display();cout<<endl; return 0; }