《c++各套试题答案》由会员分享,可在线阅读,更多相关《c++各套试题答案(29页珍藏版)》请在金锄头文库上搜索。
1、- 1 -第一套设计并测试一个矩形类(Rectangle) ,属性为矩形的左上与右下角的坐标,矩形水平放置。操作为计算矩形周长与面积。输出见样张-*/#include #include using namespace std;/*Program*/class Rectangleprivate:double left,top,right,bottom,area,perimeter;public:Rectangle(double l=0,double t=0,double r=0,double b=0);Rectangle();void Assign(double l,double t,doubl
2、e r,double b);void Show();double getLeft()return left;double getTop()return top;double getRight()return right;double getBottom()return bottom;double Area()return fabs(left-right)*(top-bottom);double Perimeter()return 2*fabs(left-right)+(top-bottom);Rectangle:Rectangle(double l,double t,double r,doub
3、le b)left=l;top=t;right=r;bottom=b;void Rectangle:Assign(double l,double t,double r,double b)left=l;top=t;right=r;bottom=b;void Rectangle:Show()cout#includeusing namespace std;class Personchar IdPerson19; /身份证号,18 位数字char Name20; /姓名char Sex; /性别int Birthday; /生日, 格式 1986 年 8 月 18 日写作 19860818char H
4、omeAddress50; /家庭地址public:Person(char a19,char b20,char c,int d,char e50);Person(Person Person();Person();void PrintPersonInfo(); /输出人员信息void inputPerson(); /输入人员信息;/*Program*/Person:Person(Person &Ps)strcpy(IdPerson,Ps.IdPerson);strcpy(Name,Ps.Name);Sex=Ps.Sex;Birthday=Ps.Birthday;strcpy(HomeAddres
5、s,Ps.HomeAddress);Person:Person(char*id,char*name,char sex,int birthday,char*homeadd)strcpy(IdPerson,id);strcpy(Name,name);Sex=sex;Birthday=birthday;strcpy(HomeAddress,homeadd);Person:Person()- 3 -IdPerson0=0;Name0=0;Sex=m;Birthday=0;HomeAddress0=0;Person:Person();/* End */void Person:inputPerson()c
6、outSex;coutBirthday;cin.get(); /吸收回车符,否则地址输不进去cout#include using namespace std;/*Program*/class Timedateprivate:int hh,mm,ss,year,month,date;public:Timedate(int y,int m,int d);Timedate();Timedate();void list();void putdate(int y,int m,int d); void puttime(int hh,int mm,int ss);Timedate:Timedate(int
7、y,int m,int d)year=y;month=m;date=d;hh=0;mm=0;ss=0;Timedate:Timedate()year=0;month=0;date=0;hh=0;mm=0;ss=0;void Timedate:putdate(int y,int m,int d)year=y;month=m;date=d;void Timedate:puttime(int h,int m,int s)hh=h;mm=m;ss=s;/* End */void Timedate:list()/成员函数 ,直接访问私有的数据成员cout#includeusing namespace s
8、td;const double PI=3.14159265;/*Program*/class Circleprivate:float radius,area,circumference;public:Circle(float=0);Cirlce(Circlevoid SetR(float Radius);float GetR()return radius;float GetCircumference()return circumference;float GetAreaCircle()return area;Circle:Circle(float Radius)radius=Radius;ar
9、ea=PI*radius*radius;circumference=PI*2*radius;Circle:Cirlce(Circle&cl)radius=cl.radius;area=cl.area;- 6 -circumference=cl.circumference;void Circle:SetR(float Radius)radius=Radius;area=PI*radius*radius;circumference=PI*2*radius;/* End */int main()Circle cl1(2),cl2,cl3=cl1;cout#includeusing namespace
10、 std;const double PI=3.14159265;/*Program*/class Circledouble radius,area,perimeter;public:Circle(double r=0);Circle()void SetR(double r);double GetR();double GetAreaCircle();double GetCircumference();Circle:Circle(double r)radius=r;area=PI*radius*radius;perimeter=2*PI*radius;void Circle:SetR(double
11、 r)radius=r;double Circle:GetR()return radius;- 7 -double Circle:GetAreaCircle()return PI*radius*radius;double Circle:GetCircumference()return 2*PI*radius;/* End */int main()Circle cl1(2),cl2,cl3=cl1;cout#include using namespace std;const double PI=3.1416;/*Program*/class Circleprivate:double radius
12、,area,circumference;public:Circle(double r=0);Circle();void Set(double r);double S()return PI*radius*radius;double L()return 2*PI*radius;Circle:Circle(double r)radius=r;void Circle:Set(double r)radius=r;- 8 -class Boxprivate:double bian,area,circumference;public:Box(double b=0);Box();void Set(double
13、 b);double S()return bian*bian;double L()return 4*bian;Box:Box(double b)bian=b;void Box:Set(double b)bian=b;class NewStylepublic:Circle a;Box b;NewStyle(Circle a1,Box b1);NewStyle();void Set(Circle a2,Box b2);double S()return (a.S()-b.S();double L()return (a.L()+b.L();NewStyle:NewStyle(Circle a1,Box
14、 b1)a=a1;b=b1;void NewStyle:Set(Circle a2,Box b2)a=a2;b=b2;/* End */int main()Circle A(4); /圆的半径为 4Box B(1); /正方形的边长为 1NewStyle C(A,B); cout#include - 9 -using namespace std;class dateint year;int month;int day;public:date(int Year=0,int Month=0,int Day=0);/构造函数,给出初始的年月日void setdate(int Year,int Month,int Day);/设置年月日int get_year();/获取年份int get_month();/获取月份int get_day();/获取日期;class card int book;/图书编号int reader;/借阅者编号date borrow_day;/出借日期 int expiry