Chapter 1 Introduction to C Program Development and

  • Slides: 52
Download presentation
Chapter 1 Introduction to C++ Program Development and Design Using C++, Third Edition

Chapter 1 Introduction to C++ Program Development and Design Using C++, Third Edition

Objectives n n n Introduction to C++ Programming Style Data Types Arithmetic Operations Variables

Objectives n n n Introduction to C++ Programming Style Data Types Arithmetic Operations Variables and Declaration Statements Program Development and Design Using C++, Third Edition 2

Programming Languages n n n Computer program (or software): Selfcontained set of instructions and

Programming Languages n n n Computer program (or software): Selfcontained set of instructions and data used to produce specific result Programming: Developing and writing software Programming language: Set of instructions used to construct program q q Variety of forms and types All programs must be converted into machine language programs n Only type of program that can operate a computer Program Development and Design Using C++, Third Edition 3

Low- and High-Level Languages n Low-level languages: Directly tied to one type of computer

Low- and High-Level Languages n Low-level languages: Directly tied to one type of computer q n Machine and assembly languages High-level languages: Can be run on all computers q q Instructions resemble human languages e. g. , C++, C, Java, Visual Basic Program Development and Design Using C++, Third Edition 4

Low- and High-Level Languages (continued) n n n Must translate high-level language programs into

Low- and High-Level Languages (continued) n n n Must translate high-level language programs into machine language Interpreted language: Source program statements translated individually (by interpreter) and executed immediately Compiled language: Source program statements translated as complete unit (by compiler) before being executed q C++ is primarily compiled language Program Development and Design Using C++, Third Edition 5

Low- and High-Level Languages (continued) Figure 1. 10: Creating an Executable C++ Program Development

Low- and High-Level Languages (continued) Figure 1. 10: Creating an Executable C++ Program Development and Design Using C++, Third Edition 6

Low- and High-Level Languages (continued) n Object program: Output produced by compiler q n

Low- and High-Level Languages (continued) n Object program: Output produced by compiler q n Machine language version of source code Large C++ programs may be stored in multiple program files q Combine additional code with object program via Linker Program Development and Design Using C++, Third Edition 7

Procedural and Object Orientations n n High-level languages are procedural or object -oriented Procedural

Procedural and Object Orientations n n High-level languages are procedural or object -oriented Procedural language: Instructions used to create self contained units (procedures) q n Procedure accepts input data, transforms data, produces output data Object-oriented language: Program first defines objects it will be manipulating q q Describes general characteristics of objects and specific units to manipulate them Reuse of existing code Program Development and Design Using C++, Third Edition 8

The Development of C++ n n Syntax: Set of rules formulating grammatically correct language

The Development of C++ n n Syntax: Set of rules formulating grammatically correct language statements FOTRAN: First procedural language q n n n Engineering and scientific applications COBOL: Business applications BASIC: Straightforward, easy to understand PASCAL: Enforced structured programming Program Development and Design Using C++, Third Edition 9

The Development of C++ (continued) n C: Structured procedural language q q n Developed

The Development of C++ (continued) n C: Structured procedural language q q n Developed in 1970 s at AT&T Bell Laboratories Dominant applications language of 1980 s C++: Object-oriented programming language q q q Developed in early 1980 s Includes existing C features General-purpose programming language Program Development and Design Using C++, Third Edition 10

Producing a Program n n Program requirement: Statement of problem or specific request for

Producing a Program n n Program requirement: Statement of problem or specific request for program Four steps: q q Analyze the Problem Develop a Solution n q q Algorithm: Exact set of steps in solution Code the Solution Test and Correct the Program Development and Design Using C++, Third Edition 11

Algorithms n n n Algorithm: Step-by-step sequence of instructions describing how data are to

Algorithms n n n Algorithm: Step-by-step sequence of instructions describing how data are to be processed Programming is essentially translation of algorithms into language that computer can use Computers require detailed step-by-step sets of instructions Program Development and Design Using C++, Third Edition 12

A First C++ Program Development and Design Using C++, Third Edition 13

A First C++ Program Development and Design Using C++, Third Edition 13

The main() Function n C++ program must have exactly one function named main() q

The main() Function n C++ program must have exactly one function named main() q n Driver function Function header line: First line of function q q q What type of data is returned from function Name of function What type of data is sent into function Program Development and Design Using C++, Third Edition 14

The main() Function (continued) n n n Keyword before function name defines type of

The main() Function (continued) n n n Keyword before function name defines type of value function returns Arguments: Data transmitted to function at run time Braces, { and }, denote beginning and end of function body q n Statements inside determine what function does Statements end with semicolon (; ) Program Development and Design Using C++, Third Edition 15

The main() Function (continued) Figure 2. 4: The Structure of a main() Function Program

The main() Function (continued) Figure 2. 4: The Structure of a main() Function Program Development and Design Using C++, Third Edition 16

The cout Object n Preprocessor commands: Perform actions before compilation q q n n

The cout Object n Preprocessor commands: Perform actions before compilation q q n n Begin with # #include: Causes contents of file to be inserted istream and ostream provide data declarations and methods for data input and output Header file: Placed at top of C++ program using the #include command q Statement “using namespace std; ” tells compiler where to look for header files Program Development and Design Using C++, Third Edition 17

The cout Object (continued) n cout is object of a prewritten class q n

The cout Object (continued) n cout is object of a prewritten class q n n Can only perform certain well-defined actions When string of characters is passed to cout, the object displays it on the monitor Strings: Combination of letters, numbers, and special characters enclosed in double quotes Program Development and Design Using C++, Third Edition 18

Programming Style n n Execution starts at beginning of main() function Multiple statements can

Programming Style n n Execution starts at beginning of main() function Multiple statements can be put on line, or one statement can be written across lines q n Poor formatting There are standard formatting practices q e. g. , Indentation is good programming practice Program Development and Design Using C++, Third Edition 19

Comments n n Explanatory remarks within program Two types: q q Line: Begins with

Comments n n Explanatory remarks within program Two types: q q Line: Begins with two slashes (//) and continues to end of line Block: Begin with /* and end with */ n n Can span multiple lines Program’s structure intended to make it readable and understandable q q Extensive comments unnecessary Reinforced if identifiers are self-describing Program Development and Design Using C++, Third Edition 20

Data Numeric n integers n real numbers Textual n characters n strings Program Development

Data Numeric n integers n real numbers Textual n characters n strings Program Development and Design Using C++, Third Edition 21

Example #include <iostream> using namespace std; int main() { cout << "Two plus two

Example #include <iostream> using namespace std; int main() { cout << "Two plus two equals: "; cout << 2 + 2; return 0; } Program Development and Design Using C++, Third Edition 22

Data Types n n n Data type: Set of values and operations applicable to

Data Types n n n Data type: Set of values and operations applicable to these values Built-in data type (primitive type) : Provided as integral part of C++ compiler Literal: An acceptable value for data type q Also called a constant Program Development and Design Using C++, Third Edition 23

Data Types (continued) Figure 2. 7: Built-In Data Types Program Development and Design Using

Data Types (continued) Figure 2. 7: Built-In Data Types Program Development and Design Using C++, Third Edition 24

The int Data Type n Supports whole number values q q Positive or negative

The int Data Type n Supports whole number values q q Positive or negative Only allowed non-numerical symbols are + and - Program Development and Design Using C++, Third Edition 25

The char Data Type n Used to store individual characters q q q n

The char Data Type n Used to store individual characters q q q n n Letters of alphabet (upper and lower case) Digits 0 through 9 Special symbols such as + $. , ! Enclosed by single quotes Typically stored using ASCII or Unicode Program Development and Design Using C++, Third Edition 26

The bool Data Type n n Used to represent Boolean (logical) data Restricted to

The bool Data Type n n Used to represent Boolean (logical) data Restricted to one of two values: true or false Program Development and Design Using C++, Third Edition 27

Signed and Unsigned Data Types n Signed data type: Permits storage of negative values

Signed and Unsigned Data Types n Signed data type: Permits storage of negative values q n Unsigned data type: Only non-negative values q n e. g. , int e. g. , char and bool Unsigned integer types have double the range of signed counterparts Program Development and Design Using C++, Third Edition 28

Signed and Unsigned Data Types (continued) Table 2. 5: Integer Data Type Storage Program

Signed and Unsigned Data Types (continued) Table 2. 5: Integer Data Type Storage Program Development and Design Using C++, Third Edition 29

Floating-Point Types n Floating-point number: Number containing a decimal point q n Also called

Floating-Point Types n Floating-point number: Number containing a decimal point q n Also called a real number Three floating-point data types: q q q float (single precision) double (double precision) long double Program Development and Design Using C++, Third Edition 30

Floating-Point Types (continued) Table 2. 6: Floating-Point Data Types Program Development and Design Using

Floating-Point Types (continued) Table 2. 6: Floating-Point Data Types Program Development and Design Using C++, Third Edition 31

Exponential Notation n n Similar to scientific notation Used to express very large and

Exponential Notation n n Similar to scientific notation Used to express very large and very small values in compact form Letter e stands for exponent Number following e represents power of 10 q Indicates number of places to move decimal point Program Development and Design Using C++, Third Edition 32

Arithmetic Operations n n Arithmetic operators: Addition (+), Subtraction (-), Multiplication (*), Division (/),

Arithmetic Operations n n Arithmetic operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus division (%) Binary operators: Requires two operands Operand: Literal value or identifier that has associated value cout can display arithmetic expression results Program Development and Design Using C++, Third Edition 33

Expression Types n Expression: Combination of operators and operands evaluated to yield value q

Expression Types n Expression: Combination of operators and operands evaluated to yield value q q q Integer expression: Operands and result are integer values Floating-point expression: Operands and result are floating-point values Mixed-mode expressions: Operands are mixture of integer and floating-point values n n If both integers, result is integer If one is real, result is double-precision Program Development and Design Using C++, Third Edition 34

Integer Division n Integers cannot contain fractional part q q q n n Remainder

Integer Division n Integers cannot contain fractional part q q q n n Remainder is always dropped e. g. 15/2 is 7 15. 0/2 is 7. 5 Modulus operator (%): Captures remainder when integer is divided by integer For example: 9%4 is …. 17%3 is …. . 15%4 is …. . 14%2 is …. . Program Development and Design Using C++, Third Edition 35

Negation n Unary operator: Operates on single operand q e. g. , minus (-)

Negation n Unary operator: Operates on single operand q e. g. , minus (-) sign Program Development and Design Using C++, Third Edition 36

Negation (continued) Table 2. 7: Summary of Arithmetic Operators Program Development and Design Using

Negation (continued) Table 2. 7: Summary of Arithmetic Operators Program Development and Design Using C++, Third Edition 37

Operator Precedence and Associativity Rules when writing expressions containing more than one arithmetic operator:

Operator Precedence and Associativity Rules when writing expressions containing more than one arithmetic operator: n q q n Cannot place two binary arithmetic operator symbols side by side Parentheses may be used to form groupings Sets of parentheses may be enclosed by other parentheses Parentheses not used to indicate multiplication Parentheses should specify logical groupings of operands Program Development and Design Using C++, Third Edition 38

Operator Precedence and Associativity (continued) Table 2. 8: Operator Precedence and Associativity Program Development

Operator Precedence and Associativity (continued) Table 2. 8: Operator Precedence and Associativity Program Development and Design Using C++, Third Edition 39

Examples on precedence 1. 8 + 5*7%2*4= 8+35%2*4= 8+1*4= 8+4= 12 2. 4/8*6. 0=

Examples on precedence 1. 8 + 5*7%2*4= 8+35%2*4= 8+1*4= 8+4= 12 2. 4/8*6. 0= 0*6. 0=0 Program Development and Design Using C++, Third Edition 40

Variables n n n All values used in computer program are stored in computer’s

Variables n n n All values used in computer program are stored in computer’s memory unit Variables: Symbolic names used in place of memory addresses q Value stored in variable can change Example of a declaration statement: int first. Num; n n This means that we can use an integer variable called first. Num in our program. C++ is case sensitive. The names firstnum, First. Num and FIRSTNUM do not refer to the same variable as first. Num Program Development and Design Using C++, Third Edition 41

Example 1 #include <iostream> using namespace std; int main() { int num 1; int

Example 1 #include <iostream> using namespace std; int main() { int num 1; int num 2; int total; num 1 = 20; num 2 = 30; total = num 1 + num 2; cout << "The sum is " << total << endl; return 0; } Program Development and Design Using C++, Third Edition 42

Example 2 #include <iostream> using namespace std; int main() { int num 1, num

Example 2 #include <iostream> using namespace std; int main() { int num 1, num 2, total; num 1 = 20; num 2 = 30; total = num 1 + num 2; cout << num 1 << " + " <<num 2<< " = " << total << endl; return 0; } Program Development and Design Using C++, Third Edition 43

Example 3 #include <iostream> using namespace std; int main() { double grade 1, grade

Example 3 #include <iostream> using namespace std; int main() { double grade 1, grade 2, total, average; grade 1 = 85. 5; grade 2 = 97. 0; total = grade 1 + grade 2; average = total/2. 0; // divide the total by 2. 0 cout << "The average grade is " << average << endl; return 0; } Program Development and Design Using C++, Third Edition 44

Example 4 #include <iostream> using namespace std; int main() { char ch= 'A'; //

Example 4 #include <iostream> using namespace std; int main() { char ch= 'A'; // this declares and initializes a character variable to A cout << "The character stored in ch is " << ch << endl; ch = 'm'; cout << "The character now stored in ch is "<< ch << endl; return 0; } Program Development and Design Using C++, Third Edition 45

Example 5 #include <iostream> using namespace std; int main() { char ch= 65; //

Example 5 #include <iostream> using namespace std; int main() { char ch= 65; // this declares and initializes a character variable to A cout << "The character stored in ch is " << ch << endl; ch = 'm'; cout << "The character now stored in ch is "<< ch << endl; return 0; } Program Development and Design Using C++, Third Edition 46

Common Programming Errors n n n Errors like division-by-zero occur as a program runs,

Common Programming Errors n n n Errors like division-by-zero occur as a program runs, so they are called run-time errors or execution errors. Forgetting to include the iostream file in a program that inputs data from the keyboard or outputs data to the screen causes the compiler to issue an error message. Omitting the semicolon at the end of a statement. This is a syntax error, i. e. a violation of the language. Syntax errors are also called compile-time errors. Program Development and Design Using C++, Third Edition 47

Common Programming Errors n n n Attempting to use the modulus operator, % with

Common Programming Errors n n n Attempting to use the modulus operator, % with non-integer operands (syntax error). Integer division, e. g. 4/8 gives 0, whereas 0. 5 was intended. This can simply give an incorrect result (0 instead of 0. 5) or may further lead to run-time error if we attempt to use this value (0) later on in the program as a denominator. Direction of chevrons “cout>>”. Program Development and Design Using C++, Third Edition 48

Common Programming Errors n n n Omitting the parentheses after main Omitting or incorrectly

Common Programming Errors n n n Omitting the parentheses after main Omitting or incorrectly typing the opening brace, {, that signifies the start of function body Omitting or incorrectly typing the closing brace, }, that signifies the end of function Misspelling name of object or function; for example, typing cot instead of cout Forgetting to close string sent to cout with double quote symbol Program Development and Design Using C++, Third Edition 49

Common Programming Errors (continued) n n n Forgetting to separate data streams passed to

Common Programming Errors (continued) n n n Forgetting to separate data streams passed to cout with insertion (“put to”) symbol, << Omitting semicolon at end of C++ statements Adding semicolon to end of #include Forgetting the n to indicate new line Incorrectly typing letter O for number zero (0), or letter l for number 1 Program Development and Design Using C++, Third Edition 50

Common Programming Errors (continued) n n Forgetting to declare all variables used in program

Common Programming Errors (continued) n n Forgetting to declare all variables used in program Storing inappropriate data type in declared variable Using variable in expression before a value has been assigned to variable Dividing integer values incorrectly Program Development and Design Using C++, Third Edition 51

Reading and Homework n n Chapter 2: Everything up until page 83 Homework: q

Reading and Homework n n Chapter 2: Everything up until page 83 Homework: q q q Exercise given in lab pg. 91: exercise 9, 10 and 11. All homework programs should be typed, compiled and executed following instructions on tutorial handed out in lab. (Web site for downloading Microsoft Visual C++ 2008 Express: www. microsoft. com/express)