您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > c++第九章练习答案
1设计一个Person类,包含name、age、sex属性以及对这些属性操作的方法。实现并测试这个类。根据类的封装性要求,把name、age、sex声明为私有的数据成员,声明公有的成员函数Register()、ShowMe()来访问这些属性,在Register()函数中对数据成员进行初始化。person1通过cin来得到信息,person2通过Register(Zhang3,19,'m')来得到信息。程序的运行结果如下所示:Enteraperson'sname,ageandsex:Li418fperson1:Li418fperson2:Zhang319m#includeiostream#includecstringusingnamespacestd;classPerson{private:charName[20];charSex;intAge;public:voidRegister(char*name,intage,charsex);voidShowMe();};voidPerson::Register(char*name,intage,charsex){strcpy(Name,name);Age=age;Sex=(sex=='m'?'m':'f');}voidPerson::ShowMe(){coutNameAgeSexendl;}intmain(){charname[20],sex;intage;Personperson1,person2;coutEnteraperson'sname,ageandsex:endl;cinnameagesex;person1.Register(name,age,sex);coutperson1:;person1.ShowMe();person1.Register(Zhang3,19,'m');person2=person1;coutperson2:;person2.ShowMe();return0;}2设计一个完整的Date类,包含属性day,month,year以及对这些属性操作的方法。实现并测试这个类。根据类的封装性要求,把day,month,year声明为私有的数据成员。输入输出函数为voidinit(int,int,int)、voidprint_ymd()、voidprint_mdy()。程序的运行结果如下所示:Enterdate(daymonthyear:11220102010-12-112-1-2010(简单的日期类)公有的数据成员#includeiostreamusingnamespacestd;classDate{public:intday,mouth,year;};voidset_date(Date&d);voidshow_date1(Dated);voidshow_date2(Dated);intmain(){coutEnterdate(daymonthyear:endl;Datedate1;set_date(date1);show_date2(date1);show_date1(date1);return0;}voidset_date(Date&d){cind.dayd.mouthd.year;}voidshow_date1(Dated){coutd.mouth-d.day-d.yearendl;}voidshow_date2(Dated){coutd.year-d.mouth-d.dayendl;}(简单的完整日期类)私有的数据成员#includeiostreamusingnamespacestd;classDate{private:intday,mouth,year;public:voidinit(int,int,int);voidprint_ymd();voidprint_mdy();};voidDate::init(intyy,intmm,intdd){mouth=(mm=1&&mm=12)?mm:1;year=(yy=1900&&yy=2100)?yy:1900;day=(dd=1&&dd=31)?dd:1;}voidDate::print_ymd(){coutyear-mouth-dayendl;}voidDate::print_mdy(){coutmouth-day-yearendl;}intmain(){coutEnterdate(daymonthyear:endl;Datedate1;intday,mouth,year;cindaymouthyear;date1.init(year,mouth,day);date1.print_ymd();date1.print_mdy();return0;}3设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。程序的运行结果如下所示:Pleaseinputname,age,sexandweightahuang3m2.4Dog’sname:ahuangDog’sage:3Dog’ssex:mDog’sweight:2.4Dogspeak:Arf!Arf!注意,其中第2行“ahuang3m2.4”为输入,单引号为中文单引号#includeiostream#includecstringusingnamespacestd;classDog{charname[20];charsex;intage;floatweight;public:voidRegister(char*Name,intAge,charSex,floatWeight);char*GetName(){returnname;}intGetAge(){returnage;}charGetSex(){returnsex;}floatGetWeight(){returnweight;}voidSpeak(){coutArf!Arf!endl;}};voidDog::Register(char*Name,intAge,charSex,floatWeight){strcpy(name,Name);age=Age;sex=Sex;weight=Weight;}intmain(){coutPleaseinputname,age,sexandweightendl;charname[20];charsex;intage;floatweight;Dogdog1;cinnameagesexweight;dog1.Register(name,age,sex,weight);coutDog’sname:dog1.GetName()endl;coutDog’sage:dog1.GetAge()endl;coutDog’ssex:dog1.GetSex()endl;coutDog’sweight:dog1.GetWeight()endl;coutDogspeak:;dog1.Speak();return0;}4设计并测试一个名为Ellipse的椭圆类,其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积。根据类的封装性要求,在类的声明中用4个私有的整型变量表示两个点的坐标值,声明成员函数initial(int,int,int,int)初始化数据成员,函数GetPosition(int&,int&,int&,int&)读取坐标值,函数Area()计算面积。程序的运行结果如下所示:Pleaseinput4integerstodefineaellipse:3399Theareaofe1:28.2743Thecoordinatesofe2:1,1,4,3#includecmath#includeiostreamusingnamespacestd;classEllipse{private:intx1,y1,x2,y2;public:Ellipse(int,int,int,int);voidGetPosition(int&,int&,int&,int&);doubleArea();};Ellipse::Ellipse(inta,intb,intc,intd):x1(a),y1(b),x2(c),y2(d){}voidEllipse::GetPosition(int&a,int&b,int&c,int&d){a=x1,b=y1,c=x2,d=y2;}doubleEllipse::Area(){return(double)abs((x1-x2)*(y1-y2))/4*3.14159;}intmain(){intx1,y1,x2,y2;coutPleaseinput4integerstodefineaellipse:endl;cinx1y1x2y2;Ellipsee1(x1,y1,x2,y2);Ellipsee2(1,1,4,3);e2.GetPosition(x1,y1,x2,y2);coutTheareaofe1:e1.Area()endl;coutThecoordinatesofe2:x1','y1','x2','y2endl;return0;}5仿照Date类设计一个Time类,成员函数SetTime()设置时间,print_12()以12小时制显示时间(AM上午,PM下午),print_24()以24小时制显示时间。程序的运行结果如下所示:Pleaseinputatime(h,m,s)13233401:23:34PM13:23:34#includecmath#includeiostreamusingnamespacestd;classTime{inthour,minute,second;public:intSecCalc();Time(inth=0,intm=0,ints=0);voidSetTime(inth=0,intm=0,ints=0);voidprint_12();voidprint_24();voidprint_hms();TimeAdd(Time&);TimeSub(Time&);};Time::Time(inth,intm,ints){SetTime(h,m,s);}voidTime::SetTime(inthh,intmm,intss){hour=(hh=0&&hh=23)?hh:0;minute=(mm=0&&mm=59)?mm:0;second=(ss=0&&ss=59)?ss:0;}voidTime::print_24(){cout((hour10)?0:0)hour:((minute10)?0:0)minute:((second10)?0:0)second;}voidTime::print_12(){inthh=(hour==0||hour==12)?12:hour%12;cout((hour10)?0:0)hh:minute:second(hour12?AM:PM);}voidTime::print_hms(){couthour:minute:secondendl;}intTime::SecCalc(){return(hour*60+minute)
本文标题:c++第九章练习答案
链接地址:https://www.777doc.com/doc-2904349 .html