C++ Syntax and Semantics, and the Program Development Process

C++ Syntax and Semantics, and the Program Development Process

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION C++ Syntax and Semantics, and 2 the Program Development Process © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION To understand how a C++ program is composed of one or more subprograms (functions). © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLCKNOWLEDGE GOALS NOT FOR SALE ORTo DISTRIBUTION know what a metalanguage is, and how it is used. NOT FOR SALE OR DISTRIBUTION To understand the concept of a data type. To learn the steps involved in entering and running a program. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTTo FOR be able SALE to: OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Read syntax templates so as to understand the formal rules governing C++ programs. Create and recognize legal C++ identifiers. Declare named constants and variables of type char and string. SKILL GOALS © Jones & Bartlett Learning,Distinguish reserved LLC words in C++ from user-defined identifiers.© Jones & Bartlett Learning, LLC NOT FOR SALE ORAssign DISTRIBUTION values to variables. NOT FOR SALE OR DISTRIBUTION Construct simple string expressions made up of constants, variables, and the concatenation operator. Construct a statement that writes to an output stream. Determine what is printed by a given output statement. Use comments© Jonesto clarify your & programs. Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Construct simpleNOT C++ FOR programs. SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 45 © Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 94289_CH02_DaleWeems.indd 45 1/17/13 4:00:46 PM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 46 CHAPTER 2 C++ Syntax and Semantics, and the Program Development Process 2.1 ©The Jones Elements & Bartlett of C++ Learning, Programs LLC © Jones & Bartlett Learning, LLC NOTProgrammers FOR SALE develop ORsolutions DISTRIBUTION to problems using a programming language.NOT FOR In this SALE chapter, OR DISTRIBUTION we start looking at the rules and symbols that make up the C++ programming language. We also review the steps required to create a program and make it work on a computer. C++ Program Structure © Jones & BartlettIn Chapter Learning, 1, we talked LLC about the four basic structures© Jones for expressing & Bartlett actions Learning, in a program- LLC NOT FOR SALEming OR language: DISTRIBUTION sequence, selection, loop, and subprogram.NOT FOR We said SALE that subprograms OR DISTRIBUTION allow us to write parts of our program separately and then assemble them into final Function ​A subprogram in C++. form. In C++, all subprograms are referred to as functions, and a C++ program is a collection of one or more functions. Each function performs some particular task, and collectively they all coop- © Jones & Bartlett Learning,erate LLCto solve the entire problem. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION main function © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Square function © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONCube function NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning,Every LLC C++ program must have© a functionJones named& Bartlett main. Execution Learning, of the LLC program always NOT FOR SALE OR DISTRIBUTIONbegins with the main function. YouNOT can think FOR of SALE main as ORthe master DISTRIBUTION and the other functions as the servants. When main wants the function Square to perform a task, main calls (or invokes) Square. When the Square function completes execution of its statements, it obedi- ently returns control to the master, main, so the master can continue executing. Let’s look at an example of a C++ program with three functions: main, Square, and Cube© Jones. Don’t &be Bartletttoo concerned Learning, with the detailsLLC in the program—just observe© Jones its overall & Bartlett look Learning, LLC andNOT structure. FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION //********************************************************* // This program demonstrates the use of three functions. © Jones & Bartlett//********************************************************* Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION #include <iostream> using namespace std; © Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 94289_CH02_DaleWeems.indd 46 1/17/13 4:00:46 PM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2.1 The Elements of C++ Programs 47 int Square( int ); int Cube(© Jonesint ); & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC int main()NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION { cout << "The square of 27 is " << Square(27) << endl; cout << "and the cube of 27 is " << Cube(27) << endl; return 0; © Jones} & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FORint Square( SALE ORint DISTRIBUTIONn ) NOT FOR SALE OR DISTRIBUTION { return n * n; } int Cube( int n ) © Jones & Bartlett {Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONreturn n * n * n; NOT FOR SALE OR DISTRIBUTION } In each of the three functions, the left brace ( { ) and the right brace ( } ) mark the beginning and the end of the statements to be executed. Statements appearing between the braces are known as ©the Jones body of & the Bartlett function. Learning, LLC © Jones & Bartlett Learning, LLC ExecutionNOT of FOR a program SALE always OR beginsDISTRIBUTION with the first statement of themain NOT function. FOR In SALE OR DISTRIBUTION our program, the first statement is cout << "The square of 27 is " << Square(27) << endl; This output statement causes information to be printed on the computer’s display screen. You will learn how to construct output statements later in the chapter. Briefly, this statement © Jonesprints & two Bartlett items. TheLearning, first is the LLC message © Jones & Bartlett Learning, LLC NOT FORThe square SALE of OR 27 DISTRIBUTIONis NOT FOR SALE OR DISTRIBUTION The second item to be printed is the value obtained by calling (invoking) the Square func- tion, with the value 27 as the number to be squared. As the servant, the Square function performs its task of squaring the number and sending the computed result (729) back to its caller, the main function. Now main can continue executing by printing the value 729 and © Jones & Bartlettproceeding Learning, to itsLLC next statement. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONIn a similar fashion, the second statementNOT in main FOR prints SALE the message OR DISTRIBUTION and the cube of 27 is and then invokes the Cube function and prints the result, 19683. The complete output pro- duced by executing this program is, therefore, © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The square of 27 is 729 and the cube of 27 is 19683 Both Square and Cube are examples of value-returning functions. A value-returning © Jonesfunction & Bartlett returns a Learning, single value toLLC its caller. The word int at© the Jones beginning & Bartlettof the first Learning, line LLC NOT FORof the SALESquare functionOR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION int Square( int n ) states that the function returns an integer value. © Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 94289_CH02_DaleWeems.indd 47 1/17/13 4:00:46 PM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 48 CHAPTER 2 C++ Syntax and Semantics, and the Program Development Process Now look at the main function again. You’ll see that the first line of the function is int© Jones main() & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The word int indicates that main is a value-returning function that should return an integer value. And it does: After printing the square and cube of 27, main executes the statement return 0; © Jones & Bartlettto return Learning, the value 0 toLLC its caller. But who calls the© main Jones function? & Bartlett The answer: Learning, the com -LLC NOT FOR SALEputer’s OR operating DISTRIBUTION system. NOT FOR SALE OR DISTRIBUTION When you work with C++ programs, the operating system is considered to be the caller of the main function. The operating system expects main to return a value when main finishes executing.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    48 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us