2023年面向对象程序设计考前复习题及答案.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2023年面向对象程序设计考前复习题及答案.docx》由会员分享,可在线阅读,更多相关《2023年面向对象程序设计考前复习题及答案.docx(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、简答题1、传统的结构化程序设计的优点和缺陷是什么?优点:(1)这种程序设计方法力求算法描述准确。(2)对每一子过程模块容易进行程序对的性证明。缺陷:(1)这种程序设计方法本质上是面向“过程”的,而“过程”和“操作”又是不稔定和多变的,因 此不能直接反映人类求解问题的思绪。(2)程序代码可重用性差。程序中除少数标准库函数外,每设计个程序时,程序员几乎从 零做起。即使重用代码,通常也是通过拷贝或编辑重新生成一份。(3)维护程序的一致性困难。该种方法将数据与对数据进行解决的程序代码分离。2、什么是对象?每个对象都具有属性(At t r i b ute)和方法(Me t h。d)这两方面的特性。对象的
2、属性描 述了对象的状态和特性,对象的方法说明了对象的行为和功能,并且对象的属性值只应由这个 对象的方法来读取和修改,两者结合在一起就构成了对象的完整描述。3、什么是消息?在面向对象的程序设计中,由于对象描述了客观实体,它们之间的联系通过对象间的联 系来反映。当个对象需要此外一个对象提供服务时,它向对方发出个服务请求,而收到 请求的对象会响应这个请求并完毕指定的服务。这种向对象发出的服务请求就称为消息。 4、什么是多态性?所谓多态性是指当程序中的其他部分发出同样的消息时,按照接受消息对象的不同可以 自动执行类中相应的方法。其好处是,用户不必知道某个对象所属的类就可以执行多态行为, 从而为程序设计
3、带来更大方便。5、什么是面向对象的程序设计方法?这种方法将设计目的从模拟现实世界的行为转向了模拟现实世界中存在的对象及其各 自的行为。ca 1 lcde n dl; b 2 =v a 12;cout b a se2 is);class D: pu b lie Bl,public B2 p r o te c ted:i n t d ;pu b lie:D( i nt val 1 , i n t v a 1 2 , i n t val 3 );D: :D(in t vail, in t va12 int v a 13) : B 1 (v a 11), B2(val 2 ) d =val3;c o
4、u tderi v ed class i s c a lied ” ;voi d main() D do bj(l, 2,3) ; 3 . # include # in c 1 u d e cla s s marriedprivat e :int numbe r;c h ar * n ame?p u b 1 ic:static int glob?void set _mes( c h ar *a);voi d married : set _mes ( c har * a)name = newc h arfst r len( a )+ 1 ;st r c p y (n a me, a);。numb
5、er= + 4-glob?co u t 编号:vVn u mbere n dl;)i n t m a rri e d : g I o b= 0 ;void m a i n () marr i ed per son” 00;int i : gchar s t r 8 coutend 1 ;for ( i= 0 ; i3; i +)g c ou t 输入姓名:cin spersoni. s e t_me s (str);c o utend 1 ;)假定输入zha ng/w a ngZ1 i /请写出程序的结果。4.# include tempi a tc c lass A BCprivate:T
6、array I;。p u blic:void set (in t x in t i ;for (i=0; i I ; i+) ar r ay i=x+i;)v o id ge t ()。 cout n 数组元素总数为:Vv I end 1 ;c oul ar r ay I-1 = a r r a yI- 1 endl; 1;void ma i n() ABC abcl ;ab c 1 .s e t( 0 );abcl. get();ABC abc2;abc2.s e t(10);a b c 2. g et ();程序运营结果:5 . #in c lu d e class Stude n t p
7、ublic:S t u d en t () cou t *,c o n s t ruct st u dent. n;s e mes h ours=l()();gpa= 3 .5;p ro t e c te d :int semeshour s ;f 1 oat g P a;);c la s s Tea c h e r pu b 1 ic:T e a c h er() cout,co n s t rue tTeacher.Vn,;);cla s s To u r p ai rpu b li c :Tour p a i r ()coutvv“cons t ruct t ourpair.n;n om
8、e e t i n g=();pro tecte d :S tude n t st u dent;T eacher t e a c her;i n t n o me e t i ng;);void ma i n() T ourpa i r t p ;cou t Vback in mai n .n”;cl a s s Mycla s s priv a te:i nt x;ini y;pu b lie:Myc 1 ass(int a, i n t b) x =a;y=b;)v oid pr i nt();void myfunc();;voi d Myc 1 a s s:pr i nt() cou
9、t x= * x ,y=, y en d I; v o id Mycla s s:myfun c () i n t x=9,y= 1 0;c o u t nIn myfu n c: x=nx * , y =,yen d 1: cout My c 1 ass : : x = V My c lass:x Mycla s s: : y =uMy c 1 ass: y end 1 ;)v oid main() M y cl a ss t e st( 10 0,2 00);test.print () ;/通过对象名访问公有成员t e st. myfunc ();# i nclude c lass A p
10、 r iv a t e:in t a;pub 1 ic:A(i n t i=0) a = i ; void print(););vo i d A : :p r i n t () c o ut ”In c 1 a ss A, p r i n t() is c all e d. M coutV用友元函数求各和:a. x + b .xendl;)void sum 1(X 1 &a,X 1 & b)cout”用普通函数调用类公共接口函数求和:” a . ge t x() +b.ge t x()en d 1;)void m a in ()XI m(l);XI n(2 );s u m(m,n);s u m
11、 1 (m, n);)答案:1 .执行结果如下:构造新同学:No name构造新同学:z hang构造 c o p y of zhang构造指导教师析构 cop y o f zhan g析购zha n g析构N o name2 .该程序的执行结果是:bascl i s c a 11c dbase2 i s c ailedder i ved c las s i s c a lied3 .程序的结果:Z h ang编号1Wa n g编号2L i编号34 .程序的结果:数组元素总数为:50a r ray 49 =49数组元素总数为:1 0 0arr a y 9 9 = 109.其执行结果是:c o
12、nslru c t student.cons truct t e ache r .const r uct t o u r pa i r.back i n in a in.5 .程序的运营结果为:x = 1 00, y = 2 00I n m y f unc: x=9, y =10Myclass:x=10 0 ,Myclass: y =200.程序的运营结果为:I n c 1 a ss A, p ri n t() is c a lied.In c la s s A, print() i s called.6 .程序的运营结果为:用友元函数求各和:3用普通函数调用类公共接口函数求和:3六、编程题1
13、 .定义类模板ABC,内含成员函数s et fl g e to用ABC生成对象abcl和a bc2o它们的数组元素数不同,显示的结果也不同。# i n c lude /定义类模板ABCtempla t e c la s s ABC priva t e :T a rray f I 1产定义数组arraypub 1 ic:v o id set (i n t x )。/ / 定义成员函数 set i nt i;for (i=0; i I ; i + + )。循环 I 次ar r ayf i =x+i;/数组元素赋值V oi d get ()。定义成员函数get cou t n n数组元素总数为:Vv
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2023 面向 对象 程序设计 考前 复习题 答案
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内