Download presentation
Presentation is loading. Please wait.
Published byValentine McKinney Modified over 9 years ago
1
Chapter 2: Introduction to C++
2
Outline Basic “Hello World!!” Variables Data Types Illustration
3
The main function int main ( ) heading { statements body return 0; } All C++ programs must have a function named main There are other things need to be specified before main. The body of the main program, must be inside the braces
4
Function heading int main ( ) { } type of returned value name of function says no parameters
5
Slide 2- 5 Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } comment preprocessor directive which namespace to use beginning of function named main beginning of block for main output statement string literal send 0 to operating system end of block for main
6
#include preprocessor directives There are build in software libraries in C++ to handle common tasks To use these library functions, we need to use the #include preprocessor directive #include Allows the iostream library, which handles regular I/O, to be used in the program Other libraries:,,,,,,, … Inserts the contents of another file into the program Do not place a semicolon at end of #include line
7
Namespace C++ uses namespaces to organize the names of program entities The namespace called std is used in the sample program. It is because of that every name created by the iostream file is part of the std namespace. In this semester, we use the namespace std mostly.
8
Slide 2- 8 Special Characters CharacterNameMeaning // Double slashBeginning of a comment # Pound signBeginning of preprocessor directive Open/close bracketsEnclose filename in #include ( ) Open/close parentheses Used when naming a function { } Open/close braceEncloses a group of statements " Open/close quotation marks Encloses string of characters ; SemicolonEnd of a programming statement
9
Slide 2- 9 The cout Object Displays output on the computer screen You use the stream insertion operator << to send output to cout : cout << "Programming is fun!";
10
Slide 2- 10 The cout Object Can be used to send more than one item to cout: cout << "Hello " << "there!"; Or: cout << "Hello "; cout << "there!";
11
Slide 2- 11 The cout Object cout << "Programming is "; cout << "fun!"; This produces one line of output
12
The cout statements SYNTAX These examples yield the same output: cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; cout << Expression << Expression... ;
13
Slide 2- 13 The endl Manipulator Programming is fun! cout << "Programming is" << endl; cout << "fun!"; You can use the endl manipulator to start a new line of output. This will produce two lines of output: a lowercase L
14
Slide 2- 14 The endl Manipulator You do NOT put quotation marks around endl The last character in endl is a lowercase L, not the number 1. endl This is a lowercase L
15
Slide 2- 15 The \n Escape Sequence You can also use the \n escape sequence to start a new line of output. This will produce two lines of output: cout << "Programming is\n"; cout << "fun!"; Notice that the \n is INSIDE the string. Programming is fun!
16
Example 1 What is the output from the next program? #include using namespace std; int main( ) { cout << "Computers, computers everywhere\n as far as\n\nI can C"; return 0; }
17
Example 1- Solution Computers, computers everywhere as far as I can C
18
Example 2 What is the output from the next program? #include using namespace std; int main( ) { cout << "Computers, computers everywhere" << endl; cout << " as far as" << endl << endl; cout << "I can C"; return 0; } Same as the results of the previous slide!
19
The cout object The cout object followed with << (the insertion operator) displays the sentence between the double quotation marks on the screen << endl ends a line, ‘\n’ does the same
20
The cout object Other escape sequences: \t for tab; \a for alarm; \b for backspace; \r for return to the beginning of the current line; \\ for backslash; \’ for single quotation mark; \” for double quotation mark
21
Slide 2- 21 Comments Used to document parts of the program Explanatory remarks made within the program Intended for persons reading the source code of the program: – Indicate the purpose of the program – Describe the use of variables – Explain complex sections of code Are ignored by the compiler
22
Slide 2- 22 Single-Line Comments Begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width;
23
Slide 2- 23 Multi-Line Comments Begin with /*, end with */ Can span multiple lines: /* this is a multi-line comment */ Can begin and end on the same line: int area; /* calculated area */
24
Slide 2- 24 Programming Style The visual organization of the source code Includes the use of spaces, tabs, and blank lines Does not affect the syntax of the program Affects the readability of the source code
25
Slide 2- 25 Programming Style Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of braces Blank lines between declaration and other statements Long statements wrapped over multiple lines with aligned operators
26
Slide 2- 26 Except for strings, double quotes, identifiers, and keywords, C++ ignores all whitespace (whitespace is any combination of one or more blank spaces, tabs, or new lines). For example, this is a valid program: // Hello World Program #include int main ( ){ cout << "Welcome to UCA!"; return 0;}
27
Slide 2- 27 Although the previous version of the program works, it is an example of extremely poor programming style. It is difficult to read and understand. The main( ) function should always be written in the standard form below : // Hello World Program #include using namespace std; int main( ) { cout<<“Hello World!”; return 0; }
28
Outline Basic “Hello World!!” Variables Data Types Illustration
29
Variable A variable is a location in memory which we can refer to by its name (identifier), and in which a data value, that can be changed later, is stored. Declaring a variable means specifying both its name (identifier) and its data type
30
Slide 2- 30 Variable Initialization To initialize a variable means to assign it a value when it is declared: int length = 12; Can initialize some or all variables: int length = 12, width = 5, area; Not initialized
31
Slide 2- 31 Variable Assignments and Initialization An assignment statement uses the = operator to store a value in a variable. item = 12; This statement assigns the value 12 to the item variable.
32
Assign a value to a variable You can assign (give) a value to a variable by using the assignment operator = VARIABLE DECLARATIONS string firstName ; char middleInitial ; char letter ; int ageOfDog; VALID ASSIGNMENT STATEMENTS firstName = “Fido” ; middleInitial = ‘X’ ; letter = middleInitial ; ageOfDog = 12 ;
33
Slide 2- 33 Assignment The variable receiving the value must appear on the left side of the = operator. This will NOT work: // ERROR! 12 = item;
34
Slide 2- 34 Variables and Literals Variable: a storage location in memory – Syntax: data-type variable-name; – Has a name and a type of data it can hold – Must be declared before it can be used: int item;
35
Slide 2- 35 Variable Declaration
36
Slide 2- 36 Variable Declaration
37
Slide 2- 37 Literals Literal: a value that is written into a program’s code. "hello, there" (string literal) 12 (integer literal)
38
Slide 2- 38 20 is an integer literal
39
Slide 2- 39 This is a string literal
40
Slide 2- 40 This is also a string literal
41
What is an identifier? An identifier is a programmer-defined name for some part of a program: variables, functions, etc. A programmer chooses identifiers as long as the rules are followed, however, using meaningful (mnemonic) identifiers is a good programming practice C++ is a case-sensitive language, lower case and upper case are different
42
Slide 2- 42 C++ Key Words You cannot use any of the C++ key words as an identifier. These words have reserved meaning.
43
Slide 2- 43 C++ Key Words You cannot use any of the C++ key words as an identifier. These words have reserved meaning.
44
Slide 2- 44 Variable Names A variable name should represent the purpose of the variable. For example: items_ordered The purpose of this variable is to hold the number of items ordered.
45
Slide 2- 45 Identifier Rules The first character of an identifier must be an alphabetic character or an underscore ( _ ), After the first character you may use alphabetic characters, numbers, or underscore characters, no special characters are allowed ( !, @, #, $, %, &, *, etc.), or commas. Upper- and lowercase characters are distinct (C++ is a case sensitive language).
46
Slide 2- 46 Valid and Invalid Identifiers – Try? IDENTIFIERVALID?REASON IF INVALID totalSales total_Sales total.Sales 4thQtrSales totalSale$
47
Slide 2- 47 Valid and Invalid Identifiers IDENTIFIERVALID?REASON IF INVALID totalSales Yes total_Sales Yes total.Sales NoCannot contain. 4thQtrSales NoCannot begin with digit totalSale$ NoCannot contain $
48
Components and Order of statements in a program #include using namespace std; int main () { // variable declarations. // statements statement 1; statement 2;. statement n; return 0; } where we define names and types of data to be manipulated by our program instructions for computer operation
49
Outline Basic “Hello World!!” Variables Data Types int char & string float bool Illustration
50
Slide 2- 50 Integer Data Types Integer variables can hold whole numbers such as 12, 7, and - 99.
51
Slide 2- 51 Declaring Variables Variables of the same type can be declared - On separate lines: int length; int width; unsigned int area; - On the same line: int length, width; unsigned int area; Variables of different types must be in different declarations
52
Declaring a variable A declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location. int ageOfDog; float taxRate; char middleInitial ; 4 bytes for ageOfDog ageOfDog
53
Slide 2- 53 This program has three variables: checking, miles, and days
54
Slide 2- 54 This program has three variables: checking, miles, and days
55
Slide 2- 55 Integer Literals An integer literal is an integer value that is typed into a program’s code. For example: items_ordered = 15; In this code, 15 is an integer literal.
56
Slide 2- 56 Integer Literals
57
Slide 2- 57 The char Data Type Used to hold characters or very small integer values Usually 1 byte of memory Character literals must be enclosed in single quote marks. Example: 'A' Numeric value of character from the character set is stored in memory: CODE: char letter; letter = 'C'; MEMORY: letter 67
58
ASCII Code
59
Slide 2- 59 What is the output?
61
Slide 2- 61 Character Strings A series of characters in consecutive memory locations: string greeting; greeting = “Hello”; Stored with the null terminator, \0, at the end: Comprised of the characters between the “” Hello\0
63
Slide 2- 63 Floating-Point Data Types The floating-point data types are: float double long double They can hold real numbers such as: 12.45 -3.8 Stored in a form similar to scientific notation All floating-point numbers are signed
64
Slide 2- 64 Floating-point Literals Can be represented in – Fixed point (decimal) notation: 31.41590.0000625 – E notation: 3.14159E16.25e-5 Are double by default
65
Samples of C++ data values int sample values 4578 -4578 0 float or double sample values 95.274 95. -0.265 char sample values ‘B’ ‘d’ ‘4’ ‘?’‘*’
66
Represents values that are true or false bool variables are stored as small integers false is represented by 0, true by 1: bool allDone = true; bool finished = false; The bool Data Type allDone 10 finished
69
Outline Basic “Hello World!!” Variables Data Types Illustration
70
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2
71
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 2 3
72
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 3
73
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 3
74
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 8 2 10 3
75
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 3
76
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 20 10 3
77
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 20 10 3 30
78
#include using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } Test Program Illustrating Assignment length1 width1 area1 length2 width2 area2 10 2 20 10 3 40
79
Slide 2- 79 Arithmetic Operators Used for performing numeric calculations C++ has unary, binary, and ternary operators: – unary (1 operand) -5 – binary (2 operands) 13 - 7
80
Slide 2- 80 Binary Arithmetic Operators SYMBOLOPERATIONEXAMPLEVALUE OF ans + addition ans = 7 + 3;10 - subtraction ans = 7 - 3;4 * multiplication ans = 7 * 3;21 / division ans = 7 / 3;2 % modulus ans = 7 % 3;1
81
Slide 2- 81 A Closer Look at the / Operator / (division) operator performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 91 / 7; // displays 13 If either operand is floating point, the result is floating point cout << 13 / 5.0; // displays 2.6 cout << 91.0 / 7; // displays 13.0
82
Slide 2- 82 A Closer Look at the % Operator % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 % requires integers for both operands cout << 13 % 5.0; // error
83
What’s the output?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.