c++第九章练习答案.docx
《c++第九章练习答案.docx》由会员分享,可在线阅读,更多相关《c++第九章练习答案.docx(16页珍藏版)》请在冰点文库上搜索。
c++第九章练习答案
1设计一个Person类,包含name、age、sex属性以及对这些属性操作的方法。
实现并测试这个类。
根据类的封装性要求,把name、age、sex声明为私有的数据成员,声明公有的成员函数Register()、ShowMe()来访问这些属性,在Register()函数中对数据成员进行初始化。
person1通过cin来得到信息,person2通过Register("Zhang3",19,'m')来得到信息。
程序的运行结果如下所示:
Enteraperson'sname,ageandsex:
Li418f
person1:
Li418f
person2:
Zhang319m
#include
#include
usingnamespacestd;
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()
{
cout<}
intmain()
{
charname[20],sex;
intage;
Personperson1,person2;
cout<<"Enteraperson'sname,ageandsex:
"<cin>>name>>age>>sex;
person1.Register(name,age,sex);
cout<<"person1:
";
person1.ShowMe();
person1.Register("Zhang3",19,'m');
person2=person1;
cout<<"person2:
";
person2.ShowMe();
return0;
}
2设计一个完整的Date类,包含属性day,month,year以及对这些属性操作的方法。
实现并测试这个类。
根据类的封装性要求,把day,month,year声明为私有的数据成员。
输入输出函数为voidinit(int,int,int)、voidprint_ymd()、voidprint_mdy()。
程序的运行结果如下所示:
Enterdate(daymonthyear:
1122010
2010-12-1
12-1-2010
(简单的日期类)公有的数据成员
#include
usingnamespacestd;
classDate
{
public:
intday,mouth,year;
};
voidset_date(Date&d);
voidshow_date1(Dated);
voidshow_date2(Dated);
intmain()
{
cout<<"Enterdate(daymonthyear:
"<Datedate1;
set_date(date1);
show_date2(date1);
show_date1(date1);
return0;
}
voidset_date(Date&d)
{
cin>>d.day>>d.mouth>>d.year;
}
voidshow_date1(Dated)
{
cout<}
voidshow_date2(Dated)
{
cout<}
(简单的完整日期类)私有的数据成员
#include
usingnamespacestd;
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()
{
cout<}
voidDate:
:
print_mdy()
{
cout<}
intmain()
{
cout<<"Enterdate(daymonthyear:
"<Datedate1;
intday,mouth,year;
cin>>day>>mouth>>year;
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,sexandweight
ahuang3m2.4
Dog’sname:
ahuang
Dog’sage:
3
Dog’ssex:
m
Dog’sweight:
2.4
Dogspeak:
Arf!
Arf!
注意,其中第2行“ahuang3m2.4”为输入,单引号为中文单引号
#include
#include
usingnamespacestd;
classDog
{
charname[20];
charsex;
intage;
floatweight;
public:
voidRegister(char*Name,intAge,charSex,floatWeight);
char*GetName(){returnname;}
intGetAge(){returnage;}
charGetSex(){returnsex;}
floatGetWeight(){returnweight;}
voidSpeak(){cout<<"Arf!
Arf!
"<};
voidDog:
:
Register(char*Name,intAge,charSex,floatWeight)
{
strcpy(name,Name);
age=Age;
sex=Sex;
weight=Weight;
}
intmain()
{
cout<<"Pleaseinputname,age,sexandweight"<charname[20];
charsex;
intage;
floatweight;
Dogdog1;
cin>>name>>age>>sex>>weight;
dog1.Register(name,age,sex,weight);
cout<<"Dog’sname:
"<cout<<"Dog’sage:
"<cout<<"Dog’ssex:
"<cout<<"Dog’sweight:
"<cout<<"Dogspeak:
";
dog1.Speak();
return0;
}
4设计并测试一个名为Ellipse的椭圆类,其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积。
根据类的封装性要求,在类的声明中用4个私有的整型变量表示两个点的坐标值,声明成员函数initial(int,int,int,int)初始化数据成员,函数GetPosition(int&,int&,int&,int&)读取坐标值,函数Area()计算面积。
程序的运行结果如下所示:
Pleaseinput4integerstodefineaellipse:
3399
Theareaofe1:
28.2743
Thecoordinatesofe2:
1,1,4,3
#include
#include
usingnamespacestd;
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;
cout<<"Pleaseinput4integerstodefineaellipse:
"<cin>>x1>>y1>>x2>>y2;
Ellipsee1(x1,y1,x2,y2);
Ellipsee2(1,1,4,3);
e2.GetPosition(x1,y1,x2,y2);
cout<<"Theareaofe1:
"<cout<<"Thecoordinatesofe2:
"<return0;
}
5仿照Date类设计一个Time类,成员函数SetTime()设置时间,print_12()以12小时制显示时间(AM上午,PM下午),print_24()以24小时制显示时间。
程序的运行结果如下所示:
Pleaseinputatime(h,m,s)
132334
01:
23:
34PM
13:
23:
34
#include
#include
usingnamespacestd;
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<<((hour<10)?
"0":
"0")<"
<<((minute<10)?
"0":
"0")<"
<<((second<10)?
"0":
"0")<}
voidTime:
:
print_12()
{
inthh=(hour==0||hour==12)?
12:
hour%12;
cout<<((hour<10)?
"0":
"0")<"
<"
<<<(hour<12?
"AM":
"PM");
}
voidTime:
:
print_hms()
{
cout<"<"<}
intTime:
:
SecCalc()
{
return(hour*60+minute)*60+second;
}
TimeTime:
:
Add(Time&t)
{
Timetmp;
intss;
ss=SecCalc()+t.SecCalc();
tmp.hour=ss/3600%24;
tmp.minute=ss%3600/60;
tmp.second=ss%3600%60;
returntmp;
}
TimeTime:
:
Sub(Time&t)
{
Timetmp;
intss;
ss=abs(SecCalc()-t.SecCalc());
tmp.hour=ss/3600%24;
tmp.minute=ss%3600/60;
tmp.second=ss%3600%60;
returntmp;
}
intmain()
{
Timet1(2,34),t2;
cout<<"Pleaseinputatime(h,m,s)"<ints,h,m;
cin>>h>>m>>s;
t2.SetTime(h,m,s);
t2.print_12();
cout<<""<t2.print_hms();
return0;
}
#include
#include
usingnamespacestd;
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<<((hour<10)?
"0":
"")<"
<<((minute<10)?
"0":
"")<"
<<((second<10)?
"0":
"")<cout<}
voidTime:
:
print_12()
{
inthh=(hour==0||hour==12)?
12:
hour%12;
cout<<((hour<10)?
"0":
"")<"
<"
<<<(hour<12?
"AM":
"PM");
}
voidTime:
:
print_hms()
{
cout<"<"<}
intTime:
:
SecCalc()
{
return(hour*60+minute)*60+second;
}
TimeTime:
:
Add(Time&t)
{
Timetmp;
intss;
ss=SecCalc()+t.SecCalc();
tmp.hour=ss/3600%24;
tmp.minute=ss%3600/60;
tmp.second=ss%3600%60;
returntmp;
}
TimeTime:
:
Sub(Time&t)
{
Timetmp;
intss;
ss=abs(SecCalc()-t.SecCalc());
tmp.hour=ss/3600%24;
tmp.minute=ss%3600/60;
tmp.second=ss%3600%60;
returntmp;
}
intmain()
{
Timet2;
cout<<"Pleaseinputatime(h,m,s)"<ints,h,m;
cin>>h>>m>>s;
t2.SetTime(h,m,s);
t2.print_12();
cout<<""<t2.print_24();
return0;
}
6设计一个Circle(圆)类,其私有数据成员有圆心坐标x和y及半径r。
其公有成员函数为:
Set(int,int,double);设置圆的属性;
voidshow(void);显示圆心和半径;
doublecircumference(void);计算周长;
doublearea(void);计算面积;
在主函数中用该类声明对象,输入圆心、半径,调用相关函数为对象设置属性,显示圆的半径、圆心,计算圆的周长、面积。
注意,圆周率用3.14。
程序运行结果如下:
请输入圆心坐标和圆的半径
111.5
该圆为
圆心(1,1)
半径1.5
圆的周长为9.42
圆的面积为7.065
#include
usingnamespacestd;
classyuan
{
intx,y;
doubler;
public:
voidset(int,int,double);
doublec(){return2*3.14*r;}
doublearea();
};
voidyuan:
:
set(inta,intb,doublec)
{
x=a,y=b,r=c;
}
doubleyuan:
:
area()
{
return3.14*r*r;
}
intmain()
{
intx,y;
doubler;
cout<<"请输入圆心坐标和圆的半径"<cin>>x>>y>>r;
yuany1;
y1.set(x,y,r);
cout<<"该圆为"<cout<<"圆心"<<"("<cout<<"半径"<cout<<"圆的周长为"<cout<<"圆的面积为"<return0;
}
#include
usingnamespacestd;
classyuan
{
intx,y;
doubler;
public:
voidset(int,int,double);
doublecircumference();
doublearea();
};
voidyuan:
:
set(inta,intb,doublec)
{
x=a,y=b,r=c;
}
doubleyuan:
:
circumference()
{
return2*3.14*r;
}
doubleyuan:
:
area()
{
return3.14*r*r;
}
intmain()
{
intx,y;
doubler;
cout<<"请输入圆心坐标和圆的半径"<cin>>x>>y>>r;
yuany1;
y1.set(x,y,r);
cout<<"该圆为"<cout<<"圆心"<<"("<cout<<"半径"<cout<<"圆的周长为"<cout<<"圆的面积为"<return0;