C++复习资料1_第1页
C++复习资料1_第2页
C++复习资料1_第3页
C++复习资料1_第4页
C++复习资料1_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.1 Fill in the blanks in each of the following:a. The company that popularized personal computing was _ Apple._b. The computer that made personal computing legitimate in business andindustry was the _IBM Personal Computerc. Computers process data under the control of sets of instructions calledcomputer _programs_.d. The six key logical units of the computer are the _input unit_, _output unit,_memory unit_, _arithmetic and logic unit_, _central processing unit_ and the _secondary storage unit_e. The three classes of languages discussed in the chapter are _machine languages_,_assembly languages_, and _high-level languages_.f. The programs that translate high-level language programs into machinelanguage are called _compilers_.g. C is widely known as the development language of the _UNIX_ operatingsystem.h. The _Pascal_ language was developed by Wirth for teaching structuredprogramming.i. The Department of Defense developed the Ada language with a capabilitycalled _multitasking._, which allows programmers to specify that many activities canproceed in parallel.1.2 Fill in the blanks in each of the following sentences about the C+ environment.a. C+ programs are normally typed into a computer using a(n) _editor._program.b. In a C+ system, a(n) _preprocessor_ program executes before the compilerstranslation phase begins.c. The _linker_ program combines the output of the compiler with variouslibrary functions to produce an executable image.d. The _loader._ program transfers the executable image of a C+ program fromdisk to memory.1.3 Fill in the blanks in each of the following statements a. Objects have the property of _information hiding _ although objects may know how tocommunicate with one another across well-defined interfaces, they normallyare not allowed to know how other objects are implemented.b. C+ programmers concentrate on creating _classes_, which contain datamembers and the member functions that manipulate those data members andprovide services to clients.c. Classes can have relationships with other classes. These relationships are called_associations_.d. The process of analyzing and designing a system from an object-oriented pointof view is called _object-oriented analysis and design (OOAD)._.e. OOD also takes advantage of _inheritance_ relationships, where new classes ofobjects are derived by absorbing characteristics of existing classes, then addingunique characteristics of their own.f. _The Unified Modeling Language (UML)_ is a graphical language that allows people who design softwaresystems to use an industry-standard notation to represent them.g. The size, shape, color and weight of an object are considered _attributes_ of theobject.1.7 Why is so much attention today focused on object-oriented programming in general and C+ in particular?ANS1:Object-oriented programming enables the programmer to build reusablesoftwarecomponents that model items in the real world. Building software quickly, correctly,andeconomically has been an elusive goal in the software industry. The modular, object-orienteddesign and implementation approach has been found to increase productivity while reducingdevelopment time, errors, and cost.ANS2: In object-oriented programming, the source code is organized in classes and objects, whichis easy to be mapping into the items in physical world. It is easy to design, reuse and maintainsuch kind of software in large scale software development, which is favored by the softwareindustry.【参考教材的答案】面向对象的编程让编程者可以建立可重用的软件组件,这些组件是现实世界事物的抽象模型。软件工业的目标是快速、正确、有效率的开发软件。面向对象的设计和实现方法模型已经被事实证明是可以提高生产效率和减少开发时间、错误和代价。【参考讲义的答案】面向对象的编程方法中,以类和对象来组织源代码,可以方便的实现与现实物理世界事物的映射。采用这种方法,易于在大型软件开发中进行软件的设计、重用和维护,因此受到软件行业的支持和采用。1.10 You are probably wearing on your wrist one of the worlds most common types of objectsa watch. Discuss how each of the following terms and concepts applies to the notion of a watch: object, attributes, behaviors, class, inheritance (consider, forexample, an alarm clock), abstraction, modeling, messages, encapsulation, interface,information hiding, data members and member functions.1.10 可能现在你的手腕上就戴着世界上最普遍的对象类型之一手表。论述以下这些术语和概念如何应用于手表这个概念上:对象、属性、行为、类、继承(例如,考虑闹钟)、封装、接口、信息隐藏、数据成员和成员函数。ANS:The entire watch is an object that is composed of many other objects (such as the moving parts, the band, the face, etc.)Watch attributes are time, color, band, style (digital or analog), etc. The behaviors of the watch include setting the time and getting the time, A watch can be considered a specific type of clock(as can an alarm clock), With that in mind, it is possible that a class called Clock could exist from which other classes such as watch and alarm clock can inherit the basic features in the clock. The watch is an abstraction of the mechanics needed to keep track of the time. The user of the watch does not need to know the mechanics of the watch in order to use it; the user only needs to know that the watch keeps the proper time. In this sense,the mechanics of the watch are encapsulated(hidden)inside the watch. The interface to the watch(its face and controls for setting the time) allows the user to set and get the time. The user is not allowed to directly touch the internal mechanics of the watch. All interaction with the internal mechanics is controlled by the interface to the watch. The data members stored in the watch are hidden inside the watch and the member functions(looking at the face to get the time and setting the time) provide the interface to the data.2.1 Fill in the blanks in each of the following.a. Every C+ program begins execution at the function _main_.b. The _left brace ()_ begins the body of every function and the _ right brace ()_ ends the body of every function.c. Every C+ statement ends with a(n) _ semicolon_.d. The escape sequence n represents the _ newline._ character, which causes the cursor to position to the beginning of the next line on the screen.e. The _ if._ statement is used to make decisions.2.2 State whether each of the following is true or false. If false, explain why. Assume the statement using std:cout; is used.a. Comments cause the computer to print the text after the / on the screen when theprogram is executed.b. The escape sequence n, when output with cout and the stream insertion operator,causes the cursor to position to the beginning of the next line on the screen.c. All variables must be declared before they are used.d. All variables must be given a type when they are declared.e. C+ considers the variables number and NuMbEr to be identical.f. Declarations can appear almost anywhere in the body of a C+ function.g. The modulus operator (%) can be used only with integer operands.h. The arithmetic operators *, /, %, + and all have the same level of precedence.i. A C+ program that prints three lines of output must contain three statements usingcout and the stream insertion operator.ANS: a. False. Comments do not cause any action to be performed when the program is executed. They are used to document programs and improve their readability.b. True. c. True. d. True. e. False. C+ is case sensitive, so these variables are unique. f. True. g. True. h. False. The operators *, / and % have the same precedence, and the operators + and have a lower precedence. i. False. A single cout statement with multiple n escape sequences can print several lines2.3 Write a single C+ statement to accomplish each of the following (assume that using declarations have not been used):a. Declare the variables c, thisIsAVariable, q76354 and number to be of type int.b. Prompt the user to enter an integer. End your prompting message with a colon (:)followed by a space and leave the cursor positioned after the space.c. Read an integer from the user at the keyboard and store the value entered in integervariable age.d. If the variable number is not equal to 7, print The variable number is not equal to 7.e. Print the message This is a C+ program on one line.f. Print the message This is a C+ program on two lines. End the first line withC+.g. Print the message This is a C+ program with each word on a separate line.h. Print the message This is a C+ program with each word separated from thenext by a tab.ANS; a. int c, thisIsAVariable, q76354, number;b. std:cout age;d. if ( number != 7 )std:cout The variable number is not equal to 7n;e. std:cout This is a C+ programn;f. std:cout This is a C+nprogramn;g. std:cout ThisnisnanC+nprogramn;h. std:cout ThististatC+tprogramn;2.4 Write a statement (or comment) to accomplish each of the following (assume that using declarations have been used):a. State that a program calculates the product of three integers.b. Declare the variables x, y, z and result to be of type int (in separate statements).c. Prompt the user to enter three integers.d. Read three integers from the keyboard and store them in the variables x, y and z.e. Compute the product of the three integers contained in variables x, y and z, andassign the result to the variable result.f. Print The product is followed by the value of the variable result.g. Return a value from main indicating that the program terminated successfully.ANS: a. / Calculate the product of three integersb. int x;int y;int z;int result;c. cout x y z;e. result = x * y * z;f. cout The product is result endl;g. return 0;2.5 Using the statements you wrote in Exercise 2.4, write a complete program that calculates and displays the product of three integers. Add comments to the code where appropriate. Note: You will need to write the necessary using declarations.ANS: 1 / Calculate the product of three integers2 #include / allows program to perform input and output34 using std:cout; / program uses cout5 using std:cin; / program uses cin6 using std:endl; / program uses endl78 / function main begins program execution9 int main()10 11 int x; / first integer to multiply12 int y; / second integer to multiply13 int z; / third integer to multiply14 int result; / the product of the three integers1516 cout x y z; / read three integers from user18 result = x * y * z; / multiply the three integers; store result19 cout The product is result endl; / print result; end line2021 return 0; / indicate program executed successfully22 / end function main2.6 Identify and correct the errors in each of the following statements (assume that thestatement using std:cout; is used):a. if ( c 7 );cout 7 )cout .Correction: Change = to =, and you may want to change equal to or greater than togreater than or equal to as well.Question2.8Fill in the blanks in each of the following:a) _ are used to document a program and improve its readability.ANS: Commentsb) The object used to print information on the screen is _.ANS: coutc) A C+ statement that makes a decision is _.ANS: ifd) Most calculations are normally performed by _ statements.ANS: assignmente) The _ object inputs values from the keyboard.ANS: cinQuestion2.10State which of the following are true and which are false. If false, explain your answers.a) C+ operators are evaluated from left to right.ANS: False. Some operators are evaluated from left to right, while other operators are evaluated right to left.b) The following are all valid variable names: _under_bar_, m928134, t5, j7,her_sales,his_account_total, a, b, c, z, z2.ANS: True.c) The statement cout a = 5; is a typical example of an assignment statement.ANS: False. The statement is an output statement. The text a = 5; is output to the screen.d) A valid C+ arithmetic expression with no parentheses is evaluated from left to right.ANS: False. Arithmetic operators can appear in any order in an expression, so a counter example is a=b+c*d; whose operators are actually evaluated from right to left because of the rules of operator precedence.e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.ANS: False. h22 is a valid variable name.Question2.12What, if anything, prints when each of the following C+ statements is performed? If nothing prints, then answer “nothing.” Assume x = 2 and y = 3.a) cout x;ANS: 2b) cout x + x;ANS: 4c) cout x=;ANS: x=d) cout x = x;ANS: x = 2e) cout x + y = x y;ANS: nothing.h) / cout x + y = x + y;ANS: nothing (because it is a comment).i) cout n;ANS: A newline is output which positions the cursor at the beginning of the next line on the screen.Question2.15State the order of evaluation of the operators in each of the following C+ statements and show the value of x after each statement is performed.a) x = 7 + 3 * 6 / 2 - 1;ANS: *, /, +, -, =, 15b) x = 2 % 2 + 2 * 2 - 2 / 2;ANS: %, *, /, +, -, =, 3c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );ANS: innermost parentheses around 3, *, /, +, *, *, 324Quetion3.1Fill in the blanks in each of the following:a) A house is to a blueprint as a(n) _ is to a class.ANS:objectb) Every class definition contains keyword _ followed immediately by the classs name.ANS:classc) A class definition is typically stored in a file with the _ filename extension.ANS:.hd) Each parameter in a function header should specify both a(n) _ and a(n) _.ANS:type, namee) When each object of a class maintains its own copy of an attribute, the variable thatrepresents the attribute is also known as a(n) _.ANS:data memberf) Keyword public is a(n) _.ANS:access specifierg) Return type _ indicates that a function will perform a task but will not return any information when it completes its task.ANS:voidh) Function _ from the library reads characters until a newline character is encountered, then copies those characters into the specified string.ANS:getlinei) When a member function is defined outside the class definition, the function header must include the class name and the _, followed by the function name to tie the member function to the class definition.ANS:binary scope resolution operator (:)j) The source-code file and any other files that use a class can include the classs header file via an_ preprocessor directive.ANS:#include3.2 State whether each of the following is true or false. If false, explain why.a. By convention, function names begin with a capital letter and all subsequentwords in the name begin with a capital letter.b. Empty parentheses following a function name in a function prototype indicatethat the function does not require any parameters to perform its task.c. Data members or member functions declared with access specifier private areaccessible to member functions of the class in which they are declared.d. Variables declared in the body of a particular member function are known asdata members and can be used in all member functions of the class.e. Every functions body is delimited by left and right braces ( and ).f. Any source-code file that contains int main() can be used to execute aprogram.g. The types of arguments in a function call must match the types of thecorresponding parameters in the function prototypes parameter list.ANS: a) False. By convention, function names begin with a lowercase letter and allsubsequent words in the name begin with a capital letter. b) True. c) True. d) False.Such variables are called local variables and can be used only in the member functionin which they are declared. e) True. f) True. g) True.3.3 What is the difference between a local variable and a data member?ANS: A local variable is declared in the body of a function and can be used only from the point at which it is declared to the immediately following closing brace. A datamember is declared in a class definition, but not in the body of any of the classsmember functions. Every object (instance) of a class has a separate copy of the classsdata members. Also, data members are accessible to all member functions of theclass.3.4 Explain the purpose of a function parameter. What is the difference between aparameter and an argument?ANS: A parameter represents additional information that a function requires to perform its task. Each parameter required by a function is specified in the function header. An argument is the value supplied in the function call. When the function is called, the argument value is passed into the function parameter so that the function can perform its task.Question3.5Explain the difference between a function prototype and a function definition.ANS: A function prototype tells the compiler the name of a function and the type of data returned by the function. A prototype also describes any additional data required by the function to perfor

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论