Upload
valentine-mckinney
View
223
Download
4
Tags:
Embed Size (px)
Citation preview
Chapter 2:
Introduction to C++
Outline
• Basic “Hello World!!”• Variables• Data Types• Illustration
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
Function heading
int main ( ){
}
type of returned value name of function says no
parameters
Slide 2- 5
Parts of a C++ Program
// sample C++ program#include <iostream>using namespace std;int main() {
cout << "Hello, there!";return 0;
}
commentpreprocessor
directive
which namespaceto use
beginning offunction named main
beginning of block for main
output statement
stringliteral
send 0 tooperating system
end of blockfor main
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!";
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!";
Slide 2- 11
The cout Object
• cout << "Programming is ";cout << "fun!";
• This produces one line of output
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 . . . ;
Slide 2- 13
The endl Manipulator
Programming isfun!
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
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!
Example 1
• What is the output from the next program?
#include <iostream>using namespace std;
int main( ){ cout << "Computers, computers everywhere\n as far as\n\nI can
C"; return 0;}
Example 1- Solution
Computers, computers everywhere as far as
I can C
Example 2
• What is the output from the next program?
#include <iostream>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!
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
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
Slide 2- 22
Single-Line Comments
Begin with // through to the end of line:int length = 12; // length in inchesint width = 15; // width in inchesint area; // calculated area
// calculate rectangle areaarea = length * width;
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 */
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
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 <iostream>int main( ){ cout << "Welcome to UCA!"; return 0;}
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 <iostream>using namespace std;
int main( ) { cout<<“Hello World!”; return 0; }
Outline
• Basic “Hello World!!”• Variables• Data Types• Illustration
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
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.
Assign a value to a variableYou 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 ;
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;
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;
Slide 2- 35
Variable Declaration
Slide 2- 36
Variable Declaration
Slide 2- 37
Literals
• Literal: a value that is written into a program’s code.
"hello, there" (string literal)12 (integer literal)
Slide 2- 38
20 is an integer literal
Slide 2- 39
This is a string literal
Slide 2- 40
This is also a string literal
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
Slide 2- 42
C++ Key Words
You cannot use any of the C++ key words as an identifier. These words have reserved meaning.
Slide 2- 43
C++ Key Words
You cannot use any of the C++ key words as an identifier. These words have reserved meaning.
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.
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).
Slide 2- 46
Valid and Invalid Identifiers – Try?
IDENTIFIER VALID? REASON IF INVALID
totalSales
total_Sales
total.Sales
4thQtrSales
totalSale$
Slide 2- 47
Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
total.Sales No Cannot contain .
4thQtrSales No Cannot begin with digit
totalSale$ No Cannot contain $
Components and Order of statements in a program #include <iostream>using namespace std;int main (){
// variable declarations..// statementsstatement 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
Outline
• Basic “Hello World!!”• Variables• Data Types
• int• char & string• float • bool
• Illustration
Slide 2- 50
Integer Data Types• Integer variables can hold whole numbers such as 12, 7, and -
99.
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
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
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
ASCII Code
Slide 2- 59
What is the output?
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 “”
H e l l o \0
Slide 2- 63
Floating-Point Data Types
• The floating-point data types are:floatdoublelong 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
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
1 0
finished
Outline
• Basic “Hello World!!”• Variables• Data Types• Illustration
#include <iostream>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
#include <iostream>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
#include <iostream>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
#include <iostream>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
10
3
#include <iostream>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
#include <iostream>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
10
3
#include <iostream>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
#include <iostream>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
#include <iostream>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
Slide 2- 80
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE 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
Slide 2- 81
A Closer Look at the / Operator
• / (division) operator performs integer division if both operands are integerscout << 13 / 5; // displays 2cout << 91 / 7; // displays 13
• If either operand is floating point, the result is floating pointcout << 13 / 5.0; // displays 2.6cout << 91.0 / 7; // displays 13.0
Slide 2- 82
A Closer Look at the % Operator
• % (modulus) operator computes the remainder resulting from integer divisioncout << 13 % 5; // displays 3
• % requires integers for both operandscout << 13 % 5.0; // error
What’s the output?