类和对象练习题Word文档格式.docx
《类和对象练习题Word文档格式.docx》由会员分享,可在线阅读,更多相关《类和对象练习题Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
C).类的其他函数可以调用类的静态成员函数
D).类的静态数据成员被类的所有对象共享
14.以下关于类和对象叙述正确的是___。
A).一个类只能有一个对象
B).一个对象可以属于几个不同的类
C)对象是类的一个具体实例(实体)
D)一个类中可以有本类的对象
18.________将在对象生存期结束时由系统自动调用。
A)构造函数B)成员变量函数C)析构函数D)虚函数
19.下面关于友员函数的说法中,正确的是________。
A)友员函数是本类的一个成员函数
B)友员函数可以通过对象访问本类的所有成员
C)友员函数有this指针
D)友员函数可以直接访问本类的所有成员
20.假定AB为一个类名,则执行ABa(4),b[5],*p;
语句时,自动调用该类构造函数的次数为。
A)5B)6C)7D)9
二.阅读程序,写出以下程序运行结果。
1.#include<
iostream.h>
lassmyclass
{inta,b;
public:
myclass(inti=0,intj=0);
voidshow();
};
myclass:
:
myclass(inti,intj)
{a=i;
b=j;
}
voidmyclass:
show()
{a+=b;
b+=a;
cout<
<
”a=”<
a<
"
"
”b=”<
b<
endl;
voidmain()
{myclassob1;
ob1.show();
myclassob2
(2);
ob2,show();
myclassob3(2,5);
ob3.show();
运行结果:
2.
#include<
classcube
{public:
cube(intht=2,intwd=5,intdp=3)
{height=ht;
width=wd;
depth=dp;
″depth=″<
depth<
″construnctedcalled″<
}
~cube(){cout<
″Destructedcalled″<
intvolume(){returnheight*width*depth;
private:
intheight,width,depth;
};
voidmain()
{cubecone(10,20,15),ctwo;
cone.volume()<
ctwo.volume()<
运行结果:
3.写出以下程序运行结果。
classA
public:
A(){}
A(inti,intj):
a(i),b(j)
{cout<
constructor\n"
;
~A(){cout<
distructor\n"
{Aob2(2,5);
Aob1(ob2);
4.当输入3、4和输入10、20时,写出以下程序输出结果。
classTC
{private:
doublelead1,lead2,total;
intp1,p2;
TC(){p1=15;
p2=20;
}
TC(intx,inty){p1=x;
p2=y;
voiddatap()
″输入2个整数:
″;
cin>
>
lead1>
lead2;
total=p1*lead1+p2*lead2;
voiddisplay(){cout<
″total=″<
total<
{TCA,B(6,8);
A.datap();
A.display();
5.写出以下程序运行结果。
#include<
classvalue{
inta;
value(){a=1;
cout<
”Default\n”;
value(inti){a=i;
”constructure\n”;
voidlist(){cout<
”“;
~value(){cout<
”Destructure\n”;
voidmain()
{valuedata1[4];
valuedata2[4]={10,20,30};
for(inti=0;
i<
4;
i++)data1[i].list();
for(i=0;
i++)data2[i].list();
6.写出以下程序运行结果。
A(inti,intj){a=i;
″constructor.\n″;
~A(){cout<
″Destructor″<
inta,b;
{A*pa,*pb;
pa=newA(4,6);
pb=newA(10,15);
deletepa;
deletepb;
7.写出以下程序运行结果。
classcount
count(){count1++;
staticintdata(){returncount1;
~count(){count1--;
staticintcount1;
intcount:
count1=60;
{counta,b,c,d,e;
count:
data()<
a.data()<
e.data()<
8.写出以下程序运行结果。
classmyclass
intx,y;
staticlongsum;
myclass(inta,intb){x=a;
y=b;
voidgetxy()
{sum+=x*y;
″sum=″<
sum<
longmyclass:
sum=1;
{myclassob1(1,2);
ob1·
getxy();
myclassob2(3,4);
ob2·
myclassob3(5,6);
ob3·
9.写出以下程序运行结果。
string.h>
A(char*str);
~A();
charstring[50];
A:
A(char*str)
{strcpy(string,str);
constructorcalledfor"
string<
~A()
destructorcalledfor"
voidfun()
{Afunobject("
Fun对象"
);
staticAstaticobject("
static对象"
Infun()"
AGlobalobject("
G对象"
//全局对象
{AMainobject("
Main对象"
InMain(),beforecallingfun\n"
fun();
InMain(),aftercallingfun\n"
10.写出以下程序运行结果。
classsample
{intx,y;
sample(){x=0;
y=0;
sample(inti,intj){x=i;
y=j;
voidcopy(sample&
A){x=A.x;
y=A.y;
voiddisplay()
″x=″<
x<
endl<
″y=″<
y<
{sampleob1,ob2(1,2);
ob1.copy(ob2);
ob1.display();
ob2.display();
11.写出以下程序运行结果。
stdlib.h>
classstringclass
stringclass(char*str);
~stringclass();
voidshowstring();
char*ptr;
longlen;
stringclass:
stringclass(char*str)
{len=strlen(str);
ptr=newchar[len+1];
if(!
ptr){cout<
″Allocutionerror\n″;
exit
(1);
strcpy(ptr,str);
~stringclass()
″FreeingAllocution\n″;
delete[]ptr;
voidstringclass:
showstring()
ptr<
″……″<
len<
{stringclasss1(″C++programming″);
stringclasss2(″Teststring″);
s1·
showstring();
s2·
二.程序填空
1.编写一个求n!
的类,分别输出2~9的阶乘。
classFACT{
intn;
longintfact;
FACT(int);
voiddisplay(){cout<
n<
″!
=″<
____<
}//输出阶乘
FACT:
FACT(intval)
{n=val;
fact=1;
while(val>
1){fact*=__;
val--;
{for(inti=2;
10;
i++)
{FACTp(i);
p.display();
2.以下程序能根据对象提供的长方形的长和宽得到该长方形的面积和周长。
classrectangle
floatledge,sedge;
rectangle(){ledge=0;
sedge=0;
rectangle(floata,floatb){ledge=a;
sedge=b;
voidarea(){cout<
”面积:
”<
____<
voidlength(){cout<
”周长:
_____<
{rectangleA(3.5,2.0),B(4.2,2.0);
A.area();
A.length();
B.area();
B.length();
3.填空完成以下程序,使该程序的执行结果为10。
classMyClass{
intx;
{x=n;
}
intGetNum(){;
voidmain(){
MyClassmy(10);
my.GetNum()<
4.以下程序能实现求a2+b2+c2。
其中a、b、c分别为5、10、15由不同对象的初始化值提供。
该程序使用静态成员实现。
classmyclass{
myclass(intx);
voidgetnumber();
staticvoidResult();
inta;
____;
//定义静态数据成员sum
myclass:
myclass(intx){
a=x;
sum+=a*a;
voidmyclass:
getnumber()
″Number=″<
Result()
″Result=″<
_____;
//static数据成员初始化
voidmain(){
myclassob1(5),ob2(10),ob3(15);
ob1.getnumber();
ob2.getnumber();
ob3.getnumber();
____;
//调用Result输出结果
5.以下程序能求出n个不同字符串的总长度。
程序中定义一个类,其中数据成员length记录每一个字符串的长度,静态数据成员total-length记录所有字符串的总长度,用静态成员函数求总长度。
classstring
string(char*s);
staticintget_total_length(stringa);
~string(){}
voidshow();
intlength;
staticinttotal_length;
charstr[80];
string:
string(char*s)//构造函数实现
{length=strlen(s);
strcpy(str,s);
intstring:
get_total_length(stringa)
{total_length+=a.length;
returntotal_length;
_________;
voidstring:
show()
str<
″″<
″length=″<
length<
{stringob1(“Thefirstabcde”);
ob1.show();
″Total=″<
get_total_length(obl)<
stringob2(“Thesecondfghijklmn”);
ob2.show();
″Totallength=″<
get_total_length(ob2)<
stringob3(″programend!
″);
ob3.show();
″Total_length=″<
get_total_length(ob3)<
Thefirstabcdelength=15
Total=15
Thesecondfghijklmnlength=20
Totallength=35
programend!
length=12
Total_length=47
6.某单位经销的货物,购进和卖出以重量为单位,以下程序记下目前库存货物的总重量(total_weight),在横线填空并写出运行结果.
classgoods
staticinttotal_weight;
intweight;
goods(intw)
{weight=w;
total_weight+=w;
goods(goods&
gd)
{weight=gd.weight;
total_weight+=weight;
~goods()
{total_weight-=weight;
total_weight<
intget(){returnweight;
staticintgettotal(){returntotal_weight;
______________;
{intw;
”输入第一种货物重量:
”;
cin>
w;
goodsg1(w);
”第一种货物重”<
g1.get()<
”输入第二种货物重量:
goodsg2(w);
”第二种货物重”<
g2.get()<
”货物总重量是:
””;
goods:
gettotal()<
输入第一种货物重量:
50
第一种货物重50
输入第二种货物重量:
68
第二种货物重68
货物总重量是:
118
7.以下程序比较classone和classtwo两个类中数据成员的大小。
classclasstwo;
//向前引用
classclassone
{intvalue1;
classone(inta){value1=a;
friendintfunction(classoneob1,classtwoob2);
classclasstwo
{intvalue2;
classtwo(intb){value2=b;
intfunction(classoneob1,classtwoob2)
{if(_________>
ob2.value2)
return1;
elseif(______<
return-1;
elsereturn0;
{classoneob1(40);
classtwoob2(15);
if(function(ob1,ob2)==1)cout<
″ob1.value>
ob2.value\n″;
elseif(function(ob1,ob2)==-1)cout<
″ob1.value<
elsecout<
″ob1.value=ob2.value\n″;
8.以下程序求两点之间的距离
math.h>
classpoint
{public:
point(doublea,doubleb){x=a;
y=b;
doublesetx(){returnx;
doublesety(){returny;
frienddoubledist(point&
d1,poin