1 / 69

Chapter One Introduction to Programming

Chapter One Introduction to Programming. Objectives. You should be able to describe: Introduction to Programming Function and Class Names The cout object Programming Style Common Programming Errors. History of C++.

imani-roman
Download Presentation

Chapter One Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter One Introduction to Programming

  2. Objectives You should be able to describe: • Introduction to Programming • Function and Class Names • The cout object • Programming Style • Common Programming Errors

  3. History of C++ • C++ began as extension to C, which is procedural language developed in the 1970s at AT&T Bell Laboratories • In early 1980s, Bjarne Stroustrup (also at AT&T) used his background in simulation languages to develop C++ • Object-orientation and other procedural improvements were combined with existing C language features to form C++

  4. Introduction to Programming • Computer program: Data and instructions used to operate a computer • Programming: Writing computer program in a language that the computer can respond to and that other programmers can understand • Programming language: Set of instructions, data, and rules used to construct a program • High-level languages use human language type instructions • Low-level languages use instructions tied to a computer type

  5. Procedural Programming Languages • Instructions are used to create self-contained units (procedures) • Procedures accept data as input and transform data to produce a specific result as an output • Initially, high-level programming languages were predominately procedural

  6. Procedure-Oriented Programs • Most high-level programs process data to produce one or more results • Procedural programs are constructed from sets of instructions, with each set called a procedure • Each procedure moves the data one step closer to the final desired output

  7. Procedure-Oriented Programs (continued)

  8. Object-Oriented Languages • Allow for procedural instructions and for definitions of objects to be manipulated • Such definitions include: • The general characteristics of objects • Specific operations to manipulate objects • C++ is an object-oriented language • Has procedures and objects • Supports code reuse

  9. Programming • Programming=represent information + process information

  10. Information • Data classified as different types • Numeric data • Logic data • Characters and sentences • …

  11. Information representation • Factorize information • Some are constants, some are variables. • Information decomposition • Information type identification

  12. Information processing • Operations • simple statements using supported operators • Compound statements by combining supported operations

  13. Algorithms and Procedures • Before writing a program, a programmer must clearly understand • What data is to be used • Desired result • Procedure needed to produce this result • The procedure is referred to as an algorithm • Algorithm: Step-by-step sequence of instructions describing how to perform a computation

  14. Example of an Algorithm • Assume that a program must calculate sum of all whole numbers from 1 through 100 • A computer can not respond to heuristic command: “Add the numbers from 1 - 100” • A computer is algorithm-responding machine and not intuition-responding machine • Several methods or algorithms can be used to find the required sum

  15. Example of an Algorithm (continued) Sum = n(a + b)/2 Where n = number of terms to be added (100) a = first number added (1) b = last number to be added (100) Sum = 100(1 + 100)/2 = 5050 Figure 1.2: Summing the Numbers from 1 through 100 Method 3. Formula - Use the formula Sum = n(a + b)/2 = 5050

  16. Flowcharting

  17. Flowcharting

  18. Flowchart Example

  19. Classes and Objects • Data Object: Set of values packaged as single unit • Class: Set of objects with similar attributes • General concept of object-oriented programming is difference between an object and the larger set of which it is a member (class) • A red, Ford Taurus sedan is an instance, or object, of general class of automobiles

  20. Program Translation • C++ source program: Set of instructions written in C++ language • Machine language: Internal computer language • Consists of a series of 1s and 0s • Source program cannot be executed until it is translated into machine language • Interpreted language translates one statement at a time • Compiled language translates all statements together

  21. Program Translation (continued)

  22. Function and Class Names • Modular programs: Segments arranged in logical order to form an integrated unit • Module: Segments of modular program • Function: Name of a C++ procedure • Composed of sequence of C++ instructions • Function interface is its inputs and outputs • Method of converting input to results is encapsulated and hidden within function

  23. Function and Class Names (continued)

  24. Function and Class Names (continued)

  25. Function and Class Naming Conventions • Identifiers: Names that convey an idea of the purpose of function or class • Identifier composition rules: (read page 15) • First character must be a letter or underscore • Only letter, digit or underscore may follow • Blank spaces NOT allowed • Identify component words with initial capitalization • Cannot be C++ keyword • Should be a mnemonic

  26. Tokens are basic building blocks of C/C++ programming. • In a C/C++ source program, the basic element recognized by the compiler is the "token.“. • A token is source-program text that the compiler does not further break down into component elements.

  27. tokens • keyword • identifier • Constant number • string-literal • operator • Punctuator (special symbol) • Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.

  28. Literals (constants) • A literal is a notation for representing a value within source code. • Literal constants (often referred to as literals or constants as apposed to symbolic constant identifiers) are invariants whose values are implied by their representations. • A letter or symbol that stands for itself as opposed to a feature, function, or entity associated with it in a programming language: • + can be a symbol that refers to add operator, but as a literal, it is a ‘+’ character, as it is.

  29. Literal numbers: Literal strings: “Obama”, “Bush”, "hello” • Literal characters 'b', 'c' (character) • Literal integers: 14 • Literal doubles: 3.1 (default double) • 3.1l(double) • 3.1f (float) • 18.46e1 (scientific notation) • We call the f in 3.1f the suffix modifier. The type of a literal is thus determined from its syntactic form.

  30. operators • C/C++ operators can be used to manipulate Variables and constants using complex expressions. • In C++, operators can be overloaded and their meanings can be user-defined. However, their precedence and the number of operands they take cannot be modified. • At minimum we need to be aware of the syntax and semantics of operators as they are supplied with the language, not overloaded.

  31. Punctuators (special symbols) • Punctuators in C++ have syntactic and semantic meaning to the compiler but do not, of themselves, specify an operation that yields a value. • Some punctuators, either alone or in combination, can also be C++ operators or be significant to the preprocessor. • Any of the following characters are considered punctuators: • ! % ^ & * ( ) – + = { } | ~ [ ] \ ; ' : " < > ? , . / # • The punctuators [ ], ( ), and { } must appear in pairs

  32. C++ Keywords

  33. Fundamental data types When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. A data type or datatype defines kind of data, the size of memory occupied, a set of possible values, the range, as well as basic operations on those values. The memory in our computers is organized in bytes. A byte is the basic unit, the minimum amount, that can be manipulated in memory. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.

  34. Built-in data types as well as the range of values that can be represented

  35. C++ Identifiers • Examples of valid identifiers: grosspay taxCalc addNums degToRad multByTwo salesTax netPay bessel Usually identifiers are used to name variables, constant symbols, functions, labels etc.

  36. C++ Identifiers (continued) • Examples of invalid identifiers: 4ab3 (begins with a number) e*6 (contains a special character) while (is a keyword)

  37. variables • A variable in computer source code is a data storage space located somewhere in memory. • It is identified by a name. • A variable’s name won’t change, so we can program with respect to the name. • But the content in the space represented by the name is changeable. • Generally the value change during the course of program execution.

  38. variables • A variable in C++ must be declared (the type of variable) and defined (values assigned to a variable) before it can be used in a program

  39. Expressions • Expressions are sequences of operators and operands that are used for one or more of these purposes: • Computing a value from the operands. • Designating objects or functions. • types of expressions • semantics of expressions

  40. Soul of your program • Input and output support allow your program interactive • Variables make your program reusable without recompiling your program • Expressions make your program intelligent. • Variables +expressions = soul of your program

  41. Types of expressions • C++ expressions are divided into several categories: • Primary expressions. These are the building blocks from which all other expressions are formed. • Postfix expressions. These are primary expressions followed by an operator — for example, the array subscript or postfix increment operator. • Expressions formed with unary operators. Unary operators act on only one operand in an expression. • Expressions formed with binary operators. Binary operators act on two operands in an expression. • Expressions with the conditional operator. The conditional operator is a ternary operator — the only such operator in the C++ language — and takes three operands. • Constant expressions. Constant expressions are formed entirely of constant data. • Expressions with explicit type conversions. Explicit type conversions, or "casts," can be used in expressions. • Expressions with pointer-to-member operators. • Casting. Type-safe "casts" can be used in expressions. • Run-Time Type Information. Determine the type of an object during program execution.

  42. Semantics of Expressions • Order of evaluation • Sequence points • Ambiguous expressions • Notation in expressions • Side effects

  43. The main Function • Each C+ program must have one and only one function named main • Called a driver functionbecause it drives the other modules

  44. The main Function (continued)

  45. The main Function (continued) • First line of function is called header line • What type of data, if any, is returned from function • The name of function • What type of data, if any, is sent into function • Data transmitted into function at run time are referred to as arguments of function

  46. main Function Composition

  47. The cout Object • The cout object sends data to the standard output display device • The display device is usually a video screen • Name derived from Console OUTput and pronounced “see out” • Data is passed to cout by the insertion symbol cout << “Hello there, World!”;

  48. C++ Sample Code using cout

  49. Newline Escape Sequence • Instructs the display device to move to a new line • A newline caused when the characters backslash \ and n are used together • Backslash provides an “escape” from the normal interpretation of the character that follows • Newline escape sequences can be placed anywhere within a message to cout

More Related