版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
浙江大学远程教育学院
《面向对象程序设计》课程作业
姓名:学号:
年级:学习中心:
第2章
[2.3]测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是
否有效。
//thisisastrange/*waytodoacomment*/
答:此注释有效,单行注释中可以嵌套/*……*/方式的注释。
[2.4]以下这个简短的C++程序不可能编译通过,为什么?
#include<iostream>
usingnamespacestd;
sum(inta,intb)
intmain()
{inta,b,c;
cout«nEntertwonumbers:";
cin»a»b;
c=sum(a,b);
cout«nsumis:"«c;
return0;
)
sum(inta,intb)
{returna+b;
)
答:不可能通过编译.在usingnamespacestd;后面加上sum(inta,intb)
就可以通过。
[2.5]回答问题。
(1)以下两个函数原型是否等价:
floatfun(inta,floatb,char*c);
floatfun(int,float,char*);
(2)以下两个函数的第一行是否等价:
floatfun(inta,floatb,char*c);
floatfun(int,float,char*);
答:(1)这两个函数原型是等价的,函数原型中的参数名可以缺省。
(2)这两个函数的第1行是不等价的,因为这个函数的第1行中必须
包含参数名。
[2.6]下列语句中错误的是(D)。
A.int*p=newint(10);B.int*p=newintflO];
C.int*p=newint;D.int*p=newint[40](0);
[2.7]假设已经有定义"constchar*constname="chen";”下面的语句中
正确的是(D)。
A.name[3]=,a,;B.name二“lin”;
C.name=newchar[5];D.cout«name[3];
8]假设已经有定义“char*constname二〃cherT;”下面的语句中正确的
是(A)o
A.name[3]=,q';B.name="lin”;
C.name=newchar[5];D.name=newchar('q');
9]假设已经有定义"constchar*name=〃chen〃;”下面的语句中错误的
是(A)o
A.name[3]=,q,;B.name="linn;
C.name=newchar[5];D.name=newchar('q');
[2.10]重载函数在调用时选择的依据中,(B)是错误的。
A.函数名字B.函数的返回类型
C.参数个数D.参数的类型
[2.11]在(A)情况下适宜采用内联函数。
A.函数代码小,频繁调用B.函数代码多,频繁调用
C.函数体含有递归语句D.函数体含有循环语句
[2.12]下列描述中,(C)是错误的。
A.内联函数主要解决程序的运行效率问题
B.内联函数的定义必须出现在内联函数第一次被调用之前
C.内联函数中可以包括各种语句
D.对内联函数不可以进行异常接口声明
[2.13]在C++中,关于下列设置默认参数值的描述中,(B)是正确的。
A.不允许设置默认参数值
B.在指定了默认值的参数右边,不能出现没有指定默认值的参数
C.只能在函数的定义性声明中指定参数的默认值
D.设置默认参数值时,必须全部都设置
[2.14]下面的类型声明中正确是(D)。
A.int&a[4];B.int&*p;
C.int&&q;D.inti,*p=&i;
[2.15]下面有关重载函数的说法中正确的是(C)。
A.重载函数必须具有不同的返回值类型
B.重载函数形参个数必须不同
C.重载函数必须有不同的形参列表
D.重载函数名可以不同
[2.16]关于new运算符的下列描述中,(D)是错误的。
A,它可以用来动态创建对象和对象数组
B.使用它创建的对象或对象数组可以使用运算符delete删除
C.使用它创建对象时要调用构造函数
D.使用它创建对象数组时必须指定初始值
[2.17]关于delete运算符的下列描述中,(C)是错误的。
A.它必须用于new返回的指针
B.使用它删除对象时要调用析构函数
C.对一个指针可以使用多次该运算符
D.指针名前只有一对方括号符号,不管所删除数组的维数
[2.18]写出下列程序的运行结果。
#include<iostream>
usingnamespacestd;
inti=15;
intmain()
{inti;
i=100;
cout«::i«endl;
return0;
)
答:101
[2.19]写出下列程序的运行结果。
#include<iostream>
usingnamespacestd;
voidf(int&m,intn)
{inttemp;
temp=m;
m=n;
n=temp;
)
intmain()
{inta=5,b=10;
f(a,b);
cout«a«un«b«endl;
return0;
答:10,10
[2.20]分析下面程序的输出结果。
#include<iostream>
usingnamespacestd;
int&f(int&i)
{i+=10;
returni;
)
intmain()
{intk=0;
int&m=f(k);
cout«k«endl;
m=20;
cout«k«endl;
return0;
)
答:10
20
[2.21]举例说明可以使用const替代#define以消除祥define的不安全性。
答:
[2.22]编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci数
列的前20项,并存储到动态分配的空间中。
答:#include<iostream>
usingnamespacestd;
intmainO
int*p=newint[20];
*p=l;
*(p+l)=l;
cout«*p«w\t«*(p+l)«w\tw;
P=p+2;
for(inti=3;i<=20;i++)
(
*p=*(p-1)+*p(p-2);
Cout«*p«M\tM;
If(i%5==0)cout<<endl;
P++;
)
)
[2.23]编写一个C++风格的程序,建立一个被称为sroot()的函数,返回其参
数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度数
的二次方根(计算二次方根时,可以使用标准库函数sqrt())
答:o#include<iostream>
#include<cmath>
Usingnamespacestd;
Doublesroot(inti)
{Returnsqrt(i);}
Doublesroot(long1)
{returnsqrt(1);}
Doublesroot(doubled)
{returnsqrt(d);}
Intmain()
I
Inti=12;
Long1=1234;
Doubled=12.34;
Cout«wi的二次方根是:"〈〈sroot(i)<<endl;
Cout«w1的二次方根是:”《sroot(l)〈〈endl;
Cout<<wd的二次方根是二〈〈sroot(d)<<endl;
Return0;
)
[2.24]编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、
5分的硬币,有多少种换法?
答:#include<iostream>
Usingnamespacestd;
Intmain()
IntI,j,sum=0;
For(i=0;i<=20;i++)
For(j=0;j<=50;j++)
If(100-5*I-2*j>=0)
!
Sum++;
Cout«100-5*1-2*j<<”\t”《j<<”\tv«i«endl;
)
Cout<<wsumis^«sum<<endl;
Return0;
)
[2.25]编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序
输出。要求使用变量的引用。
答:#include<iostream>
Usingnamespacestd;
IntmainO
{voidchange(int&,int&);
Inta,b;
Cin»a>>b;
If(a>b)change(a,b);
Cout«a«v^«b«endl;
Return0;
)
Voidchange(int&al,int&bl)
{inttemp;
Temp=al;
Al=bl;
Bl=temp;}
第三章:
[3.7]在下面有关对构造函数的描述中,正确的是(B)。
A.构造函数可以带有返回值
B.构造函数的名字与类名完全相同
C.构造函数必须带有参数
D.构造函数必须定义,不能默认
[3.8]在声明类时,下面的说法正确的是(C)。
A.可以在类的声明中给数据成员赋初值
B.数据成员的数据类型可以是register
C.private、public、protected可以按任意顺序出现
D.没有用private、public、protected定义的数据成员是公有成员
[3.9]在下面有关析构函数特征的描述中,正确的是(C)。
A.一个类中可以定义多个析构函数
B.析构函数名与类名完全相同
C.析构函数不能指定返回类型
D.析构函数可以有一个或多个参数
[3.10]构造函数是在(B)时被执行的。
A.程序编译B.创建对象
C.创建类D.程序装人内存
[3.11]在下面有关静态成员函数的描述中,正确的是(B)0
A.在静态成员函数中可以使用this指针
B.在建立对象前,就可以为静态数据成员赋值
C.静态成员函数在类外定义时,要用static前缀
D.静态成员函数只能在类外定义
[3.12]在下面有关友元函数的描述中,正确的说法是(A)o
A.友元函数是独立于当前类的外部函数
B.一个友元函数不能同时定义为两个类的友元函数
C.友元函数必须在类的外部定义
D.在外部定义友元函数时,必须加关键字friend
[3.13]友元的作用之一是(A)。
A.提高程序的运行效率B.加强类的封装性
C.实现数据的隐藏性D.增加成员函数的种类
[3,14]以下程序运行的结果是(B)。
#include<iostream>
usingnamespacestd;
classB{
public:
B(){}
B(inti,intj)
{x=i;
y=j;
}
voidprintb()
{cout«x«","«y«endl;
}
private:
intx,y;
);
classA{
public:
A()
()
A(intI,intj);
voidprinta();
private:
Bc;
A::A(inti,intj):c(i,j)
()
voidA::printa()
{c.printb();
)
intmain()
{Aa(7,8);
a.printaQ;
return0;
)
A.8,9B.7,8C.5,6D.9,10
[3,15]以下程序的运行结果是(A)o
#include<iostream>
usingnamespacestd;
classA{
public:
voidset(inti,intj)
{x=i;
y=j;
)
intget_y()
{returny;
)
private:
intx,y;
);
classbox{
public:
voidset(intl,intw,ints,intp)
{length=l;
width=w;
label.set(s,p);
intget_area()
{returnlength*width;
private:
intlength,width;
Alabel;
);
intmain()
{boxb;
b.set(4,6,l,20);
cout«b.get_area()«endl;
return0;
)
A.24B.4C.20D.6
[3,16]以下程序的运行结果是(B)0
#include<iostream>
usingnamespacestd;
classSample(
public:
Sample(inti,intj)
{x=i;
y寸
)
voiddisp()
{cout«ndispl"«endl;
)
voiddisp()const
{cout«ndisp2n«endl;
private:
intx,y;
);
intmain()
{constSamplea(l,2);
a.dispO;
return0;
)
A.displB.disp2C.displdisp2D.程序编译出错
[3.17]以下程序的运行结果是(B)。
#include<iostream>
usingnamespacestd;
classR{
public:
R(intrl,intr2)
{Rl=rl;
R2=r2;
)
voidprint();
voidprint()const;
private:
intR1,R2;
);
voidR::print()
{cout«Rl«","«R2«endl;
)
voidR::print()const
{cout«Rl«n,"«R2«endl;
intmain()
{Ra(6,8);
constRb(56,88);
b.print();
return0;
)
A.6,8B.56,88C.0,0D.8,6
[3.18]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classtoy
{public:
toy(intq,intp)
{quan=q;
price=p;
)
intget_quan()
{returnquan;
)
intget_price()
{returnprice;
)
private:
intquan,price;
);
intmain()
{toyop[3][2]={0,0
toy(10,20),toy(30,48),
toy(50,68),toy(70,80),
toy(90,16),toy(l1,120),
);
for(inti=0;i<3;i++)
{cout«op[i][0].get_quan()«,,,n;
cout«op[i][0].get_price()«n\n";
cout«opfi]f1].get_quan()«'V,;
cout«op[i][1].get_price()«,,\n";
)
cout«endl;
return0;
)
答:10,20
30,48
50,68
70,80
90,16
11,120
[3.19]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classexample
{public:
example(intn)
{i=n;
cout«"Constructing\n";
)
~example()
{cout«"Destructing\n";
}
intget_i()
{returni;
private:
inti;
);
intsqr_it(exampleo)
{returno.get_i()*o.get_i();
)
intmain()
{examplex(10);
cout«x.get_i()«endl;
cout«sqr_it(x)«endl;
return0;
)
答:Constructing
10
Destructing
100
Destructing
[3.20]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classaClass
{public:
aClass()
{total++;
)
〜aClass。
{total—;
)
intgettotal()
{returntotal;
private:
staticinttotal;
);
intaClass::total=0;
intmain()
{aClassol,o2,o3;
cout«ol.gettotaI()«Hobjectsinexistence\nn;
aClass*p;
p=newaClass;
if(!p)
{cout«"Allocationerror\n";
return1;
)
cout«ol.gettotal();
cout«nobjectsinexistenceafterallocation\nn;
deletep;
cout«ol.gettotal();
cout«Mobjectsinexistenceafterdeletion\nn;
return0;
)
答:
3objectsinexistence
4objectsinexistenceafterallocation
3objectsinexistenceafterdeletiono
[3.21]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classtest
{public:
test();
~test()什;
private:
inti;
);
test::test()
{i=25;
fbr(intctr=0;ctr<10;ctr++)
{cout«,rCountingat"«ctr«"\n";
)
)
testanObject;
intmain()
{return0;
)
答:
Countingat0
Countingat1
Countingat2
Countingat3
Countingat4
Countingat5
Countingat6
Countingat7
Countingat8
Countingat9。
[3.22]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classA{
inta,b;
public:
A()
{a=0;
b=0;
cout«nDefaultconstructorcalled.\nn;
)
A(inti,intj)
{a=i;
b=j;
cout«MConstructor:a=n«a«n,b=n«b«endl;
)
);
intmain()
{Aa[3];
Ab[3]={A(l,2),A(3,4),A(5,6)};
return0;
答:
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.
Constructor:a=l,b=2
Constructor:a=3,b=4
Constructor:a=5,b=6o
[3.23]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classTest{
private:
intval;
public:
Test()
{cout«Hdefault.n«endl;
)
Test(intn)
{val=n;
cout«nCon.n«endl;
)
Test(constTest&t)
{val=t.val;
cout«"Copycon."«endl;
)
);
intmain()
{Testtl(6);
Testt2=tl;
Testt3;
t3=tl;
return0;
)
答:Con.
Copycon.
Default.
[3.24]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classN{
private:
intA;
staticintB;
public:
N(inta)
{A=a;
B+=a;
)
staticvoidfl(Nm);
);
voidN::fl(Nm)
{cout«"A=,,«m.A«endl;
cout«"B=',«B«endl;
)
intN::B=0;
intmain()
{NP(5),Q(9);
N::fl(P);
N::fl(Q);
return0;
)
答:A=5
B=14
A=9
B=14
[3.25]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classM{
intx,y;
public:
M()
{x=y=O;
M(inti,intj)
{x=i;
y=j;
}
voidcopy(M*m);
voidsetxy(inti,intj)
{x=i;
y=j;
)
voidprint()
{cout«x«","«y«endl;
}
);
voidM::copy(M*m)
{x=m->x;
y=m->y;
)
voidfun(Mml,M*m2)
{ml.setxy(12,15);
m2->setxy(22,25);
)
intmain()
{Mp(5,7),q;
q.copy(&p);
fun(p,&q);
p.print();
qprintQ;
return0;
答:5,7
22,25
[3.26]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classM{
intA;
staticintB;
public:
M(inta)
{A=a;
B+=a;
cout«nConstructingn«endl;
)
staticvoidfl(Mm);
~M()
{cout«"Destructing\n";
)
);
voidm)
{cout«"A=n«m.A«endl;
cout«nB=n«B«endl;
)
intM::B=0;
intmain()
{Mp(5),Q(10);
M::fl(p);
M::fl(Q);
return0;
答:Constructing
Constructing
A=5
B=15
Destructing
A=10
B=15
Destructing
Destructing
Destructing□
[3.27]指出下列程序中的错误,并说明为什么。
#include<iostream>
usingnamespacestd;
classStudent{
public:
voidprintStu();
private:
charname[10];
intage;
floataver;
);
intmain()
{Studentpl,p2,p3;
pl.age=30;
return0;
答:语句“p].age=30;”编译时出现错误,因为age是私有数据成员,
不能直接访问。
[3.28]指出下列程序中的错误,并说明为什么。
#include<iostream>
usingnamespacestd;
classStudent{
intsno;
intage;
voidprintStu();
voidsetSno(intd);
);
voidprintStu();
{cout«n\nSnoisn«sno«n,n;
cout«"ageis"«age«n.n«endl;
)
voidsetSno(ints)
{sno=s;
)
voidsetAge(inta)
{age=a;
)
intmain()
{Studentlin;
lin.setSno(20021);
lin.setAge(20);
lin.printStu();
)
答:这个函数是不能访问到类student中的age属性的
age不是类的成员也不能通过lin.setage。调用
[3.29]指出下列程序中的错误,并说明为什么。
#include<iostream>
usingnamespacestd;
classPoint{
public:
intx,y;
private:
Piont()
{x=l;y=2;
)
);
intmain()
{Pointcpoint;
cpoint.x=2;
return0;
答:构造函数不能是private属性的必须是public的要不然构造函数
访问不到就不能进行申明这个实例
[3.30]下面是一个计算器类的定义,请完成该类成员函数的实现。
classcounter{
public:
counter(intnumber);
voidincrement();〃给原值加1
voiddecrement();〃给原值减1
intgetvalue();//取得计数器值
intprint();〃显示计数
private:
intvalue;
);
Classcounter{
Public:
Counter(intnumber);
Voidincrement();
Voiddecrement();
Intgetvalue();
Intprint();
Private:
Intvalue;
Counter::counter(intnumber){value=number;}
Voidcounter::increment(){value++;}
Voidcounter::decrement(){value—;}
Intcounter::getvalue(){returnvalue;}
Intcounter::print()
(
Cout«9,valueis6"«value«endl;
Return0;
[3.31]根据注释语句的提示,实现类Date的成员函数。
#include<iostream>
usingnamespacestd;
classDate{
public:
voidprintDate();〃显示日期
voidsetDay(intd);〃设置日的值
voidsetMonth(intm);〃设置月的值
voidsetYear(inty);〃设置年的值
private:
intday,month,year;
intmain()
{DatetestDay;
testDay.setDay(5);
testDay.setMonth(10);
testDay.setYear(2003);
testDay.printDate();
return0;
)
答:VoidDate::printDate()
(
Cout«,,\nDateis^^«day«^^.^^;
Cout«month«,,.,,«year«endl;
)
VoidDate::setDay(intd){day=d;}
VoidDate::setMonth(intm){month=m;}
VoidDate::setYear(inty){yearly;}o
[3.32]建立类cylinder,cylinder的构造函数被传递了两个double值,分别
表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储
在一个double变量中。在类cylinder中包含一个成员函数vol,用
来显示每个cylinder对象的体积。
答:#include<iostream>
Usingnamespacestd;
Classcylinder
(
Public:
Cylinder(doublea,doubleb);
Voidvol();
Private:
Doubler.h;
Doublevolume;
)
Cylinder::cylinder(doublea,doubleb)
(
R=a;h=b;
Volume=3.141592*r*r*h;
)
Voidcylinder::vol(){cout<<,,volumeis:“«volume«,,\n”;}
Intmain()
I
Cylinderx(2.2,8.09);
x.vol();
return0;
)
[3.33]构建一个类book,其中含有两个私有数据成员qu和price,将qu初
始化为1〜5,将price初始化为qu的10倍,建立一个有5个元素的
数组对象。显示每个对象数组元素的qu*price值。
答:#include<iostream>
Usingnamespacestd;
Classbook{
Public:
Book(inta,intb)
{qu=a;price=b;}
Voidshowmoney()
{cout〈〈qu*price<<"\n";}
Private:
Intqu,price;
}
Intmain()
Bookob[5]={
Book(1,10),book(2,20),book⑶30),book(4,40),book⑸50);
For(inti=0;i<5;i++)
Ob[i].show_money();
Return0
)
[3.34]修改习题3.33,通过对象指针访问对象数组,使程序以相反的顺序显
示每个对象数组元素的qu*price值。
答:#include<iostream>
Usingnamespacestd;
Classbook{
Public:
Book(inta,intb){qu=a,price=b;}
Voidshowjnoney(){cout<〈qu*price<<“\n”;}
Private:
Intqu,price;
);
Intmain()
{bookob[5]={book(1,10),book(2,20),book(3,30),book(4,40),book(5,50)};
Book*p;
P=&ob[4];
For(inti=0;i<5;i++)
[p->showmoney();p一;}
Return0;
[3.35]构建一个类Stock,含字符数组stockcode□及整型数据成员quan、
双精度型数据成员price。构造函数含3个参数:字符数组na口及q、
Po当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成
员stockcode,第2和第3个参数分别赋给quan、price。未设置第2
和第3个参数时,quan的值为1000,price的值为8.98。成员函数
print没有形参,需使用this指针,显示对象数据成员的内容。假设
类Stock第1个对象的三个参数分别为:“600001”,3000和5.67,第
2个对象的第1个数据成员的值是“600001”,第2和第3个数据成员
的值取默认值。要求编写程序分别显示这两个对象数据成员的值。
答:private:
charstockcode[7];
intquan;
doubleprice;
public:
Stock(charna[],intq=1000,doublep=8.98){
for(inti=0;i<7;i++){
stockcode[i]=na[i];
)
quan=q;
price=p;
)
voidprint();
);
voidStock::print(){
for(inti=0;i<7;i++){
cout«this->stockcode[i];
)
cout<<"\t"«this->quan<<"\t"«this->price<<endl;
)
intmain(){
Stockstockl(“600001”,3000,5.67);
Stockstock2(z,600001z,);
stockl.print();
stock2.print();
return0;
}
[3.36]编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求
输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数
和总成绩用静态数据成员表示)。
答:#include<iostream>
#include<string>
Usingnamespacestd;
Classstudent{
Public:
Student(intn,stringna,doubled)
{no=n;deg=d;name=na;sum+=d;num++;}
Staticdoubleavg(){returnsum/num;}
Staticinttotal(){returnnum;}
VoiddispO{cout<<no<<,,u<<deg<endl;}
Private:
Intno;
Stringname;
Doubledeg;
Staticdoublesnum;
Staticintnum;
);
Doublestudent::sum=O;
Intstudent::num=0;
Intmain()
Student
si(1001,Zhou”,97),s2(1002,“zhan”,65),s3(1001,wchen”,88);
Cout«"学号姓名成绩\n”;
SI.dispO;
S2.dispO;
S3.dispO;
Cout<<w学生人数二”〈〈Student::total()<<endl;
Cout<<”平均成绩=”〈〈Student::avg();
Return0;
第四章:
[4.8]使用派中类的主要原因是(A)o
A.提高代码的可重用性
B.提高程序的运行效率
C.加强类的封装性
D.实现数据的隐藏
[4.9]假设已经定义好了一个类student,现在要定义类derived,它是从
student私有派生的,定义类derived的正确写法是(C)。
A.clasederived::studentprivate{,•,};
B.clasederived::studentpublic{,,,};
C.clasederived::privatestudent{•,,};
D.clasederived::publicstudent{•,,};
[4.10]在多继承构造函数定义中,几个基类构造函数用(C)分隔。
A.:B.;C.,D.::
[4.11]设置虚基类的目的是(B)o
A.简化程序B.消除二义性C.提高运行效率D.减少目标代码
[4.12]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classB1{
public:
Bl(inti)
{bl=i;
cout«"ConstructorBl."«endl;
}
voidPrint()
{cout«bl«endl;
)
private:
intbl;
classB2{
public:
B2(inti)
{b2=i;
cout«*'ConstructorB2."«endl;
)
voidPrint()
{cout«b2«endl;
)
private:
intb2;
);
classA:publicB2,publicBl{
public:
A(inti,intj,int1);
voidPrint();
private:
inta;
);
A::A(inti,intj,intl):Bl(i),B2(j)
{a=l;
cout«"ConstructorA.n«endl;
)
voidA::Print()
{Bl::Print();
B2::Print();cout«a«endl;
intmain()
{Aaa(3,2,l);
aa.Print();
return0;
)
答:ConstructorB2.
ConstructorBl.
ConstructorA.
3
2
lo
[4.13]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classMain{
protected:
char*mainfood;
public:
Main(char*name)
{mainfbod=name;
)
);
classSub{
protected:
char*subfood;
public:
Sub(char*name)
{subfood=name;
)
);
classMenu:publicMain,publicSub{
public:
Menu(char*m,char*s):Main(m),Sub(s)
()
voidshow();
);
voidMenu::show()
{cout«ni#=n«mainfood«endl;
cout«"§|J#=|,«subfood«endl;
)
intmainQ
{Menum(nbread',,,,steak");
m.show();
return0;
}、
答:主食二bread
副食二steak。
[4.14]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classA{
private:
inta;
public:
A()
{a=0;}
A(inti)
{a=i;}
voidPrint()
{cout«a«n,";
classB:publicA{
private:
intbl,b2;
public:
B()
{bl=0;b2=0;
)
B(inti)
{bl=i;b2=0;
)
B(inti,intj,intk):A(i),bl(j),b2(k)
{}
voidPrint()
{A::Print();
cout«bl«","«b2«endl;
}
);
intmain()
{Bobl,ob2(l),ob3(3,6,9);
obl.PrintO;
ob2.Print();
ob3.Print();
return0;
)
答:0,0,0
0,1,0
3,6,9o
[4.15]写出下面程序的运行结果。
#include<iostream>
usingnamespacestd;
classBl{
intbl;
public:
Bl(inti)
{bl=i;
cout«MconstructorBl.H«i«endl;
)
voidprint()
{cout«bl«endl;
)
);
classB2{
intb2;
public:
B2(inti)
{b2=i;
cout«nconstructorB2.n«i«endl;
)
voidprint()
{cout«b2«endl;
)
);
classB3{
intb3;
public:
B3(inti)
{b3=i;
cout«"constructorB3.n«i«endl;
intgetb3()
{returnb3;
}
);
classA:publicB2,publicBl{
inta;B3bb;
public:
A(inti,intj,intk,intl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026山东铁路投资控股集团有限公司招聘45人备考题库附答案详解(培优)
- 2026贵州黔东南州黎平县洪州镇卫生院招聘编外工作人员4人备考题库附答案详解(能力提升)
- 2026湖南株洲市图书馆见习岗位招聘4人备考题库含答案详解(轻巧夺冠)
- 2026江苏师范大学招聘体育教师4人备考题库附答案详解(基础题)
- 2026浙江省商业集团有限公司招聘4人备考题库(第3期)含答案详解(培优a卷)
- 2026湖南岳阳市屈原管理区事业单位“四海揽才”招聘11人备考题库及一套完整答案详解
- 2026湖北随州技师学院招聘教师12人备考题库附答案详解(夺分金卷)
- 2026内蒙古阿拉善盟事业单位招聘工作人员暨“智汇驼乡·鸿雁归巢”143人备考题库含答案详解(巩固)
- 2026江铜集团德兴铜矿招聘22人备考题库及参考答案详解1套
- 2026陆军工程大学社会用工招聘7人备考题库及答案详解一套
- 2026杭州市钱塘(新)区紧缺岗位人才招聘14人考试备考题库及答案解析
- 腰椎病中医护理推拿手法
- 国家事业单位招聘2024国家基础地理信息中心招聘应届毕业生人员笔试历年参考题库典型考点附带答案详解
- 2025年中国南水北调集团江汉水网建设开发有限公司公开招聘15人笔试参考题库附带答案详解
- 2026年及未来5年中国蔬菜的净菜加工行业发展监测及投资战略规划建议报告
- (2026年)咯血的护理课件
- 社区三资工作方案
- CT增强扫描技术规范
- 2025湖北汉江水利水电(集团)有限责任公司水电公司面向社会招聘员工拟录用人选笔试历年参考题库附带答案详解
- 雨课堂学堂在线学堂云《家庭教育学(青岛大学 )》单元测试考核答案
- NCCN急性淋巴细胞白血病临床实践指南解读(2025版)
评论
0/150
提交评论